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 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/.
Let's have a look at the rules that govern how JavaScript is written. In the next section, we will try some stuff out.
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 (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.
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 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.
//
, as follows:System.log(test); //put the content of test into the logs
/*
and end with */
, as follows:/* This is a big comment that goes over multiple lines */
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.
Let's leave the dry theory behind and have a look at some practical examples.
First, we will create a space where we can write some JavaScript and make Orchestrator use it, as follows:
The scriptable task element will contain our JavaScript, and it is the only element that we will need in this whole chapter.
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.
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.
Now that we have two input parameters and one output parameter, we can do some calculations, as follows:
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.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.
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.
We just played around with numbers. Let's do the same with strings, as follows:
Number
to String
.Number
and mySecondValue to String
. Run it.String
and run it again.The results should look like this:
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:
String
type.Hello
and World
. The result should be HelloWorld.myOutput = myFirstValue +" "+ mySecondValue ;
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:
vmMemorySize = vmMemGB * 1024;
.Convert MB2GB
.