use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VmwareClient method retrieveMoRefProperties.
private List<ObjectContent> retrieveMoRefProperties(ManagedObjectReference mObj, List<String> props) throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setAll(false);
pSpec.setType(mObj.getType());
pSpec.getPathSet().addAll(props);
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(mObj);
oSpec.setSkip(false);
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.getPropSet().add(pSpec);
spec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
specArr.add(spec);
return vimPort.retrieveProperties(getPropCol(), specArr);
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VmwareContext method getHostMorByPath.
public ManagedObjectReference getHostMorByPath(String inventoryPath) throws Exception {
assert (inventoryPath != null);
String[] tokens;
if (inventoryPath.startsWith("/"))
tokens = inventoryPath.substring(1).split("/");
else
tokens = inventoryPath.split("/");
ManagedObjectReference mor = getRootFolder();
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
List<ObjectContent> ocs;
PropertySpec pSpec = null;
ObjectSpec oSpec = null;
if (mor.getType().equalsIgnoreCase("Datacenter")) {
pSpec = new PropertySpec();
pSpec.setAll(false);
pSpec.setType("ManagedEntity");
pSpec.getPathSet().add("name");
TraversalSpec dcHostFolderTraversal = new TraversalSpec();
dcHostFolderTraversal.setType("Datacenter");
dcHostFolderTraversal.setPath("hostFolder");
dcHostFolderTraversal.setName("dcHostFolderTraversal");
oSpec = new ObjectSpec();
oSpec.setObj(mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(dcHostFolderTraversal);
} else if (mor.getType().equalsIgnoreCase("Folder")) {
pSpec = new PropertySpec();
pSpec.setAll(false);
pSpec.setType("ManagedEntity");
pSpec.getPathSet().add("name");
TraversalSpec folderChildrenTraversal = new TraversalSpec();
folderChildrenTraversal.setType("Folder");
folderChildrenTraversal.setPath("childEntity");
folderChildrenTraversal.setName("folderChildrenTraversal");
oSpec = new ObjectSpec();
oSpec.setObj(mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(folderChildrenTraversal);
} else if (mor.getType().equalsIgnoreCase("ClusterComputeResource")) {
pSpec = new PropertySpec();
pSpec.setType("ManagedEntity");
pSpec.getPathSet().add("name");
TraversalSpec clusterHostTraversal = new TraversalSpec();
clusterHostTraversal.setType("ClusterComputeResource");
clusterHostTraversal.setPath("host");
clusterHostTraversal.setName("folderChildrenTraversal");
oSpec = new ObjectSpec();
oSpec.setObj(mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(clusterHostTraversal);
} else {
s_logger.error("Invalid inventory path, path element can only be datacenter and folder");
return null;
}
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
ocs = getService().retrieveProperties(getPropertyCollector(), pfSpecArr);
if (ocs != null && ocs.size() > 0) {
boolean found = false;
for (ObjectContent oc : ocs) {
String name = oc.getPropSet().get(0).getVal().toString();
if (name.equalsIgnoreCase(token) || name.equalsIgnoreCase("host")) {
mor = oc.getObj();
found = true;
if (name.equalsIgnoreCase("host"))
i--;
break;
}
}
if (!found) {
s_logger.error("Path element points to an un-existing inventory entity");
return null;
}
} else {
s_logger.error("Path element points to an un-existing inventory entity");
return null;
}
}
return mor;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VirtualMachineMO method getSnapshotDescriptorDatastorePath.
public String getSnapshotDescriptorDatastorePath() throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.getPathSet().add("name");
pSpec.getPathSet().add("config.files");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.FALSE);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
assert (ocs != null);
String vmName = null;
VirtualMachineFileInfo fileInfo = null;
assert (ocs.size() == 1);
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
assert (props.size() == 2);
for (DynamicProperty prop : props) {
if (prop.getName().equals("name")) {
vmName = prop.getVal().toString();
} else {
fileInfo = (VirtualMachineFileInfo) prop.getVal();
}
}
}
}
assert (vmName != null);
assert (fileInfo != null);
// .vmsd file exists at the same directory of .vmx file
DatastoreFile vmxFile = new DatastoreFile(fileInfo.getVmPathName());
return vmxFile.getCompanionPath(vmName + ".vmsd");
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VmwareClient method getDynamicProperty.
/**
* Get the property value of a managed object.
*
* @param mor
* managed object reference
* @param propertyName
* property name.
* @return property value.
* @throws Exception
* in case of error.
*/
@SuppressWarnings("unchecked")
public <T> T getDynamicProperty(ManagedObjectReference mor, String propertyName) throws Exception {
List<String> props = new ArrayList<String>();
props.add(propertyName);
List<ObjectContent> objContent = retrieveMoRefProperties(mor, props);
Object propertyValue = null;
if (objContent != null && objContent.size() > 0) {
List<DynamicProperty> dynamicProperty = objContent.get(0).getPropSet();
if (dynamicProperty != null && dynamicProperty.size() > 0) {
DynamicProperty dp = dynamicProperty.get(0);
propertyValue = dp.getVal();
/*
* If object is ArrayOfXXX object, then get the XXX[] by
* invoking getXXX() on the object.
* For Ex:
* ArrayOfManagedObjectReference.getManagedObjectReference()
* returns ManagedObjectReference[] array.
*/
Class dpCls = propertyValue.getClass();
String dynamicPropertyName = dpCls.getName();
if (dynamicPropertyName.indexOf("ArrayOf") != -1) {
String methodName = "get" + dynamicPropertyName.substring(dynamicPropertyName.indexOf("ArrayOf") + "ArrayOf".length(), dynamicPropertyName.length());
Method getMorMethod = dpCls.getDeclaredMethod(methodName, null);
propertyValue = getMorMethod.invoke(propertyValue, (Object[]) null);
}
}
}
return (T) propertyValue;
}
Aggregations