Learning vCO Part 1 – Bulk VM creation by CSV

Hey all!!!!

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.
bulk01

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
bulk02

3) once done you will get a pop up like in the image below, This is where we will set up the attributes the flow needs. hit the setup button on the top right corner.
bulk03

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.
bulk04
bulk05

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
bulk08

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
bulk09

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.
bulk07

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
bulk10
bulk11

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);

Basically we grab all the content with var completedCSVData = inFile.content then we split it into separate lines and add it into the array with allLines = completedCSVData.split(/\r\n|\r\n/);
bulk13

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.
bulk28

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
Screen Shot 2014-08-16 at 9.44.28 am

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.
bulk17

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.
bulk21

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.
bulk19

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[0];
vmNbOfCpus = values[1];
vmMemorySize = values[2];
vmDiskSize = values[3];
var vmTemp = values[4];
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[4];
vmGuestOs = VcVirtualMachineGuestOsIdentifier.fromString(vmTemp);

bulk20

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.
Screen Shot 2014-08-16 at 10.33.37 am

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.
Screen Shot 2014-08-16 at 12.54.32 pm

Ok now its time to test this workflow out.
First I just created a CSV file to test
Capture

17) Hit the run or debug button and we should get the prompt to select the file we want. this is shown in the below image.
bulk24

18) hit the not set link and a dialog box will appear to select the file once selected hit ok and then submit.
bulk25

Hopefully if we didn’t make any errors it should run through loop around until all 4 virtuals are created and then end
bulk26
bulk27

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.

Cheers

6 thoughts on “Learning vCO Part 1 – Bulk VM creation by CSV

  1. Pingback: Learning vCO Part 2 - Creating Azure virtual machines | Virtualise Me

  2. Pingback: Newsletter: August 31, 2014 | Notes from MWhite

  3. Pingback: Learning vCO Part 3 – Extending bulk vm flow | Virtualise Me

  4. Hello,
    I have this error in Create simple dvPor:
    item: ‘Create simple dvPortGroup virtual machine/item0’, state: ‘failed’, business state: ‘null’, exception: ‘TypeError: Cannot read property “config” from null (Dynamic Script Module name : createVirtualEthernetCardDvNetworkConfigSpec#7)’

    Any idea? Thanks

    • Hey Arul,

      impossible to say, I would take a guess and think there is something wrong with the current line value (not the right amount of elements) or its the guest OS, it has to be a valid vSphere guest OS type cant be just w2k12 or something like that.

      I will have to download this workflow and give it a go, its been many years and dont have it in my lab anymore 🙂

      Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Anti SPAM BOT Question * Time limit is exhausted. Please reload CAPTCHA.