Archive

Archive for the ‘vCloud Orchestrator’ Category

vCloud Orchestrator, Automating Org,vDC,vApp Provisioning via CSV Part2–reading CSV files

November 11, 2013 Leave a comment

In part 2 of this series, we will explore the required java script code required to read CSV file and load the information into vCO.

Reference: https://communities.vmware.com/thread/445802

To use the code, drop a scriptable task element in the workflow, and make sure to link all the required parameters to it from the previous workflows:

image

The code will read the following CSV file format:

image

the csv has the following format: VMname,OS Edition,No. of CPU. Memory, No. Of Disks, HD1 size in GB, HD2 Size in GB, HDX size in GB.

now the JavaScript code required to read this file is:

var csvFileName = "c:\\Orchestrator\\customer.csv"; 
Server.log(csvFileName); 

var fileReader = new FileReader(csvFileName); 
fileReader.open(); 
var line=fileReader.readLine(); 
Server.log (line);

    while (line != null){ 
Server.log ("loop Started");
var lineValues = line.split(","); 
var linelength = lineValues.length
var vmname = lineValues[0];
var MyTemplateName = lineValues[1];
var MyNoCPU = lineValues[2];
var MyNoMemory = lineValues[3];
var MyNoDisks = lineValues[4];

addVMworkflowParameters.put("INPdestinationVapp",MyVappName);
addVMworkflowParameters.put("INPTemplateNameString",INPTemplateNameString);
addVMworkflowParameters.put("INPVMNameString",vmname);
var myaddVMWorkflowToken = new WorkflowToken() ;
myaddVMWorkflowToken = MyAddVMWF.execute(addVMworkflowParameters);
myaddVMworkflowTokens = new Array(); 
myaddVMworkflowTokens.push(myaddVMWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myaddVMworkflowTokens)

The first section read the file and opens it then starts reading the file line by line, the While loop will start looping through the file contents line by line and splitting each line using “,” as a delimiter.

once the information is loaded, we are ready to take actions in each line, the actions will be running workflows to create the VM and assign the proper NIC, Memory and CPU to it.

The Second part, Will call another workflow, but let us explore why we are doing that…

Running External Workflow using JavaScript and wait for its completion:

The work flows that we will run is a custom workflow that we will create to lookup the data from vCloud Director, what do we mean, let us explore:

To create a VM from a template VM, we will create a vDC with all the templates that we need:

image

We will use those VMs as templates that we can close, but let us explore first the built-in workflow in orchestrator “Add vApp Virtual Machine”:

This Workflow takes the following inputs as parameters:

image 

a vApp where the VM will be placed, the source vm that will be cloned and the new VM name, the problem is that the vm input is from type vCloud:VM, but the VM name we read from the CSV file is string, so we need to search for the template by its name and return the object vCLoud:VM type with that name to this workflow.

Note: I wish if there is something like powershell (get-vm –name “myvm”) byt there isn’t , if you know a better way, I will be glad to do so.

So, how we can search the VM by name, we will create a new workflow with a scriptable task as following:

image

and takes the following inputs:

image

Configured with the following parameters:

image

and the scriptable task element runs the following code and configured with the following local inputs:

image

 

var CloneVMWFParameters = new Properties();
var queryService = AttSourceVapp.getHost().getQueryService();
var expression = new VclExpression(VclQueryVMField.NAME, INPTemplateNameString , VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);
var resultSet = queryService.queryRecords(VclQueryRecordType.ADMINVM, params);
    var records = resultSet.getRecords(new VclQueryResultAdminVMRecord());
Server.log (records.length);
for each (var record in records) { 
        if (record.vdc == vdc.getReference().href) { 
            var VMref = new VclReference(); 

        VMref.href = record.href; 
        VMref.name = record.name; 
        VMref.type = record.type;
 
      myVMname = host.getEntityByReference(VclFinderType.VM, VMref);
}
}

CloneVMWFParameters.put("vApp",INPdestinationVapp);
CloneVMWFParameters.put("vm",myVMname);
CloneVMWFParameters.put("vmName",INPVMNameString);

var myCloneVMWorkflowToken = new WorkflowToken() ;
myCloneVMWorkflowToken = AttvappVMWF.execute(CloneVMWFParameters);
myCloneVMWorkflowTokens = new Array(); 
myCloneVMWorkflowTokens.push(myCloneVMWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myCloneVMWorkflowTokens)

     The workflow creates a variable CloneVMWFParameters of type properties and creates a new query to search for a VM with the name of the template, it will make sure that this VM is located inside the vDC where templates are stored (if (record.vdc == vdc.getReference().href) ) then returns the VMobject with its href and name and type.

we then construct a a workflow properties to call the “Add vApp Virtual Machine workflow” with the vApp, VM object and New VM Name and then execute it and wait for its completion.

Reverting back to the initial code, we talked about:

addVMworkflowParameters.put("INPdestinationVapp",MyVappName);
addVMworkflowParameters.put("INPTemplateNameString",INPTemplateNameString);
addVMworkflowParameters.put("INPVMNameString",vmname);
var myaddVMWorkflowToken = new WorkflowToken() ;
myaddVMWorkflowToken = MyAddVMWF.execute(addVMworkflowParameters);
myaddVMworkflowTokens = new Array(); 
myaddVMworkflowTokens.push(myaddVMWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myaddVMworkflowTokens)

So we are creating the var addVMworkflowParameters as properties and we construct the properties with following parameters (INPdestinationVapp name “the vApp was just created”, INPTemplateNameString “the name of the template VM, this name was read from the CSV”, and the INPVMNameString which is the new VM name”).

Now our caller workflow will query the vcloud director for the template name, return the VM object corresponding to it, and then call the “Add vApp Virtual Machine” workflow with the parameters and copy the VM to the destination vApp.

If you have a smarter way to do it, let me know, I got the feeling that there might be a better way to do it.

In part 3, we will add NICs, Memory and HDs and in part 4 we will take a look on how to send email notification and add approval cycle, so stay tuned Winking smile.

Advertisements
%d bloggers like this: