Create two clones of the virtual machine and build a PropertyFilterSpec that uses a ListView managed object to track the Tasks to completion.

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.

Prerequisites

For this task you need:
  • A virtual machine managed object reference in a variable named vmRef.
  • An authenticated Web Services session with the vSphere server that manages the virtual machine.
  • A VimPort binding provider referenced by the variable methods, which is attached to the vSphere server connection context.
  • The ViewManager object is referenced by the variable viewMgr.
  • A PropertyCollector instance referenced by the variable pCollector.

Procedure

  1. Declare a method that accepts a virtual machine MOref to clone it and monitor the Task objects.
      private static void cloneVM(ManagedObjectReference vmRef) throws Exception {
  2. Use the getVMParent method to get a MOref for the parent folder of the virtual machine.

    If the parent object is a Folder, the CloneVMTask method will create the clones in the same folder that contains the specified virtual machine. If the parent object is a VirtualApp, the VirtualMachine cannot 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();
      }
  3. 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);
  4. Create two clone virtual machines.
        ManagedObjectReference cloneTask = methods.cloneVMTask( vmRef,
                                                                 folder,
                                                                 "clone__1",
                                                                 cloneSpec);
        ManagedObjectReference cloneTask2 = methods.cloneVMTask( vmRef,
                                                                 folder,
                                                                 "clone__2",
                                                                 cloneSpec);
    The CloneVMTask method returns a Task object from each invocation. You can use PropertyCollector to track task completion.
  5. 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);
  6. Create an object spec to describe the property collection.

    Add the list of Task objects to the object spec. Set the skip property to true so the PropertyCollector will ignore the properties of the ListView object itself.

        ObjectSpec oSpec = new ObjectSpec();
        oSpec.setObj(cloneTaskList);
        oSpec.setSkip(true);
  7. Create a traversal spec to tell the PropertyCollector how to move from the ListView object to the Task objects.

    Set the skip property to false so 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");
  8. Add the traversal spec to the object spec.
        oSpec.getSelectSet().add(tSpec);
  9. Create a property spec to collect the Task.info.state and Task.info.result properties.
        PropertySpec pSpec = new PropertySpec();
        pSpec.setType("Task");
        pSpec.setAll(false);
        pSpec.getPathSet().add("info.state");
        pSpec.getPathSet().add("info.result");
  10. Create a filter spec from the object spec and the property spec, and use the filter spec to create a PropertyFilter object 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);

Results

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.

What to do next

Use the PropertyCollector.WaitForUpdatesEx method to monitor the status of the Task objects.