/** * Adds a host instance to the inventory based on given properties. * * @return the created host instance */ public VMwareHost addHostSystem(List<DynamicProperty> properties) { if (properties == null || properties.size() == 0) { return null; } VMwareHost result = new VMwareHost(this); for (DynamicProperty dp : properties) { String key = dp.getName(); if ("name".equals(key) && dp.getVal() != null) { result.setName(dp.getVal().toString()); } else if ("summary.hardware.memorySize".equals(key) && dp.getVal() != null) { result.setMemorySizeMB(VMwareValue .fromBytes(Long.parseLong(dp.getVal().toString())) .getValue(Unit.MB)); } else if ("summary.hardware.numCpuCores".equals(key) && dp.getVal() != null) { result.setCpuCores(Integer.parseInt(dp.getVal().toString())); } } hostsSystems.put(result.getName(), result); return result; }
@Test public void testAddDatastore() { List<DynamicProperty> properties = createDataStoreProperties("ds1", "100", "50"); VMwareStorage storage = inventory.addStorage("host", properties); assertNotNull(storage); assertEquals("ds1", storage.getName()); assertTrue(0.5 == storage.getLevel()); storage = inventory.getStorage("ds1"); assertNotNull(storage); assertEquals("ds1", storage.getName()); assertTrue(0.5 == storage.getLevel()); assertNull(inventory.getStorage("ds2")); }
@Test public void testAddHostSystem() { // given List<DynamicProperty> properties = createHostSystemProperties("host1", "512", "2"); // when VMwareHost host = inventory.addHostSystem(properties); // then assertEquals("host1", host.getName()); assertTrue(512 == host.getMemorySizeMB()); assertTrue(2 == host.getCpuCores()); host = inventory.getHost("host1"); assertEquals("host1", host.getName()); assertTrue(512 == host.getMemorySizeMB()); assertTrue(2 == host.getCpuCores()); assertTrue(host .getBalancer() instanceof DynamicEquipartitionStorageBalancer); }
@Test public void testAddVM() throws Exception { List<DynamicProperty> properties = createVMProperties("vm1", "512", "2", "host1"); VMwareVirtualMachine vm = inventory.addVirtualMachine(properties, serviceUtil); assertNotNull(vm); assertEquals("vm1", vm.getName()); assertTrue(vm.getMemorySizeMB() == 512); assertTrue(vm.getNumCpu() == 2); inventory.addVirtualMachine(properties, serviceUtil); Mockito.verify(serviceUtil, Mockito.times(1)).getDynamicProperty( Matchers.any(ManagedObjectReference.class), Matchers.anyString()); }
@Test public void testInitialize() throws Exception { Mockito.when(serviceUtil.getDynamicProperty( Matchers.any(ManagedObjectReference.class), Matchers.anyString())).thenReturn("hostname") .thenReturn("other"); List<DynamicProperty> properties = createVMProperties("vm1", "512", "2", "hostname"); inventory.addVirtualMachine(properties, serviceUtil); properties = createVMProperties("vm2", "4096", "4", "hostname"); inventory.addVirtualMachine(properties, serviceUtil); properties = createVMProperties("vm3", "2048", "1", "otherhost"); inventory.addVirtualMachine(properties, serviceUtil); properties = createHostSystemProperties("hostname", "8192", "8"); VMwareHost host = inventory.addHostSystem(properties); inventory.initialize(); assertNotNull(host); assertEquals(6, host.getAllocatedCPUs()); }
private void resultsToTgtMorefMap(RetrieveResult results, Map<String, ManagedObjectReference> tgtMoref) { List<ObjectContent> oCont = (results != null) ? results.getObjects() : null; if (oCont != null) { for (ObjectContent oc : oCont) { ManagedObjectReference mr = oc.getObj(); String entityNm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { entityNm = (String) dp.getVal(); } } tgtMoref.put(entityNm, mr); } } }
/** * Returns all the MOREFs of the specified type that are present under the * container * * @param container {@link ManagedObjectReference} of the container to begin the * search from * @param morefType Type of the managed entity that needs to be searched * @param morefProperties Array of properties to be fetched for the moref * @return Map of MOREF and Map of name value pair of properties requested of * the managed objects present. If none exist then empty Map is * returned * @throws InvalidPropertyFaultMsg * @throws RuntimeFaultFaultMsg */ public Map<ManagedObjectReference, Map<String, Object>> inContainerByType( ManagedObjectReference container, String morefType, String[] morefProperties, RetrieveOptions retrieveOptions) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { List<ObjectContent> oCont = containerViewByType(container, morefType, retrieveOptions, morefProperties).getObjects(); Map<ManagedObjectReference, Map<String, Object>> tgtMoref = new HashMap<ManagedObjectReference, Map<String, Object>>(); if (oCont != null) { for (ObjectContent oc : oCont) { Map<String, Object> propMap = new HashMap<String, Object>(); List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { propMap.put(dp.getName(), dp.getVal()); } } tgtMoref.put(oc.getObj(), propMap); } } return tgtMoref; }
public static String populate(final RetrieveResult rslts, final Map<String, ManagedObjectReference> tgtMoref) { String token = null; if (rslts != null) { token = rslts.getToken(); for (ObjectContent oc : rslts.getObjects()) { ManagedObjectReference mr = oc.getObj(); String entityNm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { entityNm = (String) dp.getVal(); } } tgtMoref.put(entityNm, mr); } } return token; }
/** * @param propertyName The property name of current managed object * @return it will return either an array of related data objects, or an data object itself. * ManagedObjectReference objects are data objects!!! * @throws RemoteException * @throws RuntimeFault * @throws InvalidProperty * @ */ protected Object getCurrentProperty(String propertyName) { ObjectContent objContent = retrieveObjectProperties(new String[] { propertyName }); Object propertyValue = null; if (objContent != null) { DynamicProperty[] dynaProps = objContent.getPropSet(); if ((dynaProps != null) && (dynaProps[0]!= null)) { propertyValue = PropertyCollectorUtil.convertProperty(dynaProps[0].getVal()); } } return propertyValue; }
public ManagedObjectReference getMorHost(String hostname, ConnectionResources connectionResources, ManagedObjectReference vmMor) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ManagedObjectReference host = null; if (isNotBlank(hostname)) { host = getManagedObjectReference(hostname, connectionResources, ManagedObjectType.HOST_SYSTEM.getValue(), ErrorMessages.HOST_NOT_FOUND); } else if (StringUtils.isBlank(hostname) && vmMor != null) { ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); host = virtualMachineSummary.getRuntime().getHost(); break; } break; } } else { host = connectionResources.getHostMor(); } return host; }
/** * Method to retrieve properties of a {@link ManagedObjectReference} * * @param entityMor {@link ManagedObjectReference} of the entity * @param props Array of properties to be looked up * @return Map of the property name and its corresponding value * @throws InvalidPropertyFaultMsg If a property does not exist * @throws RuntimeFaultFaultMsg */ public Map<String, Object> entityProps(ManagedObjectReference entityMor, String[] props) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { final HashMap<String, Object> retVal = new HashMap<>(); // Create PropertyFilterSpec using the PropertySpec and ObjectPec PropertyFilterSpec[] propertyFilterSpecs = {new PropertyFilterSpecBuilder().propSet( // Create Property Spec new PropertySpecBuilder().all(false).type(entityMor.getType()).pathSet(props)) .objectSet( // Now create Object Spec new ObjectSpecBuilder().obj(entityMor))}; List<ObjectContent> objCont = vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(), Arrays.asList(propertyFilterSpecs), new RetrieveOptions()).getObjects(); if (objCont != null) { for (ObjectContent oc : objCont) { List<DynamicProperty> dps = oc.getPropSet(); for (DynamicProperty dp : dps) { retVal.put(dp.getName(), dp.getVal()); } } } return retVal; }
private static String populate(final RetrieveResult results, final Map<String, ManagedObjectReference> tgtMoref) { String token = null; if (results != null) { token = results.getToken(); for (ObjectContent oc : results.getObjects()) { ManagedObjectReference mr = oc.getObj(); String entityNm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { entityNm = (String) dp.getVal(); } } tgtMoref.put(entityNm, mr); } } return token; }
public ManagedObjectReference findDatastore(String name) throws Exception { // added Apache CloudStack specific name convention, we will use custom field "cloud.uuid" as datastore name as well CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager()); int key = cfmMo.getCustomFieldKey("Datastore", CustomFieldConstants.CLOUD_UUID); assert (key != 0); List<ObjectContent> ocs = getDatastorePropertiesOnHostDatastoreSystem(new String[] {"name", String.format("value[%d]", key)}); if (ocs != null) { for (ObjectContent oc : ocs) { if (oc.getPropSet().get(0).getVal().equals(name)) return oc.getObj(); if (oc.getPropSet().size() > 1) { DynamicProperty prop = oc.getPropSet().get(1); if (prop != null && prop.getVal() != null) { if (prop.getVal() instanceof CustomFieldStringValue) { String val = ((CustomFieldStringValue)prop.getVal()).getValue(); if (val.equalsIgnoreCase(name)) return oc.getObj(); } } } } } return null; }
public static VirtualMachineMO findVmFromObjectContent(VmwareContext context, ObjectContent[] ocs, String name, String instanceNameCustomField) { if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { String vmNameInvCenter = null; String vmInternalCSName = null; List<DynamicProperty> objProps = oc.getPropSet(); if (objProps != null) { for (DynamicProperty objProp : objProps) { if (objProp.getName().equals("name")) { vmNameInvCenter = (String)objProp.getVal(); } else if (objProp.getName().contains(instanceNameCustomField)) { if (objProp.getVal() != null) vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue(); } if ((vmNameInvCenter != null && name.equalsIgnoreCase(vmNameInvCenter)) || (vmInternalCSName != null && name.equalsIgnoreCase(vmInternalCSName))) { VirtualMachineMO vmMo = new VirtualMachineMO(context, oc.getObj()); return vmMo; } } } } } return null; }
/** * Adds a storage instance to the inventory based on given properties. * * @return the created storage instance */ public VMwareStorage addStorage(String host, List<DynamicProperty> properties) { if (properties == null || properties.size() == 0) { return null; } VMwareStorage result = new VMwareStorage(); for (DynamicProperty dp : properties) { String key = dp.getName(); if ("summary.name".equals(key) && dp.getVal() != null) { result.setName(dp.getVal().toString()); } else if ("summary.capacity".equals(key) && dp.getVal() != null) { result.setCapacity(VMwareValue .fromBytes(Long.parseLong(dp.getVal().toString()))); } else if ("summary.freeSpace".equals(key) && dp.getVal() != null) { result.setFreeStorage(VMwareValue .fromBytes(Long.parseLong(dp.getVal().toString()))); } } storages.put(result.getName(), result); if (storageByHost.containsKey(host)) { storageByHost.get(host).add(result); } else { List<VMwareStorage> storage = new ArrayList<VMwareStorage>(); storage.add(result); storageByHost.put(host, storage); } return result; }
public List<DynamicProperty> getDynamicProperty(ManagedObjectReference mor, String[] propertyNames) throws Exception { ObjectContent[] objContent = getObjectProperties(mor, propertyNames); if (objContent != null) { return objContent[0].getPropSet(); } return null; }
@Test public void testAddDatastore_cap0() { List<DynamicProperty> properties = createDataStoreProperties("ds1", "0", "50"); VMwareStorage storage = inventory.addStorage("host", properties); assertNotNull(storage); assertTrue(1 == storage.getLevel()); properties = createDataStoreProperties("ds1", "1", "-1"); storage = inventory.addStorage("host", properties); assertNotNull(storage); assertTrue(1 == storage.getLevel()); }
@Test public void testAddDatastore_nullValueProps() { List<DynamicProperty> properties = createNullValueProperties( "summary.name", "summary.capacity", "summary.freeSpace"); VMwareStorage storage = inventory.addStorage("host", properties); assertNotNull(storage); assertNull(storage.getName()); }
@Test public void testAddHostSystem_nullValueProps() { List<DynamicProperty> properties = createNullValueProperties("name", "summary.hardware.memorySize", "summary.hardware.numCpuCores"); VMwareHost host = inventory.addHostSystem(properties); assertNotNull(host); assertNull(host.getName()); }
@Test public void testAddVM_nullValueProps() throws Exception { List<DynamicProperty> properties = createNullValueProperties("name", "summary.config.memorySizeMB", "summary.config.numCpu", "runtime.host"); VMwareVirtualMachine vm = inventory.addVirtualMachine(properties, serviceUtil); assertNotNull(vm); assertNull(vm.getName()); }
@Test public void testAddVM_nullHostSystem() throws Exception { Mockito.when(serviceUtil.getDynamicProperty( Matchers.any(ManagedObjectReference.class), Matchers.anyString())).thenReturn(null); List<DynamicProperty> properties = createVMProperties("vm", "512", "4", "host1"); VMwareVirtualMachine vm = inventory.addVirtualMachine(properties, serviceUtil); assertNotNull(vm); assertNull(vm.getHostName()); }
private static List<DynamicProperty> createNullValueProperties( String... properties) { List<DynamicProperty> result = new ArrayList<DynamicProperty>(); for (int i = 0; i < properties.length; i++) { DynamicProperty p = new DynamicProperty(); p.setName(properties[i]); result.add(p); } return result; }
public static List<DynamicProperty> createVMProperties(String name, String memory, String cpu, String host) { ManagedObjectReference mor = new ManagedObjectReference(); mor.setValue(host); return createProperties("name", name, "summary.config.memorySizeMB", memory, "summary.config.numCpu", cpu, "runtime.host", mor); }
protected AbstractOverlay(ObjectContent cont) { this(cont.getObj(), new HashMap<>()); for (DynamicProperty dp : cont.getPropSet()) { this.props.put(dp.getName(), dp.getVal()); } }
private ManagedObjectReference getParent(ObjectContent cont) { for (DynamicProperty dp : cont.getPropSet()) { if (dp.getName().equals("parent")) { return (ManagedObjectReference) dp.getVal(); } } // root object return null; }
private String getName(ObjectContent cont) throws FinderException { for (DynamicProperty dp : cont.getPropSet()) { if (dp.getName().equals("name")) { return (String) dp.getVal(); } } // probably bad spec throw new FinderException("Name property not found/fetched for " + cont.getObj().getType()); }
private String getName(ObjectContent oc) throws FinderException { for (DynamicProperty dp : oc.getPropSet()) { if (dp.getName().equals("name")) { return (String) dp.getVal(); } } throw new FinderException("No name fetched for " + oc.getObj()); }
/** * Method to retrieve properties of a {@link ManagedObjectReference} * * @param entityMor {@link ManagedObjectReference} of the entity * @param props Array of properties to be looked up * @return Map of the property name and its corresponding value * @throws InvalidPropertyFaultMsg If a property does not exist * @throws RuntimeFaultFaultMsg */ public Map<String, Object> entityProps( ManagedObjectReference entityMor, String... props) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { init(); final HashMap<String, Object> retVal = new HashMap<>(); // Create PropertyFilterSpec using the PropertySpec and ObjectPec PropertyFilterSpec[] propertyFilterSpecs = { new PropertyFilterSpecBuilder() .propSet( // Create Property Spec new PropertySpecBuilder() .all(Boolean.FALSE) .type(entityMor.getType()) .pathSet(props) ) .objectSet( // Now create Object Spec new ObjectSpecBuilder() .obj(entityMor) ) }; List<ObjectContent> oCont = this.vimPort.retrievePropertiesEx(this.serviceContent.getPropertyCollector(), Arrays.asList(propertyFilterSpecs), new RetrieveOptions()).getObjects(); if (oCont != null) { for (ObjectContent oc : oCont) { List<DynamicProperty> dps = oc.getPropSet(); for (DynamicProperty dp : dps) { retVal.put(dp.getName(), dp.getVal()); } } } return retVal; }
/** * Get the required properties of the specified object. * * @param vimPort * @param serviceContent * @param moRef * @param type * @param properties * @return * @throws RuntimeFaultFaultMsg * @throws InvalidPropertyFaultMsg */ public static List<DynamicProperty> getProperties(VimPortType vimPort, ServiceContent serviceContent, ManagedObjectReference moRef, String type, List<String> properties) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg { // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(false); propertySpec.setType(type); propertySpec.getPathSet().addAll(properties); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(moRef); objectSpec.setSkip(false); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcontent = VimUtil .retrievePropertiesAllObjects(vimPort, serviceContent.getPropertyCollector(), listpfs); assert listobjcontent != null && listobjcontent.size() > 0; ObjectContent contentObj = listobjcontent.get(0); List<DynamicProperty> objList = contentObj.getPropSet(); return objList; }