Chapter 6. Advanced vRO Scripting with JavaScript

In the previous chapter, we learned how to combine existing workflows to create new workflows. Before we can go ahead and delve into the advanced aspects of programming, we need to learn some JavaScript. Orchestrator is actually a JavaScript engine that has a massive visual component.

In this chapter, we will not only use an example workflow to learn JavaScript, but also expand the InstallFreshVM workflow that we created in the previous chapter.

This chapter will explore the following topics:

  • JavaScript – the very basics
  • How to use JavaScript in Orchestrator
  • The if-clause in JavaScript as well as operators and conditions
  • Creating and using actions

JavaScript – the very basics

JavaScript is a script language, which means that it doesn't need to be compiled before runtime. The Java language is a "real" language, and it needs to be compiled before it can be executed. Compiling is defined as the creation of an executable (.exe) file from the Java-sourced code. For our purposes, Java and JavaScript have nothing to do with each other. JavaScript is what we will learn, and it's used to create the Orchestrator workflows and action.

To learn JavaScript, you can have a look at http://www.w3schools.com/js/.

Basic rules

Let's have a look at the rules that govern how JavaScript is written. In the next section, we will try some stuff out.

Every line ends with a semicolon

Each line of JavaScript that you write will be interpreted by Orchestrator. For Orchestrator to know that a command has ended, you need to place a semicolon (;) at its end. It's just like a full stop in a normal sentence that marks the end of a thought.

It's like the old "Let's eat, Grandpa" joke, where a missing comma leads to cannibalism ("Let's eat Grandpa").

Variables

Variables (or parameters) in JavaScript are just text and must start with a letter. After the first letter, numbers can be used. There is a convention that you should follow when creating variable names. The first letter should be in lower case. Then, capitalize the first letter of the words that follow. Examples of good variable names are newVM, myNewCoolVariable, and vmConnection.

Variables in JavaScript don't have to be declared, but it's good practice to do so. A declaration means that you have to define the content type of a variable type before using it. To tell Orchestrator that you want to define a variable called output that should contain numbers, you will write the following code:

var myOutput = new Number();
myOutput = 5;

As noted earlier, it's not really needed. You can also write var myOutput = 5, or just myOutput = 5. However, in some cases such as properties or arrays, you need to declare the variable properly.

We have already discussed the different types of variables in the previous chapter. Have a look at it. We will also discuss in a bit more detail some number and string operations via a few examples.

Case sensitivity

Every command or variable is case sensitive, which means that the System.log command is correct, but system.log or System.Log are not.

The same is true for variable names. The myTest variable is not the same as MyTest or mytest.

Orchestrator actually helps you a bit here. We will explore this a bit later.

Comments

Comments are text that are placed in the code and which are not interpreted for execution. Comments are extremely useful when writing your program. You can write in them what a specific line will do or where a variable comes from.

There are two kinds of comments—single-line comments and multi-line comments. You use single-line comments mostly directly after a command line, and they finish automatically with the Enter key. On the other hand, you use multi-line comments either to write longer text, or to comment out multiple script lines. Commenting out script lines means that you put the comment signs around a script line so that it is ignored when the program is run. This is mostly done when testing a program. Make sure that the ; end marker is placed outside the comment directly after the command.

  • Single-line comments are made by using //, as follows:
    System.log(test); //put the content of test into the logs
  • Multiline comments begin with /* and end with */, as follows:
    /* This is a big comment
    that goes 
    over multiple lines */

Formatting

JavaScript has some agreed-upon formatting rules that should be followed. Not following them will still create a working script, but others will find it harder to read. Have a look at http://javascript.crockford.com/code.html to understand how JavaScript should be formatted.

Running though some examples

Let's leave the dry theory behind and have a look at some practical examples.

Creating an example workflow

First, we will create a space where we can write some JavaScript and make Orchestrator use it, as follows:

  1. Open the Orchestrator Client.
  2. Go to the workflow folder that you created in the previous chapter.
  3. Right-click on it and create a new workflow.
  4. From the Generic toolbox, drop a Scriptable task onto the Schema.
    Creating an example workflow

The scriptable task element will contain our JavaScript, and it is the only element that we will need in this whole chapter.

Creating new parameters

Let's start the show with some simple number operations. We will use the basic mathematical operations: +, -, *, and /.

Starting from where we left off, we will now create two input parameters and one output parameter and assign all the parameters to the scriptable task. We will do this in two different ways.

  1. Click on the Scriptable task and select Edit.
  2. Select the IN tab. There are no parameters in it yet because this is a brand-new element. Click on Bind to workflow parameter/attributeCreating new parameters.
  3. The Chooser window will open, with nothing to choose from. Click on Create parameter/attribute in workflow.
  4. Now we can give the new parameter a name.
  5. We choose this parameter to be of type Number as we want to do some math.
  6. Create the parameter as an IN-parameter and then click on Ok.
    Creating new parameters
  7. Let's add an IN-parameter another way. Click on Inputs.
  8. Click on Add parameter. A new line is added.
  9. Click on the new parameter named arg_in_0. A new window will open up. Enter a new name for this parameter, such as mySecondValue, and click on OK.
  10. Now, we need to change the parameter type. Click on the default String type.
  11. In the new window, select the Number type and click on Accept.
    Creating new parameters
  12. Click on Schema and then on the Scriptable task and select Edit.
  13. Only the first parameter has been created. Click on the Bind to workflow parameter attribute Creating new parameters.
  14. The Chooser window will open and show the second parameter that we have created. Choose it and click on Ok.
  15. Use one of the described methods to create an OUT-parameter called myOutput of the Number type.

In this chapter and the one after this, you will be asked to create parameters and attributes. To do this, you should use one of the two methods that we just demonstrated.

Numerical operations

Now that we have two input parameters and one output parameter, we can do some calculations, as follows:

  1. Starting from where we left off, edit the Scriptable task and click on Scripting.
  2. Let me draw your attention to something. In the empty field, start typing myOutput. Did you see that as soon as you finished the word, it turned purple? Cool. Delete the word and type myoutput (no capital O). See how this doesn't turn purple? This is because of the case sensitivity. The parameters myoutput and myOutput are two different variables.

    Tip

    Purple-colored variables indicate that the variable is bound as an input or output of the scriptable task, custom decision, or the action that is currently being edited.

  3. You can also just click on the parameter names to paste the variable into the scripting.
  4. Create the following script:
    myOutput = myFirstValue + mySecondValue;

    Please note that I inserted an extra space to make this line more readable. This extra space is not needed, but it's fine if you keep it.

    Numerical operations
  5. Save and close the workflow and then run it.

    Tip

    If you are getting tired of saving, closing, running, and opening, have a look at Chapter 8, Errors, Logs, and Debug Mode, where we explain the debug modus.

  6. Check the variable output for the results:
    Numerical operations
  7. Go back and edit the workflow. Then try out the mathematical operations: minus (-), multiply (*), and divide (/).

String operations

We just played around with numbers. Let's do the same with strings, as follows:

  1. Starting from where we left off, edit the workflow and click on Inputs.
  2. Change the type of myFirstValue from Number to String.
  3. Save and run the workflow. Can you see the funny result?
  4. Now, set myFirstValue back to Number and mySecondValue to String. Run it.
  5. Now, change all the input and output types to String and run it again.

The results should look like this:

String operations

The results are different because Orchestrator does a bit of autocasting here. What this means is that it adds the String "2.0" to number 3, but because the first variable type is of the type String, Orchestrator decides that the second has to be of the same type too and converts 3 to "3". It then glues both "2.0" and "3" together to form the string "2.03". The output is again of the Number type. So, Orchestrator goes ahead and converts "2.03" into a number as 2.03. Repeat this thought experiment with the second and the last example. This example shows that you need to be a bit careful when combining variable types.

The + sign is used to put two strings together. Let's do this with just strings, as you would do in reality:

  1. Make sure that all the input and output parameters are of the String type.
  2. Run the workflow again and this time, enter Hello and World. The result should be HelloWorld.
  3. Let's improve this. Change the script to the following:
    myOutput = myFirstValue +" "+ mySecondValue ;
  4. Run it. This does look better now, right?

Integrating JavaScript into our program

Let's use some of the stuff that we just learned to improve our workflow that creates a VM. This is for you to try and learn, as it will repeat topics from this and the last chapter.

Note that when we select the amount of memory for the VM, it's in MB. Let's change this to the easier, usable GB by working through the following steps:

  1. You should consider making a duplicate or a new version of the original workflow before we get started.
  2. Move the vmMemorySize input-parameter to be an attribute, which is similar to what we did in the Creating new parameters section of this chapter.
  3. Create a new vmMemGB input parameter of type number.
  4. Reassign the vmMemorySize parameter to the Create simple virtual machine workflow. You can use validation to make it easier to find them all.
  5. Add a scriptable task before the Create simple virtual machine workflow element.
  6. Add the vmMemGB input parameter as an IN-parameter of the scriptable task.
  7. Add the vmMemorySize attribute as an OUT-parameter of the scriptable task.
  8. Create the script vmMemorySize = vmMemGB * 1024;.
  9. Rename the scriptable task to Convert MB2GB.
  10. Give it a go.
..................Content has been hidden....................

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