Actions are what programmers call functions. It is a piece of code that you intend to reuse often in different programs.
There are multiple differences between a workflow and an action. The main difference is that an action can return only one variable, whereas a workflow can return multiple variables. Another difference is that actions are purely JavaScript-based and do not contain any visual programming.
In an action, the in-parameters are defined the same way as in a workflow. However, the return type is a bit different. The return code is always one variable and its value is assigned by using the JavaScript return
command. If you don't want or need any return code, define the return code as void
.
A good naming convention for actions is to start the name of the action with a verb, such as get, set, create, delete, and so forth. Then, describe what the action is doing. A good way to make the name more readable is to capitalize each word (except the first word). Examples of good naming are startVM, removeAllSnapshots, and getAllVMsOfVApps. If you need more information on this, check the JavaScript style guide at http://javascript.crockford.com/code.html.
While exploring the existing action library, you will find a lot of useful pre-created actions that can be used in your own workflows.
We will now create a new action that will be used in our InstallFreshVM workflow. This action will enable us to use a more user-friendly operating system.
An action module is like a workflow folder; except that you can't create submodules.
com.packtpub.Orchestrator-essentials
.A new action module has been created.
Now that we have an action module, we will create an action in it by performing the following steps:
Name |
Type |
Description |
---|---|---|
|
|
A "pretty" name for an operating system, such as "Windows 7" |
VC:VirtualMachineGuestOsIdentifier
.if (prettyOS == "Windows 7") { return VcVirtualMachineGuestOsIdentifier.windows7_64Guest; } else if (prettyOS =="SLES 11") { return VcVirtualMachineGuestOsIdentifier.sles11_64Guest; }
After creating our first action, we're going to use it in our deploy workflow, as follows:
In the next chapter, we will improve this workflow further.
Here is a short list of things that you may like to try on your own:
Instead of using the if-clause in the action, check out the JavaScript switch
statement.
The switch
statement in JavaScript looks like this:
Statement |
Example |
---|---|
Switch (expression) { case condition: code block break; default: default code block } | Switch (prettyOS) { case "Windows 7": return VcVirtualMachineGuestOsIdentifier.windows7_64Guest; break; case "SLES 11": return VcVirtualMachineGuestOsIdentifier.sles11_64Guest; break; default: throw "Unknown OS"; } |
In expression
, fill in the variable that you would like to check, and in the case condition:
part, fill in the condition that 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 the other tests fail.