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/perluse 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
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!
Burke has been a technology professional since 1996 and has held certifications from the CNCF, Cisco, Citrix, ITIL, Linux Professional Institute, Microsoft, Novell, and VMware. He joined VMware in 2007 as part of the acquisition of Dunes Technologies from Lausanne, Switzerland where he had begun his work with Orchestrator. Burke is founder and contributor of the vCOTeam.info blog as well as a leading contributor to the VMTN Communities for Orchestrator. He has been recognized by the community as a vExpert since 2010. During his tenure at VMware, Burke co-created the Orchestrator Masters Training program, has trained hundreds of employees on Orchestrator, built many integrations for customers and partners, and has worked various roles in the VMworld Hands On Labs. He has also been a member, and technical lead, of the SDDC Livefire program, building content, labs, and vPods for the program. Publications include contributing author for ‘VMware vCloud Architecture Toolkit (vCAT)’ (VMware Press 2013) and technical resource for ‘VMware vRealize Orchestrator Cookbook - Second Edition’, ‘VMware vRealize Orchestrator Cookbook, and VMware vRealize Orchestrator Essentials (Packt Publishing 2015), Automating vSphere with VMware vCenter Orchestrator (VMware Press 2012), and VMware vSphere for Dummies (For Dummies 2011).