Creating actions

In this recipe, we will take a look at actions and their differences to workflows as well as their creation and usage.

Getting ready

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.

How to do it...

We will split this recipe into two sections, the creation and the implementation of an action.

Creating a new action

  1. In the Orchestrator Client, click on Actions (the gray gear icon).
  2. Right-click on the top-level (the orange icon) and select New module.
  3. Give the module a name that is based on either your URL or the type of work you intend to do with it. For example, I chose com.packtpub.Orchestrator-Cookbook.
  4. Right-click on the module you have created and select Add-action.
  5. The name should be descriptive and tell a user directly what it does. For this example, I chose the name getElementFromArray.
  6. Click on Add Parameter (the yellow right arrow icon) and add the following variables:

    Name

    Type

    Values

    number

    Number

    This is the index number of the array that should be returned. The first element in an array has index 0.

    array

    Array

    This is the array that contains all elements

  7. Now, click on void directly to the right of Return type and select String.
  8. In the scripting field, enter the following code:
          return array[number]; 
    
  9. Click on Save and Close.

    Creating a new action

Implementing an action into a workflow

  1. Using the Orchestrator Client, create a new workflow.
  2. Drag Action element (out of Generic) onto the schema. In the Choose Action Dialog field, enter the beginning of the name of the action you have just created. As you type, you will see the list of objects to choose from decreases. Alternatively, you can also use the All Actions section and browse through the existing actions.
  3. Add the getElementFromArray action to the schema.
  4. Create and assign the following variables to the action:

    Name

    Type

    Variable type

    number

    IN

    This is a number.

    weekDays

    Attribute

    This is an array of string values such as Mon, Tue, Wed, Thu, Fri, Sat, Sun.

    output

    Out

    This is a string value.

  5. Click on Save and Close to save and close the workflow and run the workflow.

When you now enter a number during workflow execution, the output will be one of the days of the week.

How it works...

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 also

See the example workflow 06.03 Creating actions and the getElementFromArray action.

See Changing elements in a workflow in Chapter 4, Programming Skills.

..................Content has been hidden....................

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