Following on from my series of posts on uploading OVA files to Content Library, and searching content library, the final step of all of this would be to deploy a VM using one of these templates
Inputs Required
The list of inputs that we need is as follows
Name | Type | Description |
datastore | VC:Datastore | The datastore to deploy to |
endpoint | VAPI:VAPIEndpoint | The VAPI endpoint to use |
folder | VC:VmFolder | The folder to deploy to |
host | VC:HostSystem | The target host for deployment |
hostname | string | Name of the new VM |
ovfLibraryItemId | string | id of the content library item |
respool | VC:ResourcePool | Resource pool to deploy to |
Setup
Similar to previous steps, the first thing you need to do is make a connection to the VAPI endpointvar client = endpoint.client();
Next, we need to create a model for the deployment target – where are we deploying the VM to? We use the deployment target object for this:var deploymentTarget = new com_vmware_vcenter_ovf_library__item_deployment__target();
deploymentTarget.folder_id = folder.id;
deploymentTarget.host_id = host.id;
deploymentTarget.resource_pool_id = pool.id;
The final step before we deploy the VM is to create a resource pool deployment spec, this include the name, and the datastore to use:var resourcePoolDeploymentSpec = new com_vmware_vcenter_ovf_library__item_resource__pool__deployment__spec();
resourcePoolDeploymentSpec.accept_all_EULA = true;
resourcePoolDeploymentSpec.name = hostname;
resourcePoolDeploymentSpec.default_datastore_id = datastore.id;
Quite why the datastore is in that one rather than the deployment target is a mystery to me. The only thing I can think of is that the deployment spec includes the storage provisioning profile.
Deploying the VM
To deploy the new VM, we use the com_vmware_vcenter_ovf_library__item call, and pass the ovfLibraryItemId, with the deployment target and deployment specs that we created abovevar ovfSvc = new com_vmware_vcenter_ovf_library__item(client);
var result = ovfSvc.deploy(null, ovfLibraryItemId, deploymentTarget, resourcePoolDeploymentSpec);
Returning the vmObject
The deploy method above returns a structure that includes the id of the VM which we can use to find the vmObject, which is an output item of type VC:VirtualMachinevmObject = VcPlugin.getAllVirtualMachines(null, "xpath:matches(id, '" + result.resource_id.id + "')")[0];
The search returns an array, so I’m using the [0]
to strip the object out of the array – searching on an id, there can only be one VM with a particular id unless you’re using linked mode .
Finally we close the connection to the endpointclient.close();
Full code listing
// Set the VAPI endpoint to the first endpoint returned
if (endpoint == null) {
throw "Unable to locate a VAPI endpoint";
}
var client = endpoint.client();
// create a DeploymentTarget
var deploymentTarget = new com_vmware_vcenter_ovf_library__item_deployment__target();
deploymentTarget.folder_id = folder.id;
deploymentTarget.host_id = host.id;
deploymentTarget.resource_pool_id = pool.id;
// create a ResourcePoolDeploymentSpec
var resourcePoolDeploymentSpec = new com_vmware_vcenter_ovf_library__item_resource__pool__deployment__spec();
resourcePoolDeploymentSpec.accept_all_EULA = true;
resourcePoolDeploymentSpec.name = hostname;
resourcePoolDeploymentSpec.default_datastore_id = datastore.id;
// deploy the ovf
var ovfSvc = new com_vmware_vcenter_ovf_library__item(client);
var result = ovfSvc.deploy(null, ovfLibraryItemId, deploymentTarget, resourcePoolDeploymentSpec);
// return the vmObject
vmObject = VcPlugin.getAllVirtualMachines(null, "xpath:matches(id, '" + result.resource_id.id + "')")[0];
client.close();