In this workflow, we will see how decisions can be implemented into scripts. You will learn how to create basic and custom decisions.
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.
There are three decisions that can be used in Orchestrator; we will discuss them in the following sections.
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.
String
type.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).
A Custom decision enables you to check a single variable or multiple variables against complex conditions using JavaScript code.
Name |
Type |
Section |
Use |
|
String |
IN |
This contains a word |
|
Number |
IN |
This contains the length of the word |
if (varString.length == varNumber) { return true; } else { return false; }
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.
A Decision activity lets you check the output of a workflow against a basic condition.
String
.00.00 BasicWorkflow
workflow, which is stored in the Basic Helper
folder.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.
This element introduces multiple choices. Just drag a blue arrow from the Switch element to the case element and then enter a basic condition:
String
.one
, two
into the logs.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:
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.
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"))
.
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.