Creating an action

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.

Creating a new action module

An action module is like a workflow folder; except that you can't create submodules.

  1. In the Orchestrator Client, make sure that you are using the Design mode.
  2. Click on Actions, Creating a new action module.
  3. Right-click on the top level Creating a new action module, and select New module.
  4. Give the module a name that is based on either your URL or the type of work that you intend to do with it. For example, I chose com.packtpub.Orchestrator-essentials.

A new action module has been created.

Creating a new action

Now that we have an action module, we will create an action in it by performing the following steps:

  1. Right-click on the action module that you created and select Add-action.
  2. The name should be descriptive and tell a user directly what it does. For this example, I chose the name as translateOS2GuestOS, as this action will translate for us a more descriptive OS name to the more cryptic Orchestrator type that we have been using so far.
  3. Click on Scripting.
  4. Now, click on Add Parameter and add the following variables:

    Name

    Type

    Description

    prettyOS

    String

    A "pretty" name for an operating system, such as "Windows 7"

  5. Now, click on void, which is directly to the right of Return type, and select VC:VirtualMachineGuestOsIdentifier.
  6. In the scripting field, enter the following code:
    if (prettyOS == "Windows 7") {
      return VcVirtualMachineGuestOsIdentifier.windows7_64Guest;
    } else if (prettyOS =="SLES 11") {
      return VcVirtualMachineGuestOsIdentifier.sles11_64Guest;
    }
  7. Click on Save and close.
    Creating a new action

Implementing the action into a workflow

After creating our first action, we're going to use it in our deploy workflow, as follows:

  1. By using the Orchestrator Client, make a backup copy or increase the version of your InstallFreshVM workflow.
  2. Open the workflow for editing.
  3. Move the vmGuestOS INPUT-parameter to be an attribute.
  4. Create a new vmGuestOSname INPUT-parameter.
  5. Reassign all the vmGuestOS parameters in all the workflow elements (you can use validation to make it easier to find them all).
  6. Drag the Action element from the Generic toolbox into the workflow somewhere before the Create simple virtual machine workflow element.
  7. In the Choose action menu, search and select the action that we have just created.
  8. Assign the IN-parameter to vmGuestOsName and prettyOsName and the actionResult OUT-parameter to the vmGuestOS attribute.
    Implementing the action into a workflow
  9. Run the workflow, but now type in the "pretty" name that we defined in the input mask.

In the next chapter, we will improve this workflow further.

Things you might like to try

Here is a short list of things that you may like to try on your own:

Switch case

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.

String clean-up

Use the methods that were showcased in the Common string problems and solutions section and implement them in the action to create a better user experience.

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

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