Home > Vcloud Director, vCloud Ochestrator, VMware > vCloud Director automation via Orchestrator, Automating Org,vDC,vApp Provisioning via CSV Part3- Adding NICs, CPU and modify Memory

vCloud Director automation via Orchestrator, Automating Org,vDC,vApp Provisioning via CSV Part3- Adding NICs, CPU and modify Memory


In part1: https://autodiscover.wordpress.com/2013/11/05/vcloud-orchestrator-automating-orgvdcvapp-provisioning-via-csv-part1/

and pat2: https://autodiscover.wordpress.com/2013/11/11/vcloud-orchestrator-automating-orgvdcvapp-provisioning-via-csv-part2reading-csv-files/

We explored the building blocks for automating vCloud Director cloud provisioning, we provisioned Orgs, vDCs and VMs by reading a CSV file and automating most of the provisioning process, in this blog post we will take a look on how we can add NICs, CPU and modify memory based on VM specs.

Remember, the CSV file structure is:

image

so we need to have VM name, Template name, No. of vCPUs, memory in GB, no. of HDDs and the size of each harddisk.

To start, you will need to clone add NIC to VM, Change Number of CPUs, Change memory capacity and Add a harddisk workflows.

Next, we will create what we will call “A caller” workflow, because each workflow requires certain parameters that only can be ready via query because some of them still not created yet like VM with that name or vDC for example.

We will create 4 callers to call each of the above work flows, here are their Java Scripts code:

VHD Caller Script:

var queryService = vapp.getHost().getQueryService();
var expression = new VclExpression(VclQueryVMField.NAME, vmname , 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) {
Server.log("i am in the VMREF")
            var VMref = new VclReference(); 

        VMref.href = record.href; 
        VMref.name = record.name; 
        VMref.type = record.type; 
      myVMname = host.getEntityByReference(VclFinderType.VM, VMref);
}
}
Server.log (myVMname.name);
var addHDworkflowParameters = new Properties();
addHDworkflowParameters.put("vm",myVMname);
addHDworkflowParameters.put("size",hdsize);
addHDworkflowParameters.put("controller","SCSI Controller 0");
var myaddHDWorkflowToken = new WorkflowToken() ;
myaddHDWorkflowToken = MyAddVHDWF.execute(addHDworkflowParameters);
myaddHDworkflowTokens = new Array(); 
myaddHDworkflowTokens.push(myaddHDWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myaddHDworkflowTokens)

 

 

image

image

Memory Caller:

var ChangeVMMemParameters = new Properties();
var queryService = INPVMvApp.getHost().getQueryService();
var expression = new VclExpression(VclQueryVMField.NAME, INPVMNameString , 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) {
Server.log("i am in the VMREF")
            var VMref = new VclReference(); 

        VMref.href = record.href; 
        VMref.name = record.name; 
        VMref.type = record.type;
 
      myVMname = host.getEntityByReference(VclFinderType.VM, VMref);
}
}
Server.log (myVMname.name);
ChangeVMMemParameters.put("vm",myVMname);
ChangeVMMemParameters.put("newSize",INPNewMem);

var ChangeVMmemWorkflowToken = new WorkflowToken() ;
ChangeVMmemWorkflowToken = AttChangeVMMemWF.execute(ChangeVMMemParameters);
ChangeVMmemWorkflowTokens = new Array(); 
ChangeVMmemWorkflowTokens.push(ChangeVMmemWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(ChangeVMmemWorkflowTokens)

image

image

Change CPU Caller:

var ChangeVMCPUParameters = new Properties();
var queryService = INPVMvApp.getHost().getQueryService();
var expression = new VclExpression(VclQueryVMField.NAME, INPVMNameString , 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) {
Server.log("i am in the VMREF")
            var VMref = new VclReference(); 

        VMref.href = record.href; 
        VMref.name = record.name; 
        VMref.type = record.type;
 
      myVMname = host.getEntityByReference(VclFinderType.VM, VMref);
}
}
Server.log (myVMname.name);
ChangeVMCPUParameters.put("vm",myVMname);
ChangeVMCPUParameters.put("cpuNumber",INPNewCPU);

var ChangeVMCPUWorkflowToken = new WorkflowToken() ;
ChangeVMmemWorkflowToken = AttChangeVMCPUWF.execute(ChangeVMCPUParameters);
ChangeVMCPUWorkflowTokens = new Array(); 
ChangeVMCPUWorkflowTokens.push(ChangeVMmemWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(ChangeVMCPUWorkflowTokens)

image

image

NIC Caller:

var ChangeVMCPUParameters = new Properties();
var queryService = INPVMvApp.getHost().getQueryService();
var expression = new VclExpression(VclQueryVMField.NAME, INPVMNameString , 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) {
Server.log("i am in the VMREF")
            var VMref = new VclReference(); 

        VMref.href = record.href; 
        VMref.name = record.name; 
        VMref.type = record.type;
 
      myVMname = host.getEntityByReference(VclFinderType.VM, VMref);
}
}
Server.log (myVMname.name);
ChangeVMCPUParameters.put("vm",myVMname);
ChangeVMCPUParameters.put("ipAddressingMode",ipAddressingMode);

var ChangeVMCPUWorkflowToken = new WorkflowToken() ;
ChangeVMmemWorkflowToken = AttChangeVMCPUWF.execute(ChangeVMCPUParameters);
ChangeVMCPUWorkflowTokens = new Array(); 
ChangeVMCPUWorkflowTokens.push(ChangeVMmemWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(ChangeVMCPUWorkflowTokens)

 

image

image

Now we finished with our callers scripts, let us go back to the original code to see what is happening.

now to continue interpreting the CSV file, to do that, we need to modify the original script we used to read the CSV file to be like this (this is the final script code):

var i = 0;
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 INPTemplateNameString = lineValues[1];
var MyNoCPU = lineValues[2];
var MyNoMemory = lineValues[3] * 1000;
var MyNoNICS = lineValues[4];
var addVMworkflowParameters = new Properties();
var ChangeVMmemworkflowParameters = new Properties();
var ChangeVMCPUworkflowParameters = new Properties();
var ChangeVMNICworkflowParameters = new Properties();

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)

while (MyNoNICS > 0)
{
ChangeVMNICworkflowParameters.put("INPVMNameString",vmname);
ChangeVMNICworkflowParameters.put("INPVMvApp",MyVappName);
ChangeVMNICworkflowParameters.put("INPVMvDC",myvdcname);
var myaddNICWorkflowToken = new WorkflowToken() ;
myaddNICWorkflowToken = AttNICWF.execute(ChangeVMNICworkflowParameters);
myaddNICWorkflowTokenS = new Array();
myaddNICWorkflowTokenS.push(myaddNICWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myaddNICWorkflowTokenS)

MyNoNICS = MyNoNICS – 1
}

Server.log("Adding Memory to" + " " + vmname);

ChangeVMmemworkflowParameters.put("INPVMNameString",vmname);
ChangeVMmemworkflowParameters.put("INPNewMem",MyNoMemory);
ChangeVMmemworkflowParameters.put("INPVMvApp",MyVappName);
ChangeVMmemworkflowParameters.put("INPVMvDC",myvdcname);
var ChangeVMmemWorkflowToken = new WorkflowToken() ;
ChangeVMmemWorkflowToken = AttMemWF.execute(ChangeVMmemworkflowParameters);
ChangeVMmemWorkflowTokens = new Array();
ChangeVMmemWorkflowTokens.push(ChangeVMmemWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(ChangeVMmemWorkflowTokens)

Server.log("Adding CPU to" + " " + vmname);
ChangeVMCPUworkflowParameters.put("INPVMNameString",vmname);
ChangeVMCPUworkflowParameters.put("INPNewCPU",MyNoCPU);
ChangeVMCPUworkflowParameters.put("INPVMvApp",MyVappName);
ChangeVMCPUworkflowParameters.put("INPVMvDC",myvdcname);
var ChangeVMCPUWorkflowToken = new WorkflowToken() ;
ChangeVMCPUWorkflowToken = AttCPUWF.execute(ChangeVMCPUworkflowParameters);
ChangeVMCPUWorkflowTokens = new Array();
ChangeVMCPUWorkflowTokens.push(ChangeVMCPUWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(ChangeVMCPUWorkflowToken)

Server.log("Adding NICs to" + " " + vmname);

var j = 5;

while (j < linelength)
{
Server.log ("i" + " " + i);
Server.log ("j" + " " + j);
Server.log ("linelength" + " " + linelength);
Server.log ("lineValues[j]" + " " + lineValues[j]);
var addHDworkflowParameters = new Properties();

addHDworkflowParameters.put("host",host);
addHDworkflowParameters.put("vdc",myvdcname);
addHDworkflowParameters.put("vapp",MyVappName);
addHDworkflowParameters.put("hdsize",lineValues[j]);
addHDworkflowParameters.put("vmname",vmname);
var myaddHDWorkflowToken = new WorkflowToken() ;
myaddHDWorkflowToken = MyAddVHDWF.execute(addHDworkflowParameters);
myaddHDworkflowTokens = new Array();
myaddHDworkflowTokens.push(myaddHDWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myaddHDworkflowTokens)
j = j + 1;

}
i = i + 1;
Server.log("looping");
line=fileReader.readLine();
}
fileReader.close;

 

no we are reading the file, storing the configuration in parameters, cloning the VM then looping through the configuration of NIC, vCPU and memory to configure the VM accordingly, let us take a look to what is happening exactly.

Adding NICs:

The following portion of the above code adds NICs to each VM, let us see how:

while (MyNoNICS > 0)
{
ChangeVMNICworkflowParameters.put("INPVMNameString",vmname);
ChangeVMNICworkflowParameters.put("INPVMvApp",MyVappName);
ChangeVMNICworkflowParameters.put("INPVMvDC",myvdcname);
var myaddNICWorkflowToken = new WorkflowToken() ;
myaddNICWorkflowToken = AttNICWF.execute(ChangeVMNICworkflowParameters);
myaddNICWorkflowTokenS = new Array();
myaddNICWorkflowTokenS.push(myaddNICWorkflowToken);
System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete(myaddNICWorkflowTokenS)

MyNoNICS = MyNoNICS – 1
}

The above code creates a workflow parameters with VM name, VApp and vDC, then we create a token and execute The Caller script, the workflow then go and execute and return some parameters that is necessary for the Add NIC workflow and execute the Add NIC workflow.

the same goes for the rest of callers and java codes with different parameters and attributes.

once all set and done, you are ready to go, and this will read the CSV, create org, vdc, vapp, vms with the specifications you set in the CSV.

below is a copy of the workflows to import them and have a look.

https://skydrive.live.com/redir?resid=6B566FD2C47B21C4%21130

In Part 4 we will look on how to add approval and send email notifications.

 

Merry Christmas.

  1. December 23, 2013 at 5:23 pm

    You are the best, its very amazing article.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: