Java Example of
Deploying a Virtual Machine from a Library Item in a Resource Pool
This example shows
how to deploy a virtual machine from a local library item in a resource pool.
You can also see how to verify the results of the deployment operation.
This example uses the steps that are
described in the
Deploy a Virtual Machine or Virtual Appliance from an OVF Package in a Content Library
procedure.
For related code
samples, see the
vSphere Automation SDK Java
samples at GitHub.
... // Create a virtual machine deployment specification to accept any network resource. ResourcePoolDeploymentSpec deploymentSpec = new ResourcePoolDeploymentSpec(); String vmName = "MyVirtualMachine"; deploymentSpec.setName(vmName); deploymentSpec.setAcceptAllEULA(true); // Create a deployment target specification to accept any resource pool. String clusterName = "myCluster"; ManagedObjectReference clusterMoRef = VimUtil.getCluster(this.vimAuthHelper.getVimPort(), this.vimAuthHelper.getServiceContent(), clusterName); DeploymentTarget deploymentTarget = new DeploymentTarget(); deploymentTarget.setResourcePoolId(clusterMoRef.getValue()); // Retrieve the library items OVF information and use it for populating the // deployment spec instance. LibraryItem libItemStub = stubFactory.createStub(LibraryItem.class, myStubConfiguration); OvfSummary ovfSummary = libItemStub.filter(libItemId, deploymentTarget); deploymentSpec.setAnnotation(ovfSummary.getAnnotation()); String clientToken = UUID.randomUUID().toString(); DeploymentResult result = libItemStub.deploy(clientToken,libItemId, deploymentTarget, deploymentSpec); // Verify the status of the resource deployment. System.out.printf("Resource Type=%s (ID=%s) status: ", result.getResourceId().getType(), result.getResourceId().getId()); if (result.getSucceeded() == true) { System.out.println("Resource instantiated."); } else { System.out.println("Instantiation failed."); }