Scripting with decisions

In this workflow, we will see how decisions can be implemented into scripts. You will learn how to create basic and custom decisions.

Getting ready

We just need a working Orchestrator, and you will need the rights to create new workflows and run them. We will work with the Orchestrator Client.

We need a new workflow where we can add a decision. You also should know how to work with logs.

For the Decision activity element, we will be using the example workflow 00.00 BasicWorkflow, which is stored in the Basic Helper folder.

How to do it...

There are three decisions that can be used in Orchestrator; we will discuss them in the following sections.

Basic decision

The Basic decision lets you check a single variable against a condition. A condition is always something that is either true or false. For example, the condition 5 > 3 is true, whereas the condition Team contains i  is false.

Basic decision

  1. Create a new workflow and define an in-parameter of the String type.
  2. Assemble the structure as seen in the previous figure. You will need to rearrange the lines of the workflow.
  3. Have the System log element write something such as True (green line) or False (red dashed line) into the logs.
  4. Edit the Decision element and click on Decision.
  5. Choose a condition and set a value as seen in the previous figure. When done, click on Ok.
  6. Save and run the workflow.

What happens is that the workflow will check whether the value entered fulfills the condition you have specified and then will fork to either the true or false path.

Try this out for several other variable types. Each variable type has other conditions with it. For example, the VC:VirtualMachine type has not only the name of a VM, but also its state (power).

Custom decisions

A Custom decision enables you to check a single variable or multiple variables against complex conditions using JavaScript code.

Custom decisions

  1. Create a new workflow (or reuse the last one) and define two in-parameters:

    Name

    Type

    Section

    Use

    varString

    String

    IN

    This contains a word

    varNumber

    Number

    IN

    This contains the length of the word

  2. Assemble the structure as seen in the previous screenshot.
  3. Have the System log element write something such as true (green line) or false (red dashed line) into the logs.
  4. Edit the Custom decision element and bind both in-parameter to IN.
  5. Click on Scripting and enter the following script (also see the How it works... section of this recipe):
          if (varString.length == varNumber)  { 
              return true; 
          } else { 
              return false; 
          } 
    
  6. Save and run the workflow.

When the workflow executes the Custom decision, it will compare the entered string's length with the entered number and then will fork to either the true or false path.

Tip

Please note that you have to use the JavaScript return command with either true or false to make this decision element work.

Decision activity

A Decision activity lets you check the output of a workflow against a basic condition.

Decision activity

  1. Create a new workflow (or reuse the last one) and define an in-parameter of the type String.
  2. Assemble the structure as seen in the previous screenshot. When you add the Decision activity element, you will be asked what workflow you want to use with it. For simplicity, we will use the 00.00 BasicWorkflow workflow, which is stored in the Basic Helper folder.
  3. Click on IN. You will now see all the in-parameters of the workflow you have selected. Bind the workflow input to the in-parameter you defined in step 1.
  4. Click on Decision, choose one of the output variables of the workflow you selected, and then choose a basic condition you would like to test the output against. Click on OK when finished.
  5. Have the System log element write something like true (green line) or false (red dashed line) into the logs.
  6. Save and run the workflow.

When you run the workflow, the in-parameter you defined will be forwarded to the basic workflow and then the output of the workflow will be checked against the condition you have defined.

The Switch element

This element introduces multiple choices. Just drag a blue arrow from the Switch element to the case element and then enter a basic condition:

The Switch element

  1. Create a new workflow (or reuse the last one) and define an in-parameter of the type String.
  2. Drag the Switch element onto the schema and edit the element.
  3. Click the green plus sign twice, each time a new end element will be created.
  4. Assign the in-parameter to the new branches and define a Basic decision, as seen previously.
  5. Have the System log elements write something such as one, two into the logs.
  6. Save and run the workflow.

How it works...

Decisions are a commonly-used tool in programming. Each of the three decision types lets you fork your workflow into different areas.

A Basic decision is in itself easy to use and powerful, as it doesn't require you to use any JavaScript. The previous example showed you which conditions are possible for the type String, but each variable type comes with its own pool of conditions.

A Custom decision is useful if your decision depends on things that the Basic decision doesn't cover or more than one variable is needed to make a decision. It requires you to use JavaScript, but you also gain a lot more agility.

A Decision activity checks one output of a workflow against a basic condition. It is commonly used to check whether a workflow produced a certain result. A Decision activity can be substituted by the following schema:

How it works...

The major difference is that you won't have to use an attribute to park the output of the workflow and then use this attribute in the Basic decision. However, a Decision activity is good only for a single variable and a basic condition. If you need a more complex condition or multiple variables, you would need to build the preceding schema and use a Custom decision element.

The Switch element allows you to reduce complex code and use multiple decision trees. Not only can you use the Switch element like the Java version, but you can actually use different variables for each decision. In this respect, it behaves more like an if-else if structure.

Tip

The Switch element and Decision activity have been added in vRO6 and should not be imported into older versions of Orchestrator.

JavaScript - if and else

As we already saw in the Custom decisions section, the JavaScript code for an if statement isn't that difficult. It is made up of operators and the If clause itself. The form looks like this:

Statement

Example

 
if (condition) {

    code block

} else if (condition) {

    code block

} else {

    code block

}

 
if (varString == "test") {

    return true;

} else {

    return false;

}

The condition is made up of a statement that is either true or false. This statement is built using operators. The operators that JavaScript knows are the following:

and

or

not

Equal

Not equal

Smaller

Bigger

&&

||

!

==

!=

< <=

>= >

As an example, if you want to know whether the number that is stored in variable A is bigger than 5, the conditional statement will be (A > 5). If you want to know whether the string stored in the variable Text equals Hello, the statement will be (Text == "Hello").

You can glue conditions together with the && (and) and || (or) operators and normal breaks (). For example, if A is bigger than 5 and Text is Hello, the conditional statement will look like this: ((A>5) && (Text == "Hello")).

JavaScript - Switch

The Switch statement in JavaScript looks like this:

Statement

Example

 
Switch (expression) {

case condition:

code block

break;

default:

default code block

}

 
Switch (GuestOS) {

case "RedHat":

SCSI="LSi Logic";

break;

case "Win2008":

SCSI="Paravirtual";

break;

default:

throw "Unknown OS";

}

In expression, you fill in the variable you would like to check, and in the case condition: part, you fill in the condition you want to check against. Please note that you can only check the equals (==) condition with the Switch statement. The default: part is used if all other tests fail.

See also

Also, see the example workflows:

  • 05.02.1 Basic Decision
  • 05.02.2 Custom decision
  • 05.02.3 Decision activity
  • 05.02.4 Switch
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset