use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class DatacenterMO method findVmByNameAndLabel.
public List<VirtualMachineMO> findVmByNameAndLabel(String vmLabel) throws Exception {
CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
int key = cfmMo.getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_UUID);
assert (key != 0);
List<VirtualMachineMO> list = new ArrayList<VirtualMachineMO>();
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] { "name", String.format("value[%d]", key) });
if (ocs != null && ocs.size() > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
for (DynamicProperty prop : props) {
if (prop.getVal() != null) {
if (prop.getName().equalsIgnoreCase("name")) {
if (prop.getVal().toString().equals(vmLabel)) {
list.add(new VirtualMachineMO(_context, oc.getObj()));
// break out inner loop
break;
}
} else if (prop.getVal() instanceof CustomFieldStringValue) {
String val = ((CustomFieldStringValue) prop.getVal()).getValue();
if (val.equals(vmLabel)) {
list.add(new VirtualMachineMO(_context, oc.getObj()));
// break out inner loop
break;
}
}
}
}
}
}
}
return list;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class DatacenterMO method checkIfVmAlreadyExistsInVcenter.
public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter, String vmNameInCS) throws Exception {
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
if (key == 0) {
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
}
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] { "name", String.format("value[%d]", key) });
if (ocs != null && ocs.size() > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
String vmVcenterName = null;
String vmInternalCSName = null;
for (DynamicProperty prop : props) {
if (prop.getName().equals("name")) {
vmVcenterName = prop.getVal().toString();
}
if (prop.getName().startsWith("value[") && prop.getVal() != null) {
vmInternalCSName = ((CustomFieldStringValue) prop.getVal()).getValue();
}
}
if (vmNameOnVcenter.equals(vmVcenterName)) {
if (vmInternalCSName != null && !vmInternalCSName.isEmpty() && !vmNameInCS.equals(vmInternalCSName)) {
return (new VirtualMachineMO(_context, oc.getObj()));
}
}
}
}
}
return null;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class ClusterMO method findDatastoreByExportPath.
@Override
public ManagedObjectReference findDatastoreByExportPath(String exportPath) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - findDatastoreByExportPath(). target MOR: " + _mor.getValue() + ", exportPath: " + exportPath);
ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] { "info" });
if (ocs != null && ocs.length > 0) {
for (ObjectContent oc : ocs) {
DatastoreInfo dsInfo = (DatastoreInfo) oc.getPropSet().get(0).getVal();
if (dsInfo != null && dsInfo instanceof NasDatastoreInfo) {
NasDatastoreInfo info = (NasDatastoreInfo) dsInfo;
if (info != null) {
String vmwareUrl = info.getUrl();
if (vmwareUrl.charAt(vmwareUrl.length() - 1) == '/')
vmwareUrl = vmwareUrl.substring(0, vmwareUrl.length() - 1);
URI uri = new URI(vmwareUrl);
if (uri.getPath().equals("/" + exportPath)) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - findDatastoreByExportPath() done(successfully)");
return oc.getObj();
}
}
}
}
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - findDatastoreByExportPath() done(failed)");
return null;
}
use of com.vmware.vim25.ObjectContent in project cloudstack by apache.
the class HostMO method getDatastoreMountsOnHost.
public List<Pair<ManagedObjectReference, String>> getDatastoreMountsOnHost() throws Exception {
List<Pair<ManagedObjectReference, String>> mounts = new ArrayList<Pair<ManagedObjectReference, String>>();
ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] { String.format("host[\"%s\"].mountInfo.path", _mor.getValue()) });
if (ocs != null) {
for (ObjectContent oc : ocs) {
Pair<ManagedObjectReference, String> mount = new Pair<ManagedObjectReference, String>(oc.getObj(), oc.getPropSet().get(0).getVal().toString());
mounts.add(mount);
}
}
return mounts;
}
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;
}
Aggregations