use of com.vmware.vim25.ObjectContent in project CloudStack-archive by CloudStack-extras.
the class HostMO method getVmPropertiesOnHyperHost.
public ObjectContent[] getVmPropertiesOnHyperHost(String[] propertyPaths) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() for VM properties. target MOR: " + _mor.get_value() + ", properties: " + new Gson().toJson(propertyPaths));
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.setPathSet(propertyPaths);
TraversalSpec host2VmTraversal = new TraversalSpec();
host2VmTraversal.setType("HostSystem");
host2VmTraversal.setPath("vm");
host2VmTraversal.setName("host2VmTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.setSelectSet(new SelectionSpec[] { host2VmTraversal });
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.setPropSet(new PropertySpec[] { pSpec });
pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
ObjectContent[] properties = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() done");
return properties;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VmwareResource method getVmStates.
private HashMap<String, PowerState> getVmStates() throws Exception {
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
int key = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
if (key == 0) {
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
}
String instanceNameCustomField = "value[" + key + "]";
// CLOUD_VM_INTERNAL_NAME stores the internal CS generated vm name. This was earlier stored in name. Now, name can be either the hostname or
// the internal CS name, but the custom field CLOUD_VM_INTERNAL_NAME always stores the internal CS name.
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template", instanceNameCustomField });
HashMap<String, PowerState> newStates = new HashMap<String, PowerState>();
if (ocs != null && ocs.length > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> objProps = oc.getPropSet();
if (objProps != null) {
boolean isTemplate = false;
String name = null;
String VMInternalCSName = null;
VirtualMachinePowerState powerState = VirtualMachinePowerState.POWERED_OFF;
for (DynamicProperty objProp : objProps) {
if (objProp.getName().equals("config.template")) {
if (objProp.getVal().toString().equalsIgnoreCase("true")) {
isTemplate = true;
}
} else if (objProp.getName().equals("runtime.powerState")) {
powerState = (VirtualMachinePowerState) objProp.getVal();
} else if (objProp.getName().equals("name")) {
name = (String) objProp.getVal();
} else if (objProp.getName().contains(instanceNameCustomField)) {
if (objProp.getVal() != null)
VMInternalCSName = ((CustomFieldStringValue) objProp.getVal()).getValue();
} else {
assert (false);
}
}
if (VMInternalCSName != null)
name = VMInternalCSName;
if (!isTemplate) {
newStates.put(name, convertPowerState(powerState));
}
}
}
}
return newStates;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VmwareClient method getDecendentMoRef.
/**
* Get the ManagedObjectReference for an item under the
* specified root folder that has the type and name specified.
*
* @param root a root folder if available, or null for default
* @param type type of the managed object
* @param name name to match
*
* @return First ManagedObjectReference of the type / name pair found
*/
public ManagedObjectReference getDecendentMoRef(ManagedObjectReference root, String type, String name) throws Exception {
if (name == null || name.length() == 0) {
return null;
}
try {
// Create PropertySpecs
PropertySpec pSpec = new PropertySpec();
pSpec.setType(type);
pSpec.setAll(false);
pSpec.getPathSet().add("name");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(root);
oSpec.setSkip(false);
oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.getPropSet().add(pSpec);
spec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
specArr.add(spec);
ManagedObjectReference propCollector = getPropCol();
List<ObjectContent> ocary = vimPort.retrieveProperties(propCollector, specArr);
if (ocary == null || ocary.size() == 0) {
return null;
}
// filter through retrieved objects to get the first match.
for (ObjectContent oc : ocary) {
ManagedObjectReference mor = oc.getObj();
List<DynamicProperty> propary = oc.getPropSet();
if (type == null || type.equals(mor.getType())) {
if (propary.size() > 0) {
String propval = (String) propary.get(0).getVal();
if (propval != null && name.equalsIgnoreCase(propval))
return mor;
}
}
}
} catch (InvalidPropertyFaultMsg invalidPropertyException) {
s_logger.debug("Failed to get Vmware ManagedObjectReference for name: " + name + " and type: " + type + " due to " + invalidPropertyException.getMessage());
throw invalidPropertyException;
} catch (RuntimeFaultFaultMsg runtimeFaultException) {
s_logger.debug("Failed to get Vmware ManagedObjectReference for name: " + name + " and type: " + type + " due to " + runtimeFaultException.getMessage());
throw runtimeFaultException;
}
return null;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class VmwareClient method validate.
public boolean validate() {
//
// There is no official API to validate an open vCenter API session. This is hacking way to tell if
// an open vCenter API session is still valid for making calls.
//
// It will give false result if there really does not exist data-center in the inventory, however, I consider
// this really is not possible in production deployment
//
// Create PropertySpecs
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datacenter");
pSpec.setAll(false);
pSpec.getPathSet().add("name");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(getRootFolder());
oSpec.setSkip(false);
oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.getPropSet().add(pSpec);
spec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
specArr.add(spec);
try {
List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr);
if (ocary != null && ocary.size() > 0)
return true;
} catch (Exception e) {
return false;
}
return false;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class ClusterMO method getHostPropertiesOnCluster.
public ObjectContent[] getHostPropertiesOnCluster(String[] propertyPaths) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() on Host properties. target MOR: " + _mor.getValue() + ", properties: " + new Gson().toJson(propertyPaths));
PropertySpec pSpec = new PropertySpec();
pSpec.setType("HostSystem");
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
TraversalSpec cluster2HostTraversal = new TraversalSpec();
cluster2HostTraversal.setType("ClusterComputeResource");
cluster2HostTraversal.setPath("host");
cluster2HostTraversal.setName("cluster2HostTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(cluster2HostTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() done");
return properties.toArray(new ObjectContent[properties.size()]);
}
Aggregations