Skip to content

Instantly share code, notes, and snippets.

@alanphil
Forked from stevenbryen/gist:3775710
Last active August 29, 2015 14:06
Show Gist options
  • Save alanphil/51732bb05b54c7e83772 to your computer and use it in GitHub Desktop.
Save alanphil/51732bb05b54c7e83772 to your computer and use it in GitHub Desktop.
# A simple example of calling the VCO 5.1 REST API using Ruby. This example uses the HTTParty Gem
# to POST some XML to the new RESTful API. There are two examples (more coming) which are:
# getWorkflow(id) - This will return a workflow with a given workflow ID, HTTParty will allow you to access the response
# using the syntax response["inputParameters"] for example.
# executeWorkflow(id, inputxml) - This will allow you to execute a Workflow via the API. I was unable to get a workflow
# to execute using a hash input, so i opted for the ugly xml string option. I think this might be to do with the namespace
# I will hopefully have this as a simple hash very soon but for now it accepts the raw xml as a string.
#Usage - Update base_uri variable with your VCO server IP/Hostname, examples are at the bottom of the gist.
class Vco
require 'httparty'
include HTTParty
headers 'Content-Type' => 'application/xml'
base_uri 'https://<vco-server-url>:8281/api'
def initialize(u,p)
@auth = {:username => u, :password => p}
end
def getWorkflow(id)
#Get Workflow information from ID
options = { :basic_auth => @auth }
response = self.class.get("/workflows/#{id}", options)
return response
end
def executeWorkflow(id,inputxml)
options = { :basic_auth => @auth, :body => inputxml}
response = self.class.post("/workflows/#{id}/executions/", options)
return response
end
end
username = "vcoadmin"
password = "vcoadmin"
vco = Vco.new(username,password)
# Example of get Workflow and output the name from the response
getWfl = vco.getWorkflow("3572df5e-f1ba-4ac4-b7f6-ea3b2cf4bc60")
puts getWfl["name"]
# Example of Execute Workflow using an XML string - This example is a simple Workflow with 1 input parameter. This parameter
# is called 'name' and is a string. The workflow basically outputs "Hello" + name to the log. This is used for testing the
# API. For more details on the XML request look at the API Docs at http://<vco-server-url>:8281/api/docs and look for
# Workflow Run Service
xmlstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><vco:execution-context xmlns:vco=\"http://www.vmware.com/vco\" xmlns=\"vco\"><vco:parameters><vco:parameter name=\"name\" type=\"string\" description=\"\" scope=\"local\"><vco:string>HTTPARTY</vco:string></vco:parameter></vco:parameters></vco:execution-context>"
execWfl = vco.executeWorkflow("3572df5e-f1ba-4ac4-b7f6-ea3b2cf4bc60", xmlstring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment