/** * Retrieve contents for a single object based on the property collector * registered with the service. * * @param collector * Property collector registered with service * @param mobj * Managed Object Reference to get contents for * @param properties * names of properties of object to retrieve * * @return retrieved object contents * @throws RuntimeFaultFaultMsg * @throws InvalidPropertyFaultMsg */ private ObjectContent[] getObjectProperties(ManagedObjectReference mobj, String[] properties) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { if (mobj == null) { return null; } PropertyFilterSpec spec = new PropertyFilterSpec(); spec.getPropSet().add(new PropertySpec()); if ((properties == null || properties.length == 0)) { spec.getPropSet().get(0).setAll(Boolean.TRUE); } else { spec.getPropSet().get(0).setAll(Boolean.FALSE); } spec.getPropSet().get(0).setType(mobj.getType()); spec.getPropSet().get(0).getPathSet().addAll(Arrays.asList(properties)); spec.getObjectSet().add(new ObjectSpec()); spec.getObjectSet().get(0).setObj(mobj); spec.getObjectSet().get(0).setSkip(Boolean.FALSE); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(spec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); return listobjcont.toArray(new ObjectContent[listobjcont.size()]); }
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 List<ObjectContent> getDatastorePropertiesOnHostDatastoreSystem(String[] propertyPaths) throws Exception { PropertySpec pSpec = new PropertySpec(); pSpec.setType("Datastore"); pSpec.getPathSet().addAll(Arrays.asList(propertyPaths)); TraversalSpec hostDsSys2DatastoreTraversal = new TraversalSpec(); hostDsSys2DatastoreTraversal.setType("HostDatastoreSystem"); hostDsSys2DatastoreTraversal.setPath("datastore"); hostDsSys2DatastoreTraversal.setName("hostDsSys2DatastoreTraversal"); ObjectSpec oSpec = new ObjectSpec(); oSpec.setObj(_mor); oSpec.setSkip(Boolean.TRUE); oSpec.getSelectSet().add(hostDsSys2DatastoreTraversal); PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.getPropSet().add(pSpec); pfSpec.getObjectSet().add(oSpec); List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>(); pfSpecArr.add(pfSpec); return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr); }
public List<Pair<ManagedObjectReference, String>> getLocalDatastoreOnHost() throws Exception { List<Pair<ManagedObjectReference, String>> dsList = new ArrayList<Pair<ManagedObjectReference, String>>(); ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name", "summary"}); if (ocs != null) { for (ObjectContent oc : ocs) { DatastoreSummary dsSummary = (DatastoreSummary)VmwareHelper.getPropValue(oc, "summary"); if (dsSummary.isMultipleHostAccess() == false && dsSummary.isAccessible() && dsSummary.getType().equalsIgnoreCase("vmfs")) { ManagedObjectReference morDs = oc.getObj(); String name = (String)VmwareHelper.getPropValue(oc, "name"); if (!name.startsWith("-iqn.") && !name.startsWith("_iqn.")) { dsList.add(new Pair<ManagedObjectReference, String>(morDs, name)); } } } } return dsList; }
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; }
public List<ObjectContent> getDatastorePropertiesOnDatacenter(String[] propertyPaths) throws Exception { PropertySpec pSpec = new PropertySpec(); pSpec.setType("Datastore"); pSpec.getPathSet().addAll(Arrays.asList(propertyPaths)); TraversalSpec dc2DatastoreTraversal = new TraversalSpec(); dc2DatastoreTraversal.setType("Datacenter"); dc2DatastoreTraversal.setPath("datastore"); dc2DatastoreTraversal.setName("dc2DatastoreTraversal"); ObjectSpec oSpec = new ObjectSpec(); oSpec.setObj(_mor); oSpec.setSkip(Boolean.TRUE); oSpec.getSelectSet().add(dc2DatastoreTraversal); PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.getPropSet().add(pSpec); pfSpec.getObjectSet().add(oSpec); List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>(); pfSpecArr.add(pfSpec); return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr); }
@Override public String getHyperHostDefaultGateway() throws Exception { ObjectContent[] ocs = getHostPropertiesOnCluster(new String[] {"config.network.routeTableInfo.ipRoute"}); if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { ArrayOfHostIpRouteEntry entries = (ArrayOfHostIpRouteEntry)oc.getPropSet().get(0).getVal(); if (entries != null) { for (HostIpRouteEntry entry : entries.getHostIpRouteEntry()) { if (entry.getNetwork().equalsIgnoreCase("0.0.0.0")) return entry.getGateway(); } } } } throw new Exception("Could not find host default gateway, host is not properly configured?"); }
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); }
private VmwareHypervisorHost getTargetHyperHost(DatacenterMO dcMo, String destIp) throws Exception { VmwareManager mgr = dcMo.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); List<ObjectContent> ocs = dcMo.getHostPropertiesOnDatacenterHostFolder(new String[] {"name", "parent"}); if (ocs != null && ocs.size() > 0) { for (ObjectContent oc : ocs) { HostMO hostMo = new HostMO(dcMo.getContext(), oc.getObj()); VmwareHypervisorHostNetworkSummary netSummary = hostMo.getHyperHostNetworkSummary(mgr.getManagementPortGroupByHost(hostMo)); if (destIp.equalsIgnoreCase(netSummary.getHostIp())) { return new HostMO(dcMo.getContext(), oc.getObj()); } } } throw new Exception("Unable to locate dest host by " + destIp); }
public List<DynamicProperty> getDynamicProperty(ManagedObjectReference mor, String[] propertyNames) throws Exception { ObjectContent[] objContent = getObjectProperties(mor, propertyNames); if (objContent != null) { return objContent[0].getPropSet(); } return null; }
/** * Uses the new RetrievePropertiesEx method to emulate the now deprecated * RetrieveProperties method * * @param filterSpecs * @return list of object content * @throws RuntimeFaultFaultMsg * @throws InvalidPropertyFaultMsg * @throws Exception */ private List<ObjectContent> retrievePropertiesAllObjects( List<PropertyFilterSpec> filterSpecs) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { RetrieveOptions retrieveOptions = new RetrieveOptions(); ManagedObjectReference collector = serviceContent .getPropertyCollector(); List<ObjectContent> contents = new ArrayList<ObjectContent>(); RetrieveResult results = vimPort.retrievePropertiesEx(collector, filterSpecs, retrieveOptions); if (results != null && results.getObjects() != null && !results.getObjects().isEmpty()) { contents.addAll(results.getObjects()); } String token = null; if (results != null && results.getToken() != null) { token = results.getToken(); } while (token != null && token.length() > 0) { results = vimPort.continueRetrievePropertiesEx(collector, token); token = null; if (results != null) { token = results.getToken(); if (results.getObjects() != null && !results.getObjects().isEmpty()) { contents.addAll(results.getObjects()); } } } return contents; }
protected ComputeResourceOverlay(ObjectContent cont) { super(cont); String type = getId().getType(); if (!type.equals(VimNames.TYPE_COMPUTE_RESOURCE) && !type.equals(VimNames.TYPE_CLUSTER_COMPUTE_RESOURCE)) { String msg = String.format("Cannot overlay type '%s' on top of %s", type, VimUtils .convertMoRefToString(getId())); throw new IllegalArgumentException(msg); } }
protected AbstractOverlay(ObjectContent cont) { this(cont.getObj(), new HashMap<>()); for (DynamicProperty dp : cont.getPropSet()) { this.props.put(dp.getName(), dp.getVal()); } }
public NetworkOverlay(ObjectContent cont) { super(cont); String type = getId().getType(); if (!VimUtils.isNetwork(cont.getObj())) { String msg = String.format("Cannot overlay type '%s' on top of %s", type, VimUtils .convertMoRefToString(getId())); throw new IllegalArgumentException(msg); } }
private void processAllTemplates(Set<String> oldImages, String endpointLink, String taskLink, EnumerationClient client, List<String> tenantLinks) throws RuntimeFaultFaultMsg { PropertyFilterSpec spec = client.createVmFilterSpec(client.getDatacenter()); for (List<ObjectContent> page : client.retrieveObjects(spec)) { Phaser phaser = new Phaser(1); for (ObjectContent oc : page) { if (!VimUtils.isVirtualMachine(oc.getObj())) { continue; } VmOverlay vm = new VmOverlay(oc); if (!vm.isTemplate()) { continue; } ImageState state = makeImageFromTemplate(vm); state.documentSelfLink = buildStableImageLink(endpointLink, state.id); state.endpointLink = endpointLink; state.tenantLinks = tenantLinks; state.regionId = VimUtils.convertMoRefToString(client.getDatacenter()); oldImages.remove(state.documentSelfLink); phaser.register(); Operation.createPost(PhotonModelUriUtils.createInventoryUri(getHost(), ImageService.FACTORY_LINK)) .setBody(state) .setCompletion((o, e) -> phaser.arrive()) .sendWith(this); } phaser.arriveAndAwaitAdvance(); } }
@Before public void setup() { ObjectContent cont = new ObjectContent(); ManagedObjectReference ref = new ManagedObjectReference(); ref.setType(VimNames.TYPE_VM); ref.setValue("vm-123"); cont.setObj(ref); Map<String, Object> props = new HashMap<>(); ArrayOfGuestNicInfo arrayOfGuestNicInfo = new ArrayOfGuestNicInfo(); List<GuestNicInfo> listGuestNicInfo = arrayOfGuestNicInfo.getGuestNicInfo(); GuestNicInfo nic1 = new GuestNicInfo(); List<String> ipsNic1 = nic1.getIpAddress(); String mac1Address = "00:50:56:8b:54:bd"; String mac2Address = "98:87:fd:9e:ed:6d"; nic1.setMacAddress(mac1Address); ipsNic1.add("192.168.1.10"); ipsNic1.add("192.168.1.11"); GuestNicInfo nic2 = new GuestNicInfo(); List<String> ipsNic2 = nic2.getIpAddress(); nic2.setMacAddress(mac2Address); ipsNic2.add("10.10.10.20"); listGuestNicInfo.add(nic1); listGuestNicInfo.add(nic2); props.put(vm_guest_net, arrayOfGuestNicInfo); this.overlay = new VmOverlay(ref, props); }
@Test public void test() throws Exception { String url = System.getProperty(TestProperties.VC_URL); if (url == null) { return; } String username = System.getProperty(TestProperties.VC_USERNAME); String password = System.getProperty(TestProperties.VC_PASSWORD); String datacenter = System.getProperty(TestProperties.VC_DATECENTER_ID); ManagedObjectReference datacenterMoRef = VimUtils.convertStringToMoRef(datacenter); BasicConnection conn = new BasicConnection(); conn.setURI(URI.create(url)); conn.setUsername(username); conn.setPassword(password); conn.setIgnoreSslErrors(true); conn.setRequestTimeout(30, TimeUnit.SECONDS); conn.connect(); ComputeStateWithDescription parent = new ComputeStateWithDescription(); ComputeDescription desc = new ComputeDescription(); parent.description = desc; EnumerationClient client = new EnumerationClient(conn, parent); PropertyFilterSpec spec = client.createResourcesFilterSpec(); for (List<ObjectContent> page : client.retrieveObjects(spec)) { for (ObjectContent cont : page) { this.logger.info(VimUtils.convertMoRefToString(cont.getObj())); } } }
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 List<ObjectContent> ancestrySet(ManagedObjectReference ref) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ObjectSpec ospec = new ObjectSpec(); ospec.setObj(ref); ospec.setSkip(false); TraversalSpec tspec = new TraversalSpec(); tspec.setSkip(false); tspec.setPath("parent"); tspec.setType("ManagedEntity"); tspec.setName("traverseParent"); SelectionSpec selSpec = new SelectionSpec(); selSpec.setName("traverseParent"); tspec.getSelectSet().add(selSpec); ospec.getSelectSet().add(tspec); PropertySpec pspec = new PropertySpec(); pspec.setType("ManagedEntity"); pspec.getPathSet().add("name"); pspec.getPathSet().add("parent"); PropertyFilterSpec filter = new PropertyFilterSpec(); filter.getObjectSet().add(ospec); filter.getPropSet().add(pspec); return this.connection.getVimPort() .retrieveProperties(this.connection.getServiceContent().getPropertyCollector(), Collections.singletonList(filter)); }
private List<Element> covertObjectContentToElements(List<ObjectContent> objectContents) throws FinderException { List<Element> res = new ArrayList<>(); for (ObjectContent oc : objectContents) { res.add(Element.make(oc.getObj(), getName(oc), this.prefix)); } return res; }
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()); }
private String buildPathToParent(ManagedObjectReference ref, List<ObjectContent> ocs) { ObjectContent parent = findByParent(ref, ocs); ObjectContent self = findByRef(ref, ocs); if (parent == null) { return "/" + prop(self, VimNames.PROPERTY_NAME); } else { return buildPathToParent(parent.getObj(), ocs) + "/" + prop(self, VimNames.PROPERTY_NAME); } }
private ObjectContent findByRef(ManagedObjectReference ref, List<ObjectContent> ocs) { if (ref == null) { return null; } for (ObjectContent oc : ocs) { ManagedObjectReference obj = oc.getObj(); if (Objects.equals(ref.getValue(), obj.getValue())) { return oc; } } return null; }
private ObjectContent findByParent(ManagedObjectReference ref, List<ObjectContent> ocs) { if (ref == null) { return null; } for (ObjectContent oc : ocs) { ManagedObjectReference obj = oc.getObj(); if (Objects.equals(ref.getValue(), obj.getValue())) { return findByRef(prop(oc, VimNames.PROPERTY_PARENT), ocs); } } return null; }
public static String populate(final RetrieveResult rslts, final List<ObjectContent> listobjcontent) { String token = null; if (rslts != null) { token = rslts.getToken(); listobjcontent.addAll(rslts.getObjects()); } return token; }
/** * 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; }
/** * Uses the new RetrievePropertiesEx method to emulate the now deprecated * RetrieveProperties method. * * @param listpfs * @return list of object content * @throws Exception */ public static List<ObjectContent> retrievePropertiesAllObjects( VimPortType vimPort, ManagedObjectReference propCollectorRef, List<PropertyFilterSpec> listpfs) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { RetrieveOptions propObjectRetrieveOpts = new RetrieveOptions(); List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>(); RetrieveResult rslts = vimPort.retrievePropertiesEx(propCollectorRef, listpfs, propObjectRetrieveOpts); if (rslts != null && rslts.getObjects() != null && !rslts.getObjects().isEmpty()) { listobjcontent.addAll(rslts.getObjects()); } String token = null; if (rslts != null && rslts.getToken() != null) { token = rslts.getToken(); } while (token != null && !token.isEmpty()) { rslts = vimPort.continueRetrievePropertiesEx(propCollectorRef, token); token = null; if (rslts != null) { token = rslts.getToken(); if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) { listobjcontent.addAll(rslts.getObjects()); } } } return listobjcontent; }
protected ObjectContent retrieveObjectProperties(String[] properties) { ObjectSpec oSpec = PropertyCollectorUtil.creatObjectSpec( getMOR(), Boolean.FALSE, null); PropertySpec pSpec = PropertyCollectorUtil.createPropertySpec( getMOR().getType(), properties == null || properties.length == 0, //if true, all props of this obj are to be read regardless of propName properties); PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.setObjectSet(new ObjectSpec[] { oSpec }); pfSpec.setPropSet(new PropertySpec[] { pSpec }); PropertyCollector pc = getServerConnection().getServiceInstance().getPropertyCollector(); ObjectContent[] objs; try { objs = pc.retrieveProperties(new PropertyFilterSpec[] { pfSpec }); } catch(Exception e) { throw new RuntimeException(e); } if (objs == null || objs[0]==null) return null; else return objs[0]; }
/** * initClusterHostMap is a self recursive method for generating VM/ESX to Cluster Hash Map. * In the first iteration it gathers all clusters and in consecutive calls for each cluster it updates Hash Map. * The logic here is use ComputeResource Entity as a base for gathering all virtual machines and ESX Hosts. * As part of configurations, GraphiteReceiver invokes this method at regular intervals (configured) and during runtime * if VM/ESX does not exist in the hash map. */ public static boolean initClusterHostMap(String clusterName, ManagedObjectReference rootFolder, ExecutionContext context, Map<String,String> clusterMap){ try { if(clusterName == null){ clusterMap.clear(); } VimConnection connection = context.getConnection(); RetrieveResult retrieveResult = getRetrieveResult(clusterName, connection, rootFolder); while((retrieveResult != null) && (retrieveResult.getObjects() != null) && (retrieveResult.getObjects().size() > 0)){ String token = retrieveResult.getToken(); for(ObjectContent objectContent : retrieveResult.getObjects()){ List<DynamicProperty> dynamicProperties = objectContent.getPropSet(); if(clusterName != null){ String dpsGet = String.valueOf(dynamicProperties.get(0).getVal()); clusterMap.put(dpsGet.replace(" ", "_"), clusterName.replace(" ", "_")); } else { initClusterHostMap((String) (dynamicProperties.get(0).getVal()), objectContent.getObj(), context, clusterMap); } } if (token == null) { return true; } retrieveResult = connection.getVimPort().continueRetrievePropertiesEx(connection.getPropertyCollector(), token); } return true; } catch(Exception e){ logger.fatal("Critical Error Detected."); logger.fatal(e.getLocalizedMessage()); return false; } }