Create Clones of the Virtual
Machine
Create two clones of the virtual machine and build a PropertyFilterSpec that uses a
ListView managed object to track the Tasks to completion.
For this task you need:
- A virtual machine managed object reference in a variable namedvmRef.
- An authenticated Web Services session with the vSphere server that manages the virtual machine.
- A VimPort binding provider referenced by the variablemethods, which is attached to the vSphere server connection context.
- The ViewManager object is referenced by the variableviewMgr.
- A PropertyCollector instance referenced by the variablepCollector.
Given the MOref of a virtual machine and
the MOref of its parent folder, this function initiates two clone operations,
resulting in two
Task
managed objects that you can use to monitor
progress of the clone operations. The function uses the Task
objects to build a PropertyFilterSpec
that you can use to report
status updates for the two Task
objects.- Declare a method that accepts a virtual machine MOref to clone it and monitor theTaskobjects.private static void cloneVM(ManagedObjectReference vmRef) throws Exception {
- Use thegetVMParentmethod to get a MOref for the parent folder of the virtual machine.If the parent object is aFolder, theCloneVMTaskmethod will create the clones in the same folder that contains the specified virtual machine. If the parent object is aVirtualApp, theVirtualMachinecannot be cloned.DynamicProperty parent = getVMParentProperty( vmRef ); if (parent.getName().equals("parentVApp")) { System.out.println("Will not clone VM in a VApp."); throw new Exception(); } else { ManagedObjectReference folder = (ManagedObjectReference) parent.getVal(); }
- Create a clone specification. Use default values whenever possible.VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); VirtualMachineRelocateSpec vmrs = new VirtualMachineRelocateSpec(); cloneSpec.setLocation(vmrs); cloneSpec.setPowerOn(true); cloneSpec.setTemplate(false);
- Create two clone virtual machines.ManagedObjectReference cloneTask = methods.cloneVMTask( vmRef, folder, "clone__1", cloneSpec); ManagedObjectReference cloneTask2 = methods.cloneVMTask( vmRef, folder, "clone__2", cloneSpec);TheCloneVMTaskmethod returns aTaskobject from each invocation. You can use PropertyCollector to track task completion.
- Create a list view to reference the clone tasks.List<ManagedObjectReference> taskList = new ArrayList<ManagedObjectReference>(); taskList.add(cloneTask); taskList.add(cloneTask2); ManagedObjectReference cloneTaskList = methods.createListView(viewMgr, taskList);
- Create an object spec to describe the property collection.Add the list ofTaskobjects to the object spec. Set theskipproperty totrueso the PropertyCollector will ignore the properties of theListViewobject itself.ObjectSpec oSpec = new ObjectSpec(); oSpec.setObj(cloneTaskList); oSpec.setSkip(true);
- Create a traversal spec to tell the PropertyCollector how to move from theListViewobject to theTaskobjects.Set theskipproperty tofalseso the PropertyCollector will try to collect properties from the managed objects at the end of this traversal step.TraversalSpec tSpec = new TraversalSpec(); tSpec.setName("traverseTasks"); tSpec.setPath("view"); tSpec.setSkip(false); tSpec.setType("ListView");
- Add the traversal spec to the object spec.oSpec.getSelectSet().add(tSpec);
- Create a property spec to collect theTask.info.stateandTask.info.resultproperties.PropertySpec pSpec = new PropertySpec(); pSpec.setType("Task"); pSpec.setAll(false); pSpec.getPathSet().add("info.state"); pSpec.getPathSet().add("info.result");
- Create a filter spec from the object spec and the property spec, and use the filter spec to create aPropertyFilterobject to guide the property collection.The filter managed object needs to persist on the server until the PropertyCollector finishes reporting task status.PropertyFilterSpec fSpec = new PropertyFilterSpec(); fSpec.getObjectSet().add(oSpec); fSpec.getPropSet().add(pSpec); ManagedObjectReference pFilter = methods.createFilter(pCollector, fSpec, true);
Two cloning operations are underway, with status of the operations reflected in
Task
objects. A PropertyFitler
managed object
is created and configured to monitor the task status.Use the
PropertyCollector.WaitForUpdatesEx
method to monitor the
status of the Task
objects.