Skip to content

Building input forms

Every input you declare turns into a field on the run form. Left alone, the form is a plain list of names and text boxes in the order you declared them. That’s fine for you, but it’s not fine for the people who’ll run your workflow from the Orchestrator Client or from a VCF Automation catalog. They need labels, sensible defaults, choices instead of free text, and a clear message when they get something wrong, before the run starts.

This recipe builds Report VMs, a read-only report with five inputs, and shapes its form on the Input Form tab: two tabs, a drop-down filled by an action, a fixed list of choices, a field that only appears when it’s needed, and a validation action that catches a bad regular expression before the run starts.

Getting ready

  • The action com.vcfguide.chapter3/findVmsByName from recipe 3.2.
  • The folder VCF Orchestrator Guide/Chapter 3 and the action module com.vcfguide.chapter3.
  • If you want to see the form in VCF Automation, a project with the workflow published to the catalog. That’s covered in Chapter 8. Everything here works from the Orchestrator Client alone.

How to do it

Write two actions for the form

  1. In Library > Actions, create the action listVcenterNames in the module com.vcfguide.chapter3. Give it no inputs and the return type Array/string:
com.vcfguide.chapter3/listVcenterNames
// Returns the host name of every vCenter registered in the vSphere plug-in, sorted.
// id is the host name, such as vc01.vmw.lab; name is the full SDK URL.
var connections = VcPlugin.allSdkConnections;
var names = [];
for (var i = 0; i < connections.length; i++) {
names.push(connections[i].id);
}
names.sort();
return names;
  1. Create a second action, validateNamePattern, with one input namePattern of type string and the return type string:
com.vcfguide.chapter3/validateNamePattern
// Returns an error message when namePattern is not a valid regular expression,
// and null when it is. An empty pattern is left to the Required constraint.
if (namePattern === null || namePattern === "") {
return null;
}
try {
var test = new RegExp(namePattern);
} catch (e) {
return "'" + namePattern + "' is not a valid regular expression: " + e.message;
}
return null;

Use id, not name. On 9.1.1, a vCenter connection’s name is the full SDK address, such as https://vc01.vmw.lab:443/sdk, while its id is the host name, vc01.vmw.lab, which is what people expect to pick. The same goes for vm.sdkConnection in step 4.

Create the workflow

  1. Create the workflow Report VMs in VCF Orchestrator Guide/Chapter 3 with these inputs and one output:
Name Type Direction
vcenterName string Input
namePattern string Input
powerState string Input
limitResults boolean Input
maxResults number Input
vmNames Array/string Output
  1. On the Schema tab, add one scriptable task called Find matching VMs, with all five inputs in and vmNames out:
Report VMs — Find matching VMs
var candidates = System.getModule("com.vcfguide.chapter3").findVmsByName(namePattern);
vmNames = [];
for (var i = 0; i < candidates.length; i++) {
var vm = candidates[i];
if (vm.sdkConnection.id !== vcenterName) {
continue;
}
var state = vm.runtime.powerState.value;
if (powerState !== "any" && state !== powerState) {
continue;
}
vmNames.push(vm.name + " (" + state + ")");
// maxResults only counts when the person ticked "Limit the results"
if (limitResults && maxResults > 0 && vmNames.length >= maxResults) {
System.log("Stopped at " + maxResults + " results");
break;
}
}
System.log(vmNames.length + " VMs in " + vcenterName + " match");
  1. Click Save, then Run. The form lists the five inputs by name, as plain boxes in the order you declared them. Close it without running. That’s the starting point.

Lay out the form

  1. Open the Input Form tab. It has two sub-tabs, Request and Validations. On Request, the designer has three parts: Workflow Inputs and Generic Elements on the left, the form canvas in the middle, and the Properties of the selected field on the right. The first form tab is called General, and every input sits on it, labelled with its description.
  2. Rename the first tab of the form What to find. Click Add tab and call the new one Options. Drag limitResults and then maxResults onto the Options tab. Leave the other three on What to find, in the order vcenterName, namePattern, powerState.
Input Form tab of Report VMs, Request sub-tab: Workflow Inputs on the left, each marked Added, and Generic Elements below; the canvas with the form tabs What to find (selected) and Options and an Add tab link, holding vCenter (drop-down), VM name pattern and Power state (drop-down); Properties of vCenter on the right: Appearance tab, Label vCenter, Data type String, Display type DropDown, Visibility Yes, Read-only No, Custom help.
Figure 3.18. The Input Form tab, with the form split over two tabs.

Fill a drop-down from an action

  1. Select vcenterName. On the Appearance tab, under Label and type, set Label to vCenter and Display type to DropDown.
  2. On the Values tab, open Value options, set Value source to Orchestrator action, and under Select action choose com.vcfguide.chapter3/listVcenterNames.
  3. On the Constraints tab, turn on Required.
Properties of vCenter, Values tab: Default value Constant (collapsed); Value options expanded with Value source Orchestrator action and Select action com.vcfguide.chapter3/listVcenterNames.
Figure 3.19. A drop-down whose choices come from an action.

Offer a fixed list of choices

  1. Select powerState. Set Label to Power state and Display type to DropDown.
  2. On the Values tab, leave Value options on Constant and enter four values: any, poweredOn, poweredOff, and suspended. Set Default value to any.
Needs a lab check. Check how constant value options are entered on 9.1.1: one per line, comma-separated, or with a separate label for each value.

Guide free text with a default, help, and constraints

  1. Select namePattern. Set Label to VM name pattern and add Custom help: A regular expression, matched without regard to case. Use .* for every VM.
  2. On the Values tab, set Default value to .*. On the Constraints tab, turn on Required.

Show a field only when it’s needed

  1. Select limitResults. Set Label to Limit the results, and check that Display type is Checkbox, the usual display for a boolean.
  2. Select maxResults. Set Label to Maximum number of VMs. On the Appearance tab, open Visibility and set Value source to Conditional value. An expression appears: set Set value to Yes, and in the If row pick limitResults, choose the comparison for “equals”, and enter true. Add expression adds another rule.
  3. On the Values tab, set Default value to 50 and Step to 10. On the Constraints tab, set Minimum value to 1 and Maximum value to 500.

Hiding a field doesn’t empty it, so maxResults may still arrive with its default when the box isn’t ticked. That’s why the script in step 4 checks limitResults before it uses maxResults.

Options tab of the Report VMs form designer with Limit the results and Maximum number of VMs. Properties of Maximum number of VMs, Appearance tab: Visibility set to Conditional value, Value source Conditional value, Add expression, and one expression with Set value Yes and an If row of three boxes: the field, the comparison and the value.
Figure 3.20. A field that only appears when a checkbox is ticked.
Needs a lab check. Walk steps 15–17 on 9.1.1. Confirm the display type name Checkbox, how a conditional visibility rule is built (the condition editor and its operators), and whether a hidden field still submits its default value.

Check the whole form before the run starts

  1. Open the Validations sub-tab of the input form. Drag an Orchestrator validation from Validation Types onto it, enter the label Name pattern is a valid regular expression, and choose the action validateNamePattern.
  2. Click Save.
Validations sub-tab of the Report VMs input form: Validation Types on the left with Orchestrator validation; on the canvas the card Name pattern is a valid regular expression, Type Orchestrator validation, Action com.vcfguide.chapter3/validateNamePattern, Highlight VM name pattern.
Figure 3.21. A validation action that checks the form before it's submitted.

Try it

  1. Click Run. The form opens on What to find. vCenter is a drop-down listing vc01.vmw.lab, VM name pattern already holds .*, and Power state shows any. On Options, Maximum number of VMs is hidden until you tick Limit the results.
  2. Enter (vc as the pattern and click Run. The form refuses, and shows the message from validateNamePattern. Change the pattern to ^vc and run it again.
Run page of Report VMs: a red banner reads 1 error found, Correct the highlighted errors; the tab What to find has a red mark; vCenter vc01.example.com, VM name pattern (vc underlined in red with the message '(vc' is not a valid regular expression: Unterminated parenthetical, Power state any.
Figure 3.22. The finished form, refusing an invalid pattern.
Needs a lab check. Checked on 9.1.1: the sub-tabs Request and Validations, the default tab General, Add tab, DropDown, the value sources Constant, Conditional value, Orchestrator action and Bind field, Custom help, and Orchestrator validation. Still to check: the comparison names in the If row of a conditional value (step 16), Step, Minimum value and Maximum value. The validation message appears under the field it highlights, with a 1 error found banner and a red mark on the tab (figure 3.22).

How it works

The form isn’t part of the schema. It’s a separate layer, stored with the workflow, that describes how each input is presented and checked. Only workflow inputs can appear on it. Variables never do. The designer also offers generic elements, such as text, drop-downs, and password boxes, that don’t match an input. You can bind them to inputs or use them for the form’s own logic.

Each input’s properties are split over three tabs:

  • Appearance: how the field looks and when it’s shown. Label, Display type, Visibility, Read-only, and Custom help.
  • Values: what it holds before the person touches it, and what they can choose from. Default value, Value options, and Step.
  • Constraints: what counts as a valid value. Required, Regular expression, Minimum value and Maximum value (a number’s value, or a string’s length), and Match text box, which makes a field equal another, as for a password typed twice.

Most properties can take their value from more than one source. Constant is a fixed value. Orchestrator action calls an action, and the designer only offers actions whose return type fits the field. Bind field copies another field. Conditional value picks a value from rules on other fields, as maxResults does for its visibility. Computed value builds a value with simple operators. Computed values aren’t available for plug-in types such as AD:UserGroup.

An action called by the form runs on the Orchestrator server, while the form is open. When its inputs are bound to other fields, it runs again whenever one of them changes, but only once all the fields it depends on hold valid values. That’s how cascading drop-downs work: choose a vCenter, and a second drop-down bound to vcenterName refreshes with that vCenter’s clusters.

A validation on the Validations tab is different. It runs when the person submits the form, and it checks the form as a whole. Orchestrator passes it the fields whose names match its inputs, which is why validateNamePattern’s input must be called namePattern. If it returns a message, the form shows it and refuses to submit. If it returns nothing, the form goes through.

Back in vRO 5.5–7Lay out the run dialog on the workflow's Presentation tab in the Java client, with display groups, input steps, and properties such as mandatory and Predefined answers, often written as OGNL expressions.
In Orchestrator 9.xDesign the form on the Input Form tab, with tabs, a drag-and-drop canvas, and properties grouped as Appearance, Values, and Constraints. Logic is written as actions, not expressions.

There's more

Value options or default value? A drop-down’s choices come from Value options. A common mistake is to put the action under Default value instead, which leaves the drop-down empty. Broadcom KB 428466 describes exactly that.

Labels and values. To show one thing and submit another, such as a cluster’s name while the workflow receives its ID, have the action return Array/Properties, each with a label key for the text shown and a value key for the value submitted.

Needs a lab check. Confirm that the 9.1.1 Orchestrator input form accepts Array/Properties with label and value keys for a string drop-down. The behaviour is documented for VCF Automation custom forms and confirmed in a Broadcom KB, but not on an Orchestrator page.

Keep form actions fast. The person waits while each action runs, and a chain of dependent actions adds up. Keep them to a few seconds. Avoid marking a field Required only because an action uses it, and check inputs for null in the action: an empty field bound to an action input is passed as null, not as an empty string.

Return an empty choice in dependent lists. When a dependent list’s action returns a single value, the form can keep the old selection after the field it depends on changes. Broadcom KB 394722 calls this expected behaviour and suggests returning an extra empty value.

Regular expressions must suit Java and JavaScript. A Regular expression constraint must be valid in both syntaxes. A pattern that only one accepts makes the form show “Some data cannot be retrieved” (Broadcom KB 314888).

Forms in VCF Automation. When you publish a workflow to the VCF Automation catalog, its form comes along, and you can refine it there with VCF Automation’s custom form designer. In VCF Automation 9.0.x, Broadcom KB 450158 reports that a Multi value picker fed by an action fails in the catalog with “Error getting external data 403”. Use a DropDown or Dual List until it’s fixed. Chapter 8 covers publishing.

Needs a lab check. Confirm in VCF Automation 9.1 that a catalog item made from Report VMs starts with this form, and whether KB 450158 still applies.

Don’t trust the form alone. A REST client or a parent workflow can run Report VMs without ever seeing the form. If an input must never be wrong, check it in the workflow as well, as recipe 3.4 does.

See also