Making vCO Workflow Wrappers

User Rating: 5 / 5

Star activeStar activeStar activeStar activeStar active
 

A vCO workflow wrapper is a set of workflow tasks that are added at the beginning of a workflow in order to take string values and look up a more complex data type or pre-defining inputs using attributes. You may have done these in the past already in vCO. Some examples would be finding a virtual machine by name or finding an Active Directory user by email address.

If you're running the vCO workflow from within the vCO client or vSphere web client, you probably don't need to worry about this as both of those tools understand the complex data types that come with plugins. On the other hand, if you're calling the vCO workflow from a tool like vCAC, it becomes extremely important to make it so that the vCO workflows only require strings as inputs--meaning that you need to add a vCO workflow wrapper to convert these string values to their more complex data types.

There are two ways to create this wrapper functionality: add a pre-built task or workflow that came with the plugin, or to pull back all objects of that type and then pick through the array where some conditions match.

Let's take a look at this further.

A typical use case might be that you want to tell vCO to clone a virtual machine. To do this, we can use the vCenter plug in and the built in workflow called "Clone Virtual Machine, No Customization". Below is a look at all the inputs and data types that this workflow is expecting to be provided with:

 

You'll note that we have some complex data types. One of which is the field called "vm" which is a type of VC:VirutalMachine. This is a complex data type for the vCenter plugin (as denoted by the VC) for the object of virtual machines.

First we should figure out if there's something already built to help us find a virtual machine by its name with the output being a VC:VirtualMachine:

1. Open the workflow you want to change and press edit. If you're using an out of the box one, it's very recommended (and often required) to make a duplicate of the workflow in order to edit it.  alt
2. Go into the schema tab and at the top left corner of the workflow you'll see a search field  
3. I usually enter in what kind of object it is I'm looking for. Here I searched for "virtual machine". There is no science to this part. I simply know that I want a workflow that find the complex VC:VirtualMachine type by a string that I provide to it--probably the machine name alt
4. After some perusing the list of results, I find a workflow called "Get Virtual Machine by Name". Hey, that sounds about right! alt
5. Drag and drop that task into the beginning of your workflow  
6. Go to the inputs tab, select the "vm" parameter and click the button that says "Move as attribute". alt
7. Still in the inputs screen, click the "Add Parameter" button. Rename the new parameter something descriptive like "vmName" and leave it as a string type. alt
8. Switch over to the General tab and find the "vm" parameter that we just moved to be an attribute. Because the "Get Virtual Machine from name" task outputs the virtual machine into an array of virtual machines (for potential multiple results), we need to tweak the type of our attribute so that it will be able to match up to the output of the "Get Virtual Machine from name" task. Click on the VC:VirtualMachine type associated to the "vm" parameter and a box will pop up. At the top left of the box, use the radio button to set the type to "Array of" and click "Accept". alt
9. Go back to the schema tab, and click the edit button on the "Get Virtual Machine" task.  
10. Go to the Visual Binding tab and link the input of "vmName" to the search criteria in the task, and the output of "vms" to the "vm" attribute. alt
11. Close that task screen. Congrats! you have just created a vCO workflow wrapper for the VC:VirtualMachine type. Your vCO workflow will now accept a string name of the virtual machine, find the complex object of VC:VirtualMachine, and pass it to the later "CloneVM" task, where it's needed.

OK, so now let's take a more complex issue of creating a wrapper for a object type that doesn't have a handy piece of code already built for you. We can still use this copy of the "Clone Virtual Machine, no customization" workflow from the above instructions to demonstrate how we do this.

The rough process is that we're going to create a scriptable task, have it pull all of the objects of that type into an array, and then compare the sting input from the user with a value on the object (usually input string = object name). 

1. You should still be in the workflow from above if you're following along, if not open the workflow you want to change and press edit. If you're using an out of the box one, it's very recommended (and often required) to make a duplicate of the workflow in order to edit it.  
2. We're going to now create a wrapper for the datastore input value. We'll start in the inputs tab, because we know that there's no helpful bits for looking up. Select the "datastore" parameter and press the button to "Move as Attribute" alt
3. Create a new string input parameter in this screen and name it something descriptive. I named mine "stringDatastore" alt
4. Go into the schema tab and at the top left corner of the workflow and, if you're not already, go to the Generic tab in the toolbar to the right.  
5. Drag a "Scriptable Task" task into the workflow. It doesn't really matter where it goes as long as it's before the task that needs it--so anywhere before the "Clone VM" activity. alt
6. Press edit on the Scriptable Task and do some housekeeping by giving it a descriptive task name in the Info tab's "Name" field alt
7. Go to the "In" tab, and press the "Bind to workflow parameter/attribute". Select your string input that we'll be using as our lookup criteria. alt
8. Go to the "Out" tab, and press the "Bind to workflow parameter/attribute". Select your complex data type that you want to end up with and store in an attribute to be used later in the workflow. alt
9. Here comes the fun part. Go to the "Scripting" tab and click on the "Search API" button. In the resulting search field, put in what you're looking for--in this case, it's "datastore". Make it easier on yourself by filtering to only show attributes and methods. Immediately I see a few methods that say they return all datastores in the description. *TIP* I find it easiest to call the VcPlugin methods. Use these ones when you can. We're in luck, there's a VcPlugin method to "getAllDatastores". alt
10. Click the "Go to selection" button, and then the "Close" button. It will land you back on the scripting screen with the method highlighted. You call call it using standard JavaScript addressing. This would be "VcPlugin.getAllDatastores()". alt
11. Now we have to confirm the field on the datastore object that we'll be comparing our input string against. Again, click the "Search API" button. Search for "datastore" again, but this time filter on "Types & enumerations". You'll see "VC/VC:Datastores" so click "Go to selection" then "Close" to get back to the scripting screen. alt
12. On the left side of the screen, it now gives me details about the object type. It tells me that there is a "name" field on this object, so I will be able to compare my input string to the object's "name" property. alt

13. Here comes the JavaScript. Basically all we're doing here is creating a variable array to catch all the datastores. We then count how many there are in the array, then compare each one's name, one at a time, to the string until we get a match. When we get a match, we're going to assign it to our output variable. This code snippet comes in handy:

var datastores = VcPlugin.getAllDatastores();
var arraylength = datastores.length;
var i = 0;
while (i < arraylength){
if (datastores[i].name == stringDatastore){
    datastore = datastores[i];
    }
i++;
}



alt

14. Close this scripting task. Congratulations! You have now created a wrapper to lookup a complex object type from its name without any help from pre-created workflows.

Now that you know how to build a wrapper for your workflows you can call them from tools like VMware's vCloud Automation Center (vCAC). For more information on how to make calls to vCO from vCAC, please visit vCACteam.info

Comments  

0 #4 burke 2014-03-21 14:47
Quoting artur:
hi, thanks for tutorial however when I try to execute script I'm getting
TypeError: Cannot read property "0" from undefined (Dynamic Script Module name : cloneVM#8)

any ideas ?

thanks
Artur

This typically indicates you missed a required binding for the workflow you are wrapping.. make sure that the inputs for your sub-workflow are all bound to valid values. If further assistance is required, please move this to the Orchestrator Communities here: ~objecttype~objecttype[thread]]Orchestrator Community
0 #3 artur 2014-03-21 14:36
hi, thanks for tutorial however when I try to execute script I'm getting
TypeError: Cannot read property "0" from undefined (Dynamic Script Module name : cloneVM#8)

any ideas ?

thanks
Artur
0 #2 burke 2014-02-17 13:31
Demhyt:

In your Scriptable task, you should build a new array of names from the objects... For example:
var vmNames = new Array();
for each (vm in allVMs){
vmNames.push(vm.name);
}

The above assumes your array of VC:VirtualMachi nes is named "allVMs" - replace as needed. When you have done the above loop, pass the vmNames array/String as output. You may now get that array of strings for the output via your SOAP or REST call as needed.
0 #1 demhyt 2014-02-15 07:01
Hi Vcoteam, that's one of great work...but can you help me I want to get all the my vm in datacenter so I create scriptable task and I create php script using soap method...I can pass the output to my php but the output type is VC:VirtualMachi ne, just want to know how to convert the object type to string type?, becuse my output like this,
stdClass Object ( [getWorkflowTok enResultReturn] => stdClass Object ( [name] => vms [type] => Array/Any [value] => #{#VC:VirtualMachine#dunes://service.dunes.ch/CustomSDKObject?id='vcenter.lab.local/vm-9024'&dunesName='VC:VirtualMachine'#;

I just want to simple get the virtual machine names for edit spec purpose...thank s before!

Search