Using VCO to Obtain the vCenter Datacenter MoRef

While working with VCO and NSX I find that I frequently need the MoRef (Managed Object Reference) of the Datacenter object in vCenter. Depending on your design you may have multiple datacenters. This workflow will accept the name of the datacenter as the input and will output the moref. You will need the vCenter plugin installed for this to function.

Create a new workflow and edit it.

2014-04-27_14-49-08

Add and input parameter called strDatacenter.

2014-04-27_14-56-42

Create an output parameter called datacenterMoref.

2014-04-27_14-59-42

Add a Scriptable task on the Schema tab.

2014-04-27_15-02-35

Bind the input parameter.

2014-04-27_15-11-32

Bind the output parameter.

2014-04-27_15-12-49

Modify the code in the scripting object. This code will call the VC plugin and query for all datacenter objects. It will check for a match of the input parameter and grab the moref.

// Get all datacenter objects
var datacenters = VcPlugin.getAllDatacenters();
// While loop setup, length and increment counter
var length = datacenters.length;
var i = 0;
// Loop through the datacenter objects, find matching datacenter, extract moref
while (i < length){
	if (datacenters[i].name == strDatacenter) {
		datacenterMoref = datacenters[i].id
		}
	i++;
	}
// Log the moref
System.log(datacenterMoref);
2014-04-27_15-14-26

Save the workflow and test.

2014-04-27_15-17-17

If successful, you should see the moref id in the logs. You can now use this in other workflows.

2014-04-27_15-18-52