use of com.vmware.vim25.DynamicProperty 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.DynamicProperty in project cloudstack by apache.
the class DatacenterMO method getDvSwitchMor.
public ManagedObjectReference getDvSwitchMor(ManagedObjectReference dvPortGroupMor) throws Exception {
String dvPortGroupKey = null;
ManagedObjectReference dvSwitchMor = null;
PropertySpec pSpec = new PropertySpec();
pSpec.setType("DistributedVirtualPortgroup");
pSpec.getPathSet().add("key");
pSpec.getPathSet().add("config.distributedVirtualSwitch");
TraversalSpec datacenter2DvPortGroupTraversal = new TraversalSpec();
datacenter2DvPortGroupTraversal.setType("Datacenter");
datacenter2DvPortGroupTraversal.setPath("network");
datacenter2DvPortGroupTraversal.setName("datacenter2DvPortgroupTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(datacenter2DvPortGroupTraversal);
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);
if (ocs != null) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
assert (props.size() == 2);
for (DynamicProperty prop : props) {
if (prop.getName().equals("key")) {
dvPortGroupKey = (String) prop.getVal();
} else {
dvSwitchMor = (ManagedObjectReference) prop.getVal();
}
}
if ((dvPortGroupKey != null) && dvPortGroupKey.equals(dvPortGroupMor.getValue())) {
return dvSwitchMor;
}
}
}
}
return null;
}
use of com.vmware.vim25.DynamicProperty 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.DynamicProperty 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.DynamicProperty in project cloudstack by apache.
the class HostMO method loadVmCache.
private void loadVmCache() throws Exception {
if (s_logger.isDebugEnabled())
s_logger.debug("load VM cache on host");
_vmCache.clear();
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 ?!");
}
// name is the name of the VM as it appears in vCenter. The CLOUD_VM_INTERNAL_NAME custom
// field value contains the name of the VM as it is maintained internally by cloudstack (i-x-y).
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name", "value[" + key + "]" });
if (ocs != null && ocs.length > 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();
} else if (prop.getName().startsWith("value[")) {
if (prop.getVal() != null)
vmInternalCSName = ((CustomFieldStringValue) prop.getVal()).getValue();
}
}
String vmName = null;
if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName)) {
vmName = vmInternalCSName;
} else {
vmName = vmVcenterName;
}
if (s_logger.isTraceEnabled())
s_logger.trace("put " + vmName + " into host cache");
_vmCache.put(vmName, new VirtualMachineMO(_context, oc.getObj()));
}
}
}
}
Aggregations