Java Example of Creating an OVF Template in a Content Library from a Virtual Machine

This example shows how to capture a virtual machine in an OVF template 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 Template in a Content Library from a Virtual Machine or vApp procedure.
For related code samples, see the
vsphere-automation-sdk-java
VMware repository at GitHub.
... // 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()); } }