VCF Automation Blog

from Stefan Schnell

Normally actions are integrated into workflows and invoked from there. The way to invoke a workflow from an action is rather unusual and less common. However, this approach offers the advantage of complete embedding in a programming language environment. All the possibilities offered by coding can therefore be used. E.g. for unit tests, this means that quality assurance measures can also be implemented for workflows on this way.

Execute Workflow from Action


This post shows examplary how a workflow can be invoked up from an action. For this purpose we take the workflow 'Get virtual machines by name', which returns a list of virtual machines from all registered vCenter server hosts that match the provided expression. This workflow has the following Inputs/Outputs interface:

vcf automation execute workflow from action - action perspective
That's all we need to know to execute this workflow. The following action shows how to ... A simple sequence that can be used again and again, with the exception of the Inputs and Outputs, which must be adapted to the corresponding workflow.

/**
 * Example to call a workflow from an action. In this case the workflow
 * 'Get virtual machines by name' is called to get all VMs which
 * contains 'ubuntu' in its name.
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * Checked with VMware Aria Automation 8.12.0.
 */

in_criteria = "ubuntu";

// Define folder and name of the workflow
var folderName = "Library/vCenter/Virtual Machine management/Basic";
var workflowName = "Get virtual machines by name";
var workflowId = null;

// Detect workflow and get its ID
workflowCategory = Server.getWorkflowCategoryWithPath(folderName);
var allWorkflows = workflowCategory.allWorkflows;
allWorkflows.forEach( function (workflow){
  if (workflow.name === workflowName && workflowId === null) {
    workflowId = workflow.id;
  }
});

if (workflowId !== null) {

  var workflow = Server.getWorkflowWithId(workflowId);

  // Define the inputs
  var workflowInputs = new Properties();
  workflowInputs.put("criteria", in_criteria);

  // Execute the workflow
  var workflowToken = workflow.execute(workflowInputs);

  // Wait that the workfow terminated
  var workflowTokens = [];
  workflowTokens.push(workflowToken);
  System.getModule("com.vmware.library.vc.basic")
    .waitForCompletionForBatchWorkflow(workflowTokens);

  // Get the outputs
  var workflowOutputs = workflowToken.getOutputParameters();
  var out_vms = workflowOutputs.get("vms");

} else {
  System.warn("Can not find " + folderName + "/" + workflowName);
}

out_vms.forEach(function(vm) {
  System.log(vm.name);
});

It is important that the Inputs and Outputs must be defined with their exact names that they have in the interface definition.

The following image sequence shows the result of executing the 'Get virtual machines by name' workflow with the criteria 'ubuntu' in the HOL.

vcf automation execute workflow from action - action perspective
vcf automation execute workflow from action - workflow runs perspective
vcf automation execute workflow from action - workflow perspective

Conclusion

As we can see, executing workflows from actions is very simple. This opens up many opportunities for us, e.g. as already mentioned, the integration of workflows in unit tests. The detailed logging of the workflows helps us to analyze the results quickly and clearly, to detect errors in this context.