In a previous article, I taught you how to explore and use the REST API to start a Workflow using a generic browser based REST Client. In this article, I will provide a perl based example of running the "Create a Record" workflow that was created in Part 2 of my SQL Plug-in Dynamic Types Simple CMDB for vCAC article. I have barely more experience with perl than Python so this will be another very short article! You may download the script in this article from my vroClientScripts Repository on GitHub.

 

The Script

The following code should be saved as something like runWorkflow.pl - I tried to maintain similar structure as my Python article:

#!/usr/bin/perl
use REST::Client;
use MIME::Base64;
use File::Slurp;

# Update for your environment here. Use double quotes around each defined value.
$usr = {REPLACE WITH YOUR VRO USERNAME};
$pwd = {REPLACE WITH YOUR VRO PASSWORD};
$wfid = {REPLACE WITH YOUR VRO WORKFLOW ID};
$jsonFile = read_file({REPLACE WITH PATH TO JSON BODY FILE}); // updated as per comment at end of article
$vroServer = {REPLACE WITH VRO URL:PORT};

###### Make no changes below this line ##########
# Disable server verification (for older LWP implementations)
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;

# Setup the connection:
my $client = REST::Client->new();
# Disable server verification (for newer LWP implementations)
$client->getUseragent()->ssl_opts( SSL_verify_mode => SSL_VERIFY_NONE );

$client->setHost("https://$vroServer/vco/api");
$client->addHeader("Authorization", "Basic ".encode_base64( $usr .':'. $pwd ));
$client->addHeader("Content-Type","application/json");
$client->addHeader("Accept","application/json");

# Perform an HTTP POST on the URI:
$client->POST( "/workflows/$wfid/executions", $jsonFile);
die $client->responseContent() if( $client->responseCode() >= 300 );
print "Response Code: " . $client->responseCode() . "\n";
my @headers = $client->responseHeaders();
foreach (0..$#headers){
    print $headers[$_] . ": " . $client->responseHeader($headers[$_]) . "\n";
}

 


Note: Before attempting to run the script, be sure to modify the parameters at the beginning of the script to reflect YOUR workflow.

I have kept the script as simple as I can, including the option to NOT Verify the SSL Certificate so that self-signed certs do not prevent the script from running.

The json file

While testing this, my example workflow was the "Create a Record" workflow as noted earlier. That workflow had a number of inputs so these were loaded into a json file. In order to learn more about how to get the correct input json for your workflow, please reference my article: How to use the REST API to Start a Workflow

Here's my example json file:

{
"parameters": [
{
"type": "string",
"scope": "local",
"name": "ServerName",
"value": {
"string": {
"value": "vcac-iaas-01c"
}
}
},
{
"type": "string",
"scope": "local",
"name": "ServerIP",
"value": {
"string": {
"value": "192.168.110.89"
}
}
},
{
"type": "string",
"scope": "local",
"name": "ServerDNS",
"value": {
"string": {
"value": "corp.local"
}
}
},
{
"type": "string",
"scope": "local",
"name": "ServerID",
"value": {
"string": {
"value": "vcac-iaas-01d.corp.local"
}
}
},
{
"type": "string",
"scope": "local",
"name": "ServerOwner",
"value": {
"string": {
"value": "administrator"
}
}
}
]
}

 

Example Run

example_run_27cd44547fcdcbf3393751ed6619fcf6.png

I ran the script from my OSX console, here is the output:

sh-3.2# ./runWorkflow.pl
Response Code: 202
Connection: close
Date: Thu, 29 Jan 2015 21:18:53 GMT
Location: https://vco55.vcoteam.lab:8281/vco/api/workflows/776ce656-1a3d-45da-a617-ba756295d96f/executions/ff8080814abac606014b378f9d370487/
Server: Apache-Coyote/1.1
Content-Length: 0
Client-Date: Thu, 29 Jan 2015 21:18:53 GMT
Client-Peer: 192.168.1.22:8281
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=VMware/OU=VMware/CN=vco55.vcoteam.lab
Client-SSL-Cert-Subject: /C=US/O=VMware/OU=VMware/CN=vco55.vcoteam.lab
Client-SSL-Cipher: AES128-SHA
Client-SSL-Socket-Class: IO::Socket::SSL
Client-SSL-Warning: Peer certificate not verified

When I check the Orchestrator client, I can see that the workflow has run and completed successfully (See screenshot above!). Note that the "Location" header is the workflow token - that is what you would check for completion status as well as output parameters.

Help Me Make This Sample Better!

As noted at the beginning of this article, I don't know perl but I managed to get a very simple example script working here. Please submit your comments and/or improvement suggestions to help increase the value and usefulness of this simple script.
Improvements needed:
- Get credentials from a hidden config file
- Pass in all parameters as command line arguments
- Display/parse output
Thanks!