Use the PropertyCollector to get a managed object reference for a virtual machine named in the command line.
Given the name of a virtual machine, this function retrieves references to all of the virtual machines in the datacenter and looks for a match to the specified name.
Prerequisites
For this task you need:
- A virtual machine name is referenced by the String variable
vmName
.
- An authenticated Web Services session with the vSphere server that manages the virtual machine.
- A VimPort binding provider is referenced by the variable
methods
, which is attached to the vSphere server connection context.
- The ServiceInstance content is referenced by the variable
serviceContent
.
- The ViewManager object is referenced by the variable
viewMgr
.
- The PropertyCollector object is referenced by the variable
pCollector
.
Procedure
- Declare a function that accepts a virtual machine name and returns a managed object reference.
private static ManagedObjectReference getVmRef( String vmName )
throws Exception
{
ManagedObjectReference vmRef = null;
- Use a container view to collect references to all virtual machines in the datacenter.
Start the collection from the root folder at the top of the managed object hierarchy.
List<String> vmList = new ArrayList<String>();
vmList.add("VirtualMachine");
ManagedObjectReference cViewRef =
methods.createContainerView(viewMgr,
serviceContent.getRootFolder(),
vmList,
true);
- Create an ObjectSpec to define the beginning of the traversal.
Use the setObj method to specify that the container view is the starting object for this traversal. Set the skip property to true
to indicate that you don't want the PropertyCollector to collect properties from the container.
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(cViewRef);
oSpec.setSkip(true);
- Create a traversal spec to traverse from the starting object to the objects in the view.
Set the skip property to false
to indicate that you don't want the PropertyCollector to skip the objects reached by this traversal.
TraversalSpec tSpec = new TraversalSpec();
tSpec.setName("traverseEntities");
tSpec.setType("ContainerView");
tSpec.setPath("view");
tSpec.setSkip(false);
- Add the traversal spec to the object spec.
oSpec.getSelectSet().add(tSpec);
- Specify the property for retrieval (virtual machine name).
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.getPathSet().add("name");
- Create a PropertyFilterSpec and add the object and property specs to it.
PropertyFilterSpec fSpec = new PropertyFilterSpec();
fSpec.getObjectSet().add(oSpec);
fSpec.getPropSet().add(pSpec);
- Create a list for the filters and add the spec to it.
List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>();
fSpecList.add(fSpec);
- Get the data from the server.
RetrieveOptions ro = new RetrieveOptions();
RetrieveResult props = methods.retrievePropertiesEx(pCollector,fSpecList,ro);
- Go through the returned list and look for a match to the specified vmName.
if (props != null) {
for (ObjectContent oc : props.getObjects()) {
String vmname = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null) {
for (DynamicProperty dp : dps) {
vmname = (String) dp.getVal();
// If the name of this virtual machine matches
// the specified name, save the managed object reference.
if (vmname.equals(vmName)) {
vmRef = oc.getObj();
break;
}
}
if (vmRef != null) { break; }
}
}
}
if (vmRef == null) {
System.out.println("Specified Virtual Machine not found.");
throw new Exception();
}
return vmRef;
} // end getVmRef
What to do next
Given the MOref of the named virtual machine, you can use the PropertyCollector to retrieve ia MOref to its parent folder, which you will need to clone the virtual machine.