In this recipe, we will take a look at actions and their differences to workflows as well as their creation and usage.
We just need a working Orchestrator and you will need the rights to create new workflows and actions as well as the right to run workflows. We will work with the Orchestrator Client.
JavaScript arrays will be used, so you should read the introduction to this chapter.
We will split this recipe into two sections, the creation and the implementation of an action.
com.packtpub.Orchestrator-Cookbook
.getElementFromArray
.
Name |
Type |
Values |
|
Number |
This is the index number of the array that should be returned. The first element in an array has index 0. |
|
Array |
This is the array that contains all elements |
String
.return array[number];
getElementFromArray
action to the schema.
Name |
Type |
Variable type |
|
IN |
This is a number. |
|
Attribute |
This is an array of string values such as Mon, Tue, Wed, Thu, Fri, Sat, Sun. |
|
Out |
This is a string value. |
When you now enter a number during workflow execution, the output will be one of the days of the week.
Actions are what programmers would call functions. There are multiple differences between a workflow and an action; the main difference is that an action can only return one variable, whereas a workflow can return multiple variables. Another is that actions are purely JavaScript-based and do not contain any visual programming. Actions can still call other actions; however, you will need the JavaScript command System.getModule([Module]).[Action]([in-parameter])
to call them. As you can see, an action is called using its module name, while a workflow is called (for example, via the API) using its ID. This is a rather important difference, as renaming an action is hard because its name, and maybe the module name, must be changed everywhere.
In an action, 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 using the JavaScript return
command. If you don't want or need any return code, define the return code as void
.
Binding an action into a workflow can be done just as you would integrate any workflow, by dragging the Action element onto the schema. When binding the out-parameter of an action, you will notice that the name that is displayed in the Action element is ActionResult
. Your attribute or out-parameter that is bound to the return value of the Action element should be named something more meaningful.
Another thing that is important for good programming is the name of the action and the module you place it in. Browse through the existing action modules to explore how other programmers have done it.
A good recommendation is to start the name 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). 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
.
Exploring the existing action library, you will find a lot of useful actions that are pre-created and can be used in your own workflows.
See the example workflow 06.03 Creating actions
and the getElementFromArray
action.
See Changing elements in a workflow in Chapter 4, Programming Skills.