Had some very very good training recently with one of the vCO best Joerg Lew.
After the custom training I now understand vCO so much better and thought I would post the first part of my vCO Learning posts, which will hopefully help people out when it comes to writing flows and understanding how they work.
Today we will be creating a flow that will create VMs from a csv list that will contain VM name, num vCPU, Num Ram, disk size, OS type.
1) make sure you have vCO setup and that it is connected to your vCenter before you start. Now first thing is first lets create a new workflow, you should now have something similar to the image below.
2) Now we are going to build this flow with almost zero bells and whistles, it will be as basic as you can get, for one so its not a 100 page blog post and second this will build the base and you can then tweek and add improvements where you wish. Search for the workflow under vcenter/virtual machine management/basic called “create simple dvportgroup virtual machine” and then drag it over onto your new flow. like the image below
4) when you have hit that setup button you will get a promote workflow parameters dialogue box. Here we want to create all the inputs as value which makes them an attribute, and leave the output as default. While we are here we can set some of the variables that will not be in the CSV file which in most cases will be static but you can most definitely add the with the CSV file if need. But here we will be setting VM folder, VM host, VM datastore, VM Network and resource pool. When clicking in the value you will get a tree list of your current connected vCenters and you just select which one you want. You will end up with something similar to the images below.
5) ok now we are doing inputs we will need to create an input for the file we will upload, This is done from the inputs tab of the new workflow you are creating. create the input and select it as being the mimeAttachement. like shown in the image below
6) while we are adding inputs and attributes I think we will need two more attributes one being the array we want to feed the CSV file data into and the other being an variable we can use to feed the array rows into to build from. so here I created an allFiles attribute as array/string type and currentLnine just being a string. This is done from the general tab of the new workflow we are creating. Like the image below
7) now we want to drag in a custom script from the generic items on the left and place in front of the vm workflow we dragged in earlier. As shown in the image below. This script will be grabbing the CSV and passing the data into an array so give it a meaningful name. You will have something similar to the below image.
8) next we need to edit the script we just dragged in do this by selecting the pen icon above the script picture, We need to add the input as the “inputFile” and the output as the “allLines” This is shown in the below images
9) now we have the inputs and outputs sorted for our new custom script object we now need to do something to use the input and output the output. So we create a little bit of a java script
allLines = new Array(); var completedCSVData = inFile.content allLines = completedCSVData.split(/\r\n|\r\n/); System.debug("all lines: = " + allLines);
10) next we need to pull the data out of the array 1 row at a time so for this we need to drag out another custom script object and place it ofter the one we just did and give it a meaningful name, like the image below.
11) now its time to edit this script, we need to set allFiles as the input and the output is both the currentLine and allFiles, I wont show you in individual inputs and outputs, the visual view should give you a good indication
12) now we need to write some java script to do something with the input and outputs. We will want to add the following code:
System.debug("Before currentline " +allLines) currentLine = allLines.pop(); System.debug("currentline "+ currentLine); System.debug(allLines)
we use the “pop” function which takes the last time in the array and pulls it out, we add that line to currentline and allLines now has one less row in it. This is why we want to output allLines back to the AllLines variable so it gets overwritten.
13) getting close now we now have the data we need we just need to make it usable, So we have to drag out another custom script object and place it before the create vm workflow as shown in the image below. Give this one a name I have called it parsing data as thats what we will be doing.
14) We know that the CSV file will have the name, CPU, RAM, Disk and OS type so we need to add them to the out parameters and the input will be current line. The image below will explain it with the visual binding tag.
15) now time for some more code to break up the current line into the different properties. to do this we will use the following code:
var values = currentLine.split(","); vmName = values; vmNbOfCpus = values; vmMemorySize = values; vmDiskSize = values; var vmTemp = values; vmGuestOs = VcVirtualMachineGuestOsIdentifier.fromString(vmTemp);
because we have strings and the OS type needs to be the type VcVirtualMachineGuestOsIdentifier we need to convert it with the following 2 lines:
var vmTemp = values;
vmGuestOs = VcVirtualMachineGuestOsIdentifier.fromString(vmTemp);
16) OK so that is basically it but we are missing something, if we ran this it would only create one VM we need a way to loop through the allLines array. SO what we will do is place a custom decision before we pop the line out of allLines. drag and drop the object onto the schema and then connect the create vm workflow to the custom decision. this is shown in the below image.
17) This one is a quick one liner, we have out input as allLines, because we did the pop() function previously this automatically removes the last line and we write the allLine back out. this way we loop it through this and the allLines will be reduced to nothing. But out CSV file has a header so we need a way to end the workflow so we enter this code
return (allLines.length >1 ); so that when there is only one line left in the allLines array which will be the CSV header line we exit.
Hopefully you have gotten something out of this, Its a good base to start on and expand on.
Head over to Flowgrab to download the flow an all supporting actions etc located here
I have attached the workflow I used which can be imported into your vCO if you want.