Quantcast
Channel: VMware Communities : Discussion List - vRealize Orchestrator
Viewing all articles
Browse latest Browse all 6251

Utilizing Python to Interface with the API (Orchestrator 5.1.2)

$
0
0

So I'm having a problem getting the POST operation (Below - # Start execution) to work. Everything, I have tried has resulted in a 400 error. I have also tried setting the post variable just using a string and the JSON ({"parameters":....}). If anyone can give me a some help figuring out what I'm doing wrong here it would be appreciated.

# Retrive API URL for a workflow
import pycurl, json, pprint
from io import BytesIO
data = BytesIO()
orchestrator_url = "https://server.domain.com:8281/api/workflows?conditions=name=Workflow%20Name"
user_pwd = "user:******"
c = pycurl.Curl()
c.setopt(pycurl.URL, orchestrator_url)
c.setopt(pycurl.USERPWD, user_pwd)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEFUNCTION, data.write)
c.perform()
response = json.loads(data.getvalue())
for attribute in response['links'][0]['attributes']:   if attribute['name'] == 'itemHref':     orchestrator_url = attribute['value'].decode("utf-8").encode("ascii","ignore")     break
# Start execution
data = BytesIO()
orchestrator_url += "executions"
parameters = {}
parameters['parameters'] = [1]
parameters['parameters'][0] = {}
parameters['parameters'][0]['scope'] = "LOCAL"
parameters['parameters'][0]['type'] = "string"
parameters['parameters'][0]['name'] = "environment"
parameters['parameters'][0]['value'] = {}
parameters['parameters'][0]['value']['value'] = "Production"
parameters['parameters'][0]['value']['objectType'] = "string"
post =  json.dumps(parameters)
# post = {"parameters": [{"scope": "LOCAL", "type": "string", "name": "environment", "value": {"value": "Production", "objectType": "string"}}]}
# post = json.dumps({"parameters": [{"scope": "LOCAL", "type": "string", "name": "environment", "value": {"value": "Production", "objectType": "string"}}]})
c.setopt(pycurl.URL, orchestrator_url)
c.setopt(pycurl.USERPWD, user_pwd)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEFUNCTION, data.write)
c.setopt(pycurl.HTTPHEADER, ['Accept: application/json;v=5.1.1', 'Content-Type: application/json;v=5.1.1'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, post)
# c.setopt(pycurl.POSTFIELDS, '%s' % post)
c.perform()
response = json.loads(data.getvalue())
c.setopt(pycurl.HTTPHEADER, ['Accept: application/json;v=5.1.1', 'Content-Type: application/json;v=5.1.1'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, '%s' % post)
c.perform()
response = json.loads(data.getvalue())

 

Just for reference. The JSON for a completed execution:

{u'endDate': 1419616471963,
u'href': u'https://server.domain.com:8281/api/workflows/ae280954-0442-4342-a60d-374ed0f9772e/executions/402880114a7d7df6014a87bc467f0037/',
u'id': u'402880114a7d7df6014a87bc467f0037',
u'inputParameters': [{u'name': u'environment',                       u'scope': u'LOCAL',                       u'type': u'string',                       u'value': {u'objectType': u'string',                                  u'value': u'Production'}}],
u'name': u'Workflow Name',
u'outputParameters': [{u'name': u'output',                        u'scope': u'LOCAL',                        u'type': u'string',                        u'value': {u'objectType': u'string',                                   u'value': u'e4ecf81b7384'}}],
u'relations': {u'links': [{u'href': u'https://server.domain.com:8281/api/workflows/ae280954-0442-4342-a60d-374ed0f9772e/executions/',                            u'rel': u'up'},                           {u'href': u'https://server.domain.com:8281/api/workflows/ae280954-0442-4342-a60d-374ed0f9772e/executions/402880114a7d7df6014a87bc467f0037/',                            u'rel': u'remove'},                           {u'href': u'https://server.domain.com:8281/api/workflows/ae280954-0442-4342-a60d-374ed0f9772e/executions/402880114a7d7df6014a87bc467f0037/logs/',                            u'rel': u'logs'},                           {u'href': u'https://server.domain.com:8281/api/workflows/ae280954-0442-4342-a60d-374ed0f9772e/executions/402880114a7d7df6014a87bc467f0037/state/',                            u'rel': u'state'}]},
u'startDate': 1419616470657,
u'startedBy': u'user@server.domain.com',
u'state': u'COMPLETED'}

 

And the typical response I get back:

HTTP Status 400 -


type Status report

message

descriptionThe request sent by the client was syntactically incorrect ().


JBossWeb/2.0.1.GA


Viewing all articles
Browse latest Browse all 6251

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>