This example shows how to capture a virtual machine in an OVF package and store the file in a new library item in a specified library.

This example uses the steps that are described in the Create an OVF Package in a Content Library from a Virtual Machine procedure.

import com.vmware.vcenter.ovf.LibraryItem;
import com.vmware.vcenter.ovf.LibraryItemTypes;
import com.vmware.vcenter.ovf.LibraryItemTypes.CreateTarget;
import com.vmware.vcenter.ovf.LibraryItemTypes.DeployableIdentity;
import java.util.UUID;
 
// Specify the resource to be captured.
LibraryItemTypes.DeployableIdentity deployableIdentity = new LibraryItemTypes.DeployableIdentity();
deployableIdentity.setType("VirtualMachine");
deployableIdentity.setId("vm-32");
 
// Create a target spec to identify a library to hold the new item.
LibraryItemTypes.CreateTarget createTarget = new LibraryItemTypes.CreateTarget();
createTarget.setLibraryId(myLibraryId);
 
// Specify OVF properties.
LibraryItemTypes.CreateSpec createSpec = new LibraryItemTypes.CreateSpec();
createSpec.setName("snap-32");
createSpec.setDescription("Snapshot of VM-32");
 
// Initiate synchronous capture operation.
LibraryItem itemStub = myStubFactory.createStub(LibraryItem.class, myStubConfiguration);
String clientToken = UUID.randomUUID().toString();
LibraryItemTypes.CreateResult result = itemStub.create(clientToken, deployableIdentity, createTarget, createSpec);
 
// Verify capture status.
System.out.printf("Resource Type=%s (ID=%s) status:",
                  deployableIdentity.getType(),
                  deployableIdentity.getId());
  if (result.getSucceeded() == true) {
     System.out.println("Resource captured.");

  }else {
     System.out.println("Capture failed.");
  }
  
  if (result.getError() != null) {
    
    for (OvfError error : result.getError().getErrors()) {
        System.out.printf("Error: %s", error.getMessage().toString());
    }
  
    for (OvfWarning warning : result.getError().getWarnings()) {
        System.out.printf("Warning: %s", warning.getMessage().toString());
    }
  
    for (OvfInfo info : result.getError().getInformation()) {
        List<LocalizableMessage> messages = info.getMessage();
    }

    for (LocalizableMessage message : messages) {
      System.out.printf("Message: %s", message.toString());
    }
  }