void Watch(PropertyManager PM, String path) { ManagedObjectReference vm = cb.getConnection().Service.FindByInventoryPath( cb.getConnection().ServiceContent.searchIndex, path); if (vm == null) { System.Console.WriteLine("Virtual Machine located at path: " + path + " not found."); return; } // Create a FilterSpec PropertySpec pSpec = new PropertySpec(); pSpec.type = vm.type; pSpec.pathSet = new String[] { "guest", "summary.quickStats", "summary.runtime.powerState" }; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = vm; oSpec.skip = false; oSpec.skipSpecified = true; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; Console.WriteLine("Updates being displayed...Press Ctrl-Break to exit"); PM.Register(pfSpec, false, new PropertyFilterUpdateHandler(DisplayUpdates)); while (true) { System.Threading.Thread.Sleep(100); } }
private PropertyFilterSpec[] createPFSForRecentTasks( ManagedObjectReference taskManagerRef) { PropertySpec pSpec = new PropertySpec(); pSpec.all= false; pSpec.type="Task"; pSpec.pathSet= new String[] {"info.entity", "info.entityName", "info.name", "info.state", "info.cancelled", "info.error"}; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = taskManagerRef; oSpec.skip= false; oSpec.skipSpecified = true; TraversalSpec tSpec = new TraversalSpec(); tSpec.type="TaskManager"; tSpec.path="recentTask"; tSpec.skip= false; oSpec.selectSet=new SelectionSpec[]{tSpec}; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet=new PropertySpec[]{pSpec}; pfSpec.objectSet=new ObjectSpec[]{oSpec}; return new PropertyFilterSpec[]{pfSpec}; }
private PropertyFilterSpec createEventFilterSpec() { // Set up a PropertySpec to use the latestPage attribute // of the EventHistoryCollector PropertySpec propSpec = new PropertySpec(); propSpec.all = false; propSpec.pathSet=new String[] { "latestPage" }; propSpec.type =_eventHistoryCollector.type; // PropertySpecs are wrapped in a PropertySpec array PropertySpec[] propSpecAry = new PropertySpec[] { propSpec }; // Set up an ObjectSpec with the above PropertySpec for the // EventHistoryCollector we just created // as the Root or Starting Object to get Attributes for. ObjectSpec objSpec = new ObjectSpec(); objSpec.obj =_eventHistoryCollector; objSpec.skip = false; // Get Event objects in "latestPage" from "EventHistoryCollector" // and no "traversl" further, so, no SelectionSpec is specified objSpec.selectSet= new SelectionSpec[] { }; // ObjectSpecs are wrapped in an ObjectSpec array ObjectSpec[] objSpecAry = new ObjectSpec[] { objSpec }; PropertyFilterSpec spec = new PropertyFilterSpec(); spec.propSet= propSpecAry; spec.objectSet= objSpecAry; return spec; }
/** * Create Property Collector Filter to get names of all * ScheduledTasks the ScheduledTaskManager has. * * @return PropertyFilterSpec to get properties */ private PropertyFilterSpec createTaskPropertyFilterSpec() { // The traversal spec traverses the "scheduledTask" property of // ScheduledTaskManager to get names of ScheduledTask ManagedEntities // A Traversal Spec allows traversal into a ManagedObjects // using a single attribute of the managedObject TraversalSpec scheduledTaskTraversal = new TraversalSpec(); scheduledTaskTraversal.type=_scheduleManager.type; scheduledTaskTraversal.path= "scheduledTask"; // We want to get values of the scheduleTask property // of the scheduledTaskManager, which are the ScheduledTasks // so we set skip = false. scheduledTaskTraversal.skip= false; scheduledTaskTraversal.skipSpecified = true; // no further traversal needed once we get to scheduled task list scheduledTaskTraversal.selectSet=new SelectionSpec[] { }; scheduledTaskTraversal.name="scheduleManagerToScheduledTasks"; // Setup a PropertySpec to return names of Scheduled Tasks so // we can find the named ScheduleTask ManagedEntity to delete // Name is an attribute of ScheduledTaskInfo so // the path set will contain "info.name" PropertySpec propSpec = new PropertySpec(); propSpec.all= false; propSpec.allSpecified= true; propSpec.pathSet= new String[] { "info.name" }; propSpec.type="ScheduledTask"; // PropertySpecs are wrapped in a PropertySpec array // since we only have a propertySpec for the ScheduledTask, // the only values we will get back are names of scheduledTasks PropertySpec[] propSpecArray = new PropertySpec[] { propSpec }; // Create an Object Spec to specify the starting or root object // and the SelectionSpec to traverse to each ScheduledTask in the // array of scheduledTasks in the ScheduleManager ObjectSpec objSpec = new ObjectSpec(); objSpec.obj=_scheduleManager; objSpec.selectSet= new SelectionSpec[] { scheduledTaskTraversal } ; // Set skip = true so properties of ScheduledTaskManager // are not returned, and only values of info.name property of // each ScheduledTask is returned objSpec.skip = true; objSpec.skipSpecified= true; // ObjectSpecs used in PropertyFilterSpec are wrapped in an array ObjectSpec[] objSpecArray = new ObjectSpec[] { objSpec }; // Create the PropertyFilter spec with // ScheduledTaskManager as "root" object PropertyFilterSpec spec = new PropertyFilterSpec(); spec.propSet = propSpecArray; spec.objectSet= objSpecArray; return spec; }
/* * getProperties -- * * Retrieves the specified set of properties for the given managed object * reference into an array of result objects (returned in the same oder * as the property list). */ public static Object[] getProperties(ManagedObjectReference moRef, String[] properties) { // PropertySpec specifies what properties to // retrieve and from type of Managed Object PropertySpec pSpec = new PropertySpec(); pSpec.type = moRef.type; pSpec.pathSet = properties; // ObjectSpec specifies the starting object and // any TraversalSpecs used to specify other objects // for consideration ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = moRef; // PropertyFilterSpec is used to hold the ObjectSpec and // PropertySpec for the call PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; // retrieveProperties() returns the properties // selected from the PropertyFilterSpec ObjectContent[] ocs = new ObjectContent[20]; ocs = ecb._svcUtil.retrievePropertiesEx(_sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); // Return value, one object for each property specified Object[] ret = new Object[properties.Length]; if (ocs != null) { for (int i = 0; i < ocs.Length; ++i) { ObjectContent oc = ocs[i]; DynamicProperty[] dps = oc.propSet; if (dps != null) { for (int j = 0; j < dps.Length; ++j) { DynamicProperty dp = dps[j]; // find property path index for (int p = 0; p < ret.Length; ++p) { if (properties[p].Equals(dp.name)) { ret[p] = dp.val; } } } } } } return ret; }
private void getUpdates() { ManagedObjectReference vmRef = cb.getServiceUtil().GetDecendentMoRef(null,"VirtualMachine", cb.get_option("vmname")); if(vmRef == null) { Console.WriteLine("Virtual Machine " + cb.get_option("vmname") + " Not Found"); return; } String[][] typeInfo = { new String[]{"VirtualMachine", "name","summary.quickStats","runtime"} }; PropertySpec[] pSpecs = cb.getServiceUtil().BuildPropertySpecArray(typeInfo); ObjectSpec[] oSpecs = null; oSpecs = new ObjectSpec[]{new ObjectSpec()}; Boolean oneOnly = vmRef != null; oSpecs[0].obj = oneOnly ? vmRef : cb.getConnection().ServiceContent.rootFolder; oSpecs[0].skip = !oneOnly; if(!oneOnly) { SelectionSpec[] selectionSpecs = cb.getServiceUtil().buildFullTraversal(); oSpecs[0].selectSet = selectionSpecs; } PropertyFilterSpec pSpec = new PropertyFilterSpec(); pSpec.objectSet = new ObjectSpec[] { oSpecs[0] }; pSpec.propSet = new PropertySpec[] { pSpecs[0] }; ManagedObjectReference propColl = cb.getConnection().PropCol; ManagedObjectReference propFilter = cb.getConnection()._service.CreateFilter(propColl, pSpec, false); String version = ""; do { UpdateSet update = cb.getConnection()._service.CheckForUpdates(propColl, version); if(update != null && update.filterSet != null) { handleUpdate(update); version = update.version; } else { Console.WriteLine("No update is present!"); } Console.WriteLine(""); Console.WriteLine("Press <Enter> to check for updates"); Console.WriteLine("Enter 'exit' <Enter> to exit the program"); String line = Console.ReadLine(); if(line.Trim().Equals("exit")) break; }while(true); cb.getConnection()._service.DestroyPropertyFilter(propFilter); }
public static PropertyFilterSpec[] SwitchFilter(ManagedObjectReference mor) { PropertySpec prop = new PropertySpec { type = "VmwareDistributedVirtualSwitch", pathSet = new string[] { "config", "uuid", } }; ObjectSpec objectspec = new ObjectSpec { obj = mor, //_vms or vm-mor }; PropertyFilterSpec[] ret = new PropertyFilterSpec[] { new PropertyFilterSpec { propSet = new PropertySpec[] { prop }, objectSet = new ObjectSpec[] { objectspec } } }; return(ret); }
public static PropertyFilterSpec[] PortGroupFilter(ManagedObjectReference mor) { PropertySpec prop = new PropertySpec { type = "DistributedVirtualPortgroup", pathSet = new string[] { "config" } }; ObjectSpec objectspec = new ObjectSpec { obj = mor, //_vms or vm-mor }; PropertyFilterSpec[] ret = new PropertyFilterSpec[] { new PropertyFilterSpec { propSet = new PropertySpec[] { prop }, objectSet = new ObjectSpec[] { objectspec } } }; return(ret); }
public static PropertyFilterSpec[] NetworkSummaryFilter(ManagedObjectReference mor, string props) { PropertySpec prop = new PropertySpec { type = "Network", pathSet = props.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries) }; ObjectSpec objectspec = new ObjectSpec { obj = mor, //_vms or vm-mor }; PropertyFilterSpec[] ret = new PropertyFilterSpec[] { new PropertyFilterSpec { propSet = new PropertySpec[] { prop }, objectSet = new ObjectSpec[] { objectspec } } }; return(ret); }
private PropertyFilterSpec[] createPFSForRecentTasks( ManagedObjectReference taskManagerRef) { PropertySpec pSpec = new PropertySpec(); pSpec.all = false; pSpec.type = "Task"; pSpec.pathSet = new String[] { "info.entity", "info.entityName", "info.name", "info.state", "info.cancelled", "info.error" }; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = taskManagerRef; oSpec.skip = false; oSpec.skipSpecified = true; TraversalSpec tSpec = new TraversalSpec(); tSpec.type = "TaskManager"; tSpec.path = "recentTask"; tSpec.skip = false; oSpec.selectSet = new SelectionSpec[] { tSpec }; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; return(new PropertyFilterSpec[] { pfSpec }); }
public static PropertyFilterSpec[] DatastoreFilter(ManagedObjectReference mor) { PropertySpec prop = new PropertySpec { type = "Datastore", pathSet = new string[] { "browser", "capability", "summary" } }; ObjectSpec objectspec = new ObjectSpec { obj = mor, //_res selectSet = new SelectionSpec[] { new TraversalSpec { type = "ComputeResource", path = "datastore" } } }; return(new PropertyFilterSpec[] { new PropertyFilterSpec { propSet = new PropertySpec[] { prop }, objectSet = new ObjectSpec[] { objectspec } } }); }
public static PropertyFilterSpec[] InitFilter(ManagedObjectReference rootMOR) { var plan = new TraversalSpec { name = "FolderTraverseSpec", type = "Folder", path = "childEntity", selectSet = new SelectionSpec[] { new TraversalSpec() { type = "Datacenter", path = "hostFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "Datacenter", path = "networkFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "ComputeResource", path = "resourcePool", selectSet = new SelectionSpec[] { new TraversalSpec { type = "ResourcePool", path = "resourcePool" } } }, new TraversalSpec() { type = "ComputeResource", path = "host" } } }; var props = new PropertySpec[] { new PropertySpec { type = "Datacenter", pathSet = new string[] { "name", "parent", "vmFolder" } }, new PropertySpec { type = "ComputeResource", pathSet = new string[] { "name", "parent", "resourcePool", "host" } }, new PropertySpec { type = "HostSystem", pathSet = new string[] { "configManager" } }, new PropertySpec { type = "ResourcePool", pathSet = new string[] { "name", "parent", "resourcePool" } }, new PropertySpec { type = "DistributedVirtualSwitch", pathSet = new string[] { "name", "parent", "uuid" } }, new PropertySpec { type = "DistributedVirtualPortgroup", pathSet = new string[] { "name", "parent", "config" } } }; ObjectSpec objectspec = new ObjectSpec(); objectspec.obj = rootMOR; objectspec.selectSet = new SelectionSpec[] { plan }; PropertyFilterSpec filter = new PropertyFilterSpec(); filter.propSet = props; filter.objectSet = new ObjectSpec[] { objectspec }; return(new PropertyFilterSpec[] { filter }); }
private async Task <ObjectContent[]> LoadReferenceTree(VimPortTypeClient client) { var plan = new TraversalSpec { name = "FolderTraverseSpec", type = "Folder", path = "childEntity", selectSet = new SelectionSpec[] { new TraversalSpec() { type = "Datacenter", path = "hostFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "Datacenter", path = "networkFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "ComputeResource", path = "resourcePool", selectSet = new SelectionSpec[] { new TraversalSpec { type = "ResourcePool", path = "resourcePool" } } }, new TraversalSpec() { type = "ComputeResource", path = "host" }, new TraversalSpec() { type = "Folder", path = "childEntity" } } }; var props = new PropertySpec[] { new PropertySpec { type = "Datacenter", pathSet = new string[] { "name", "parent", "vmFolder" } }, new PropertySpec { type = "ComputeResource", pathSet = new string[] { "name", "parent", "resourcePool", "host" } }, new PropertySpec { type = "HostSystem", pathSet = new string[] { "configManager" } }, new PropertySpec { type = "ResourcePool", pathSet = new string[] { "name", "parent", "resourcePool" } }, new PropertySpec { type = "DistributedVirtualSwitch", pathSet = new string[] { "name", "parent", "uuid" } }, new PropertySpec { type = "DistributedVirtualPortgroup", pathSet = new string[] { "name", "parent", "config" } } }; ObjectSpec objectspec = new ObjectSpec(); objectspec.obj = _sic.rootFolder; objectspec.selectSet = new SelectionSpec[] { plan }; PropertyFilterSpec filter = new PropertyFilterSpec(); filter.propSet = props; filter.objectSet = new ObjectSpec[] { objectspec }; PropertyFilterSpec[] filters = new PropertyFilterSpec[] { filter }; RetrievePropertiesResponse response = await client.RetrievePropertiesAsync(_props, filters); return(response.returnval); }
private void getUpdates() { ManagedObjectReference vmRef = cb.getServiceUtil().GetDecendentMoRef(null, "VirtualMachine", cb.get_option("vmname")); if (vmRef == null) { Console.WriteLine("Virtual Machine " + cb.get_option("vmname") + " Not Found"); return; } String[][] typeInfo = { new String[] { "VirtualMachine", "name", "summary.quickStats", "runtime" } }; PropertySpec[] pSpecs = cb.getServiceUtil().BuildPropertySpecArray(typeInfo); ObjectSpec[] oSpecs = null; oSpecs = new ObjectSpec[] { new ObjectSpec() }; Boolean oneOnly = vmRef != null; oSpecs[0].obj = oneOnly ? vmRef : cb.getConnection().ServiceContent.rootFolder; oSpecs[0].skip = !oneOnly; if (!oneOnly) { SelectionSpec[] selectionSpecs = cb.getServiceUtil().buildFullTraversal(); oSpecs[0].selectSet = selectionSpecs; } PropertyFilterSpec pSpec = new PropertyFilterSpec(); pSpec.objectSet = new ObjectSpec[] { oSpecs[0] }; pSpec.propSet = new PropertySpec[] { pSpecs[0] }; ManagedObjectReference propColl = cb.getConnection().PropCol; ManagedObjectReference propFilter = cb.getConnection()._service.CreateFilter(propColl, pSpec, false); String version = ""; do { UpdateSet update = cb.getConnection()._service.CheckForUpdates(propColl, version); if (update != null && update.filterSet != null) { handleUpdate(update); version = update.version; } else { Console.WriteLine("No update is present!"); } Console.WriteLine(""); Console.WriteLine("Press <Enter> to check for updates"); Console.WriteLine("Enter 'exit' <Enter> to exit the program"); String line = Console.ReadLine(); if (line.Trim().Equals("exit")) { break; } }while(true); cb.getConnection()._service.DestroyPropertyFilter(propFilter); }
public async Task <IEnumerable <ManagedEntity> > GetPools() { if (session == null) { throw new Exception("Not Logged"); } //////////////////////////////////////////////////////// var dc2hostFolder = new TraversalSpec { type = "Datacenter", path = "hostFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "ressourcesTSpec" } } }; var cr2resourcePool = new TraversalSpec { type = "ComputeResource", path = "resourcePool", selectSet = new SelectionSpec[] { new SelectionSpec { name = "ressourcesTSpec" } } }; var rp2rp = new TraversalSpec { type = "ResourcePool", path = "resourcePool", selectSet = new SelectionSpec[] { new SelectionSpec { name = "ressourcesTSpec" } } }; var folderTS = new TraversalSpec { name = "ressourcesTSpec", type = "Folder", path = "childEntity", selectSet = new SelectionSpec[] { new SelectionSpec { name = "ressourcesTSpec" }, dc2hostFolder, cr2resourcePool, rp2rp } }; var ospec = new ObjectSpec { obj = serviceContent.rootFolder, skip = false, selectSet = new SelectionSpec[] { folderTS } }; ///////////////////////////////////////////// var dcSp = new PropertySpec { type = "Datacenter", all = false, pathSet = new string[] { "parent", "name" } }; var folderSp = new PropertySpec { type = "Folder", all = false, pathSet = new string[] { "parent", "name" } }; var computeSp = new PropertySpec { type = "ComputeResource", all = false, pathSet = new string[] { "parent", "name" } }; var rpSp = new PropertySpec { type = "ResourcePool", all = false, pathSet = new string[] { "parent", "name" } }; //////////////////////////////////////////// var fs = new PropertyFilterSpec { objectSet = new ObjectSpec[] { ospec }, propSet = new PropertySpec[] { dcSp, folderSp, computeSp, rpSp } }; var vmProps = await Vim25Client.RetrievePropertiesAsync(serviceContent.propertyCollector, new PropertyFilterSpec[] { fs }); return(vmProps.returnval .Select(obj => new ManagedEntity { MoRef = obj.obj.Value, Type = obj.obj.type, Name = (string)obj.propSet.FirstOrDefault(o => o.name == "name")?.val, Parent = ((ManagedObjectReference)obj.propSet.FirstOrDefault(o => o.name == "parent")?.val)?.Value })); }
protected ManagedObjectAndProperties[] GetManagedObjectAndProperties(ManagedObjectReference managedObject, string path, string childType, string[] childProperties, string[] properties, out Dictionary <string, object> propertyValues) { PropertySpec propertySpec1 = new PropertySpec(); propertySpec1.type = this._managedObject.type; propertySpec1.all = false; propertySpec1.pathSet = properties; PropertySpec propertySpec2 = new PropertySpec(); propertySpec2.type = childType; propertySpec2.all = false; propertySpec2.pathSet = childProperties; ObjectSpec objectSpec = new ObjectSpec(); objectSpec.obj = managedObject; objectSpec.skip = false; objectSpec.selectSet = new SelectionSpec[1] { (SelectionSpec) new TraversalSpec() { type = managedObject.type, path = path } }; PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); if (properties != null) { propertyFilterSpec.propSet = new PropertySpec[2] { propertySpec1, propertySpec2 } } ; else { propertyFilterSpec.propSet = new PropertySpec[1] { propertySpec2 } }; propertyFilterSpec.objectSet = new ObjectSpec[1] { objectSpec }; List <ManagedObjectAndProperties> objectAndPropertiesList = new List <ManagedObjectAndProperties>(); ObjectContent[] objectContentArray = this.VcService.RetrieveProperties(new PropertyFilterSpec[1] { propertyFilterSpec }); propertyValues = (Dictionary <string, object>)null; if (objectContentArray != null) { foreach (ObjectContent objectContent in objectContentArray) { if (objectContent != null) { if (objectContent.obj.Value == this._managedObject.Value) { propertyValues = this.VcService.PropSetToDictionary(objectContent.propSet); } else { objectAndPropertiesList.Add(new ManagedObjectAndProperties() { ManagedObject = objectContent.obj, Properties = this.VcService.PropSetToDictionary(objectContent.propSet) }); } } } } return(objectAndPropertiesList.ToArray()); }
private object CreateForge( ObjectSpec spec, PluggableObjectType type) { if (Log.IsDebugEnabled) { Log.Debug(".create Creating factory, spec=" + spec); } // Find the factory class for this pattern object Type forgeClass = null; var namespaceMap = patternObjects.Pluggables.Get(spec.ObjectNamespace); var pair = namespaceMap?.Get(spec.ObjectName); if (pair != null) { if (pair.Second.PluggableType == type) { forgeClass = pair.First; } else { // invalid type: expecting observer, got guard if (type == PluggableObjectType.PATTERN_GUARD) { throw new PatternObjectException( "Pattern observer function '" + spec.ObjectName + "' cannot be used as a pattern guard"); } throw new PatternObjectException( "Pattern guard function '" + spec.ObjectName + "' cannot be used as a pattern observer"); } } if (forgeClass == null) { if (type == PluggableObjectType.PATTERN_GUARD) { var message = "Pattern guard name '" + spec.ObjectName + "' is not a known pattern object name"; throw new PatternObjectException(message); } if (type == PluggableObjectType.PATTERN_OBSERVER) { var message = "Pattern observer name '" + spec.ObjectName + "' is not a known pattern object name"; throw new PatternObjectException(message); } throw new PatternObjectException("Pattern object type '" + type + "' not known"); } object result; try { if (forgeClass == typeof(string)) { result = string.Empty; } else { result = TypeHelper.Instantiate(forgeClass); } } catch (MemberAccessException e) { var message = "Error invoking pattern object factory constructor for object '" + spec.ObjectName; message += "', no invocation access for Class.newInstance"; throw new PatternObjectException(message, e); } return result; }
public TaskWaiter(PropertyManager pm, ManagedObjectReference task) { _task = task; _pm = pm; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = task; oSpec.skip = false; oSpec.skipSpecified = true; PropertySpec pSpec = new PropertySpec(); pSpec.all = false; pSpec.allSpecified = true; pSpec.pathSet = new String[] { "info.error", "info.state" }; pSpec.type = task.type; _pfSpec = new PropertyFilterSpec(); _pfSpec.objectSet = new ObjectSpec[] { oSpec }; _pfSpec.propSet = new PropertySpec[] { pSpec }; }
private StringBuilder getEsxLicenseInfo() { // No need for a traversal, since in this case starting object = target object // This is the Object Specification used in this search. ObjectSpec ospec = new ObjectSpec(); // We're starting this search with the service instance's licenseManager. ospec.obj = vim_svc_content.licenseManager; ospec.skip = false; // checks that starting object matches the type specified in the PropertySpec /* this is working just for edition: * // This is the Property Specification use in this search. * PropertySpec pspec = new PropertySpec(); * * pspec.type = "LicenseManager"; * * pspec.pathSet = new string[] { "licensedEdition" }; */ // this is new for obj ref also: ************* START PropertySpec[] pspec = new PropertySpec[] { new PropertySpec(), new PropertySpec() }; pspec[0].type = "LicenseManager"; /* For VI API 2.0 */ pspec[0].pathSet = new string[] { "featureInfo" }; /* for VI API 2.5 */ /* * pspec[0].pathSet = new string[] { "licensedEdition", "featureInfo" }; */ pspec[1].type = "LicenseManager"; pspec[1].all = false; // this is new for obj ref also: ************* END // Build the PropertyFilterSpec and set its PropertySpecficiation (propSet) // and ObjectSpecification (objectset) attributes to pspec and ospec respectively. PropertyFilterSpec pfspec = new PropertyFilterSpec(); pfspec.propSet = pspec; //new PropertySpec[] { pspec }; pfspec.objectSet = new ObjectSpec[] { ospec }; // Retrieve the property values from the VI3 SDk web service. ObjectContent[] occoll = vim_svc.RetrieveProperties( vim_svc_content.propertyCollector, new PropertyFilterSpec[] { pfspec }); // go through results of the property retrieval if there were any. StringBuilder sb = new StringBuilder(); if (occoll != null) { DynamicProperty pc = null; foreach (ObjectContent oc in occoll) { DynamicProperty[] pcary = null; pcary = oc.propSet; for (int i = 0; i < pcary.Length; i++) { pc = pcary[i]; if (pc != null && pc.val.GetType().IsArray) { if (pc.val.GetType() == typeof(VimApi.LicenseFeatureInfo[])) { LicenseFeatureInfo[] host_license_info = (LicenseFeatureInfo[])pc.val; foreach (LicenseFeatureInfo host_license in host_license_info) { sb.Append(oc.propSet[i].name).Append(".costUnit") .Append("<BDNA,1>") .Append(host_license.costUnit) /* commented for VI API 2.5 to VI API 2.0 */ /* * .Append("<BDNA,2>") * .Append(oc.propSet[i].name).Append(".edition") * .Append("<BDNA,1>") * .Append(host_license.edition) * * .Append("<BDNA,2>") * .Append(oc.propSet[i].name).Append(".expiresOn") * .Append("<BDNA,1>") * .Append(host_license.expiresOn) * * .Append("<BDNA,2>") * .Append(oc.propSet[i].name).Append(".featureDescription") * .Append("<BDNA,1>") * .Append(host_license.featureDescription) */ .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".featureName") .Append("<BDNA,1>") .Append(host_license.featureName) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".key") .Append("<BDNA,1>") .Append(host_license.key) /* commented for VI API 2.5 to VI API 2.0 */ /* * .Append("<BDNA,2>") * .Append(oc.propSet[i].name).Append(".sourceRestriction") * .Append("<BDNA,1>") * .Append(host_license.sourceRestriction) */ .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".state") .Append("<BDNA,1>") .Append(host_license.state) .Append("<BDNA,>"); } } } else { ///Property = oc.propSet[i].name and PropertyValue = oc.propSet[i].val /// [Note: Property and their value are seperated by <BDNA,1> delimiter ] sb.Append(oc.propSet[i].name) .Append("<BDNA,1>") .Append(oc.propSet[i].val) .Append("<BDNA,>"); } } } } return(sb); }
private Object[] getProperties(VimService service, ManagedObjectReference moRef, String[] properties) { PropertySpec pSpec = new PropertySpec(); pSpec.type = moRef.type; pSpec.pathSet = properties; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = moRef; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = (new PropertySpec[] { pSpec }); pfSpec.objectSet = (new ObjectSpec[] { oSpec }); ObjectContent[] ocs = service.RetrieveProperties(sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); Object[] ret = new Object[properties.Length]; if (ocs != null) { for (int i = 0; i < ocs.Length; ++i) { ObjectContent oc = ocs[i]; DynamicProperty[] dps = oc.propSet; if (dps != null) { for (int j = 0; j < dps.Length; ++j) { DynamicProperty dp = dps[j]; for (int p = 0; p < ret.Length; ++p) { if (properties[p].Equals(dp.name)) { ret[p] = dp.val; } } } } } } return ret; }
private static ObjectContent[] getVMInfo(ManagedObjectReference vmMoRef) { // This spec selects VirtualMachine information PropertySpec vmPropSpec = new PropertySpec(); vmPropSpec.type = "VirtualMachine"; vmPropSpec.pathSet = new String[] { "name", "config.guestFullName", "config.hardware.memoryMB", "config.hardware.numCPU", "guest.toolsStatus", "guestHeartbeatStatus", "guest.ipAddress", "guest.hostName", "runtime.powerState", "summary.quickStats.overallCpuUsage", "summary.quickStats.hostMemoryUsage", "summary.quickStats.guestMemoryUsage", }; PropertySpec hostPropSpec = new PropertySpec(); hostPropSpec.type = "HostSystem"; hostPropSpec.pathSet = new String[] { "name" }; PropertySpec taskPropSpec = new PropertySpec(); taskPropSpec.type = "Task"; taskPropSpec.pathSet = new String[] { "info.name", "info.completeTime" }; PropertySpec datastorePropSpec = new PropertySpec(); datastorePropSpec.type = "Datastore"; datastorePropSpec.pathSet = new String[] { "info" }; PropertySpec networkPropSpec = new PropertySpec(); networkPropSpec.type = "Network"; networkPropSpec.pathSet = new String[] { "name" }; TraversalSpec hostTraversalSpec = new TraversalSpec(); hostTraversalSpec.type = "VirtualMachine"; hostTraversalSpec.path = "runtime.host"; TraversalSpec taskTravesalSpec = new TraversalSpec(); taskTravesalSpec.type = "VirtualMachine"; taskTravesalSpec.path = "recentTask"; TraversalSpec datastoreTraversalSpec = new TraversalSpec(); datastoreTraversalSpec.type = "VirtualMachine"; datastoreTraversalSpec.path = "datastore"; TraversalSpec networkTraversalSpec = new TraversalSpec(); networkTraversalSpec.type = "VirtualMachine"; networkTraversalSpec.path = "network"; // ObjectSpec specifies the starting object and // any TraversalSpecs used to specify other objects // for consideration ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = vmMoRef; // Add the TraversalSpec objects to the ObjectSpec // This specifies what additional object we want to // consider during the process. oSpec.selectSet = new SelectionSpec[] { hostTraversalSpec, taskTravesalSpec, datastoreTraversalSpec, networkTraversalSpec }; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); // Add the PropertySpec objects to the PropertyFilterSpec // This specifies what data we want to collect while // processing the found objects from the ObjectSpec pfSpec.propSet = new PropertySpec[] { vmPropSpec, hostPropSpec, taskPropSpec, datastorePropSpec, networkPropSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; return _service.RetrieveProperties( _sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); }
private static ObjectContent[] getHosts(ManagedObjectReference dcMoRef) { // PropertySpec specifies what properties to // retrieve from what type of Managed Object PropertySpec pSpec = new PropertySpec(); pSpec.type = "HostSystem"; pSpec.pathSet = new String[] { "name", "runtime.connectionState" }; SelectionSpec recurseFolders = new SelectionSpec(); recurseFolders.name = "folder2childEntity"; TraversalSpec computeResource2host = new TraversalSpec(); computeResource2host.type = "ComputeResource"; computeResource2host.path = "host"; TraversalSpec folder2childEntity = new TraversalSpec(); folder2childEntity.type = "Folder"; folder2childEntity.path = "childEntity"; folder2childEntity.name = recurseFolders.name; // Add BOTH of the specifications to this specification folder2childEntity.selectSet = new SelectionSpec[] { recurseFolders }; // Traverse from a Datacenter through // the 'hostFolder' property TraversalSpec dc2hostFolder = new TraversalSpec(); dc2hostFolder.type = "Datacenter"; dc2hostFolder.path = "hostFolder"; dc2hostFolder.selectSet = new SelectionSpec[] { folder2childEntity }; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = dcMoRef; oSpec.skip = true; oSpec.selectSet = new SelectionSpec[] { dc2hostFolder }; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; return _service.RetrieveProperties( _sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); }
///<summary> /// Specifications to find all the VMs in a Datacenter and /// retrieve their name and runtime.powerState values. ///</summary> private static ObjectContent[] getVMs(ManagedObjectReference dcMoRef) { // The PropertySpec object specifies what properties // retrieve from what type of Managed Object PropertySpec pSpec = new PropertySpec(); pSpec.type = "VirtualMachine"; pSpec.pathSet = new String[] { "name", "runtime.powerState" }; SelectionSpec recurseFolders = new SelectionSpec(); recurseFolders.name = "folder2childEntity"; TraversalSpec folder2childEntity = new TraversalSpec(); folder2childEntity.type = "Folder"; folder2childEntity.path = "childEntity"; folder2childEntity.name = recurseFolders.name; folder2childEntity.selectSet = new SelectionSpec[] { recurseFolders }; // Traverse from a Datacenter through the 'vmFolder' property TraversalSpec dc2vmFolder = new TraversalSpec(); dc2vmFolder.type = "Datacenter"; dc2vmFolder.path = "vmFolder"; dc2vmFolder.selectSet = new SelectionSpec[] { folder2childEntity }; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = dcMoRef; oSpec.skip = true; oSpec.selectSet = new SelectionSpec[] { dc2vmFolder }; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; return _service.RetrieveProperties(_sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); }
///<summary> ///Specifications to find all the Datacenters and ///retrieve their name, vmFolder and hostFolder values. ///</summary> private static ObjectContent[] getDatacenters() { // The PropertySpec object specifies what properties // to retrieve from what type of Managed Object PropertySpec pSpec = new PropertySpec(); pSpec.type = "Datacenter"; pSpec.pathSet = new String[] { "name", "vmFolder", "hostFolder" }; // The following TraversalSpec and SelectionSpec // objects create the following relationship: // // a. Folder -> childEntity // b. recurse to a. // // This specifies that starting with a Folder // managed object, traverse through its childEntity // property. For each element in the childEntity // property, process by going back to the 'parent' // TraversalSpec. // SelectionSpec to cause Folder recursion SelectionSpec recurseFolders = new SelectionSpec(); // The name of a SelectionSpec must refer to a // TraversalSpec with the same name value. recurseFolders.name = "folder2childEntity"; // Traverse from a Folder through the 'childEntity' property TraversalSpec folder2childEntity = new TraversalSpec(); // Select the Folder type managed object folder2childEntity.type = "Folder"; // Traverse through the childEntity property of the Folder folder2childEntity.path = "childEntity"; // Name this TraversalSpec so the SelectionSpec above // can refer to it folder2childEntity.name = recurseFolders.name; // Add the SelectionSpec above to this traversal so that // we will recurse through the tree via the childEntity // property folder2childEntity.selectSet = new SelectionSpec[] { recurseFolders }; // The ObjectSpec object specifies the starting object and // any TraversalSpecs used to specify other objects // for consideration ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = _sic.rootFolder; // We set skip to true because we are not interested // in retrieving properties from the root Folder oSpec.skip = true; // Specify the TraversalSpec. This is what causes // other objects besides the starting object to // be considered part of the collection process oSpec.selectSet = new SelectionSpec[] { folder2childEntity }; // The PropertyFilterSpec object is used to hold the // ObjectSpec and PropertySpec objects for the call PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; // RetrieveProperties() returns the properties // selected from the PropertyFilterSpec return _service.RetrieveProperties( _sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); }
private StringBuilder getEsxHostInfo() { // I name my tspecs so that they are self-explanatory. 'dc2hf' stands for 'Datacenter to Host Folder' TraversalSpec dc2hf = new TraversalSpec(); dc2hf.type = "Datacenter"; dc2hf.path = "hostFolder"; dc2hf.selectSet = new SelectionSpec[] { new SelectionSpec() }; dc2hf.selectSet[0].name = "traverseChild"; TraversalSpec cr2host = new TraversalSpec(); cr2host.type = "ComputeResource"; cr2host.path = "host"; TraversalSpec f2c = new TraversalSpec(); f2c.type = "Folder"; f2c.name = "traverseChild"; f2c.path = "childEntity"; f2c.selectSet = new SelectionSpec[] { new SelectionSpec(), dc2hf, cr2host }; f2c.selectSet[0].name = f2c.name; // This is the Object Specification used in this search. ObjectSpec ospec = new ObjectSpec(); // We're starting this search with the service instance's rootFolder. ospec.obj = vim_svc_content.rootFolder; // Add the top-level tspec (the Folder-2-childEntity) to the ospec. ospec.selectSet = new SelectionSpec[] { f2c }; // This is the Property Specification use in this search. PropertySpec pspec = new PropertySpec(); pspec.type = "HostSystem"; // Do not collect all properties about this object. ///pspec.all = true; // Collect only the name property from this object. ///pspec.pathSet = new string[] { "name", "hardware.memorySize" }; // above only gives memorysize // pspec.pathSet = new string[] { "name"}; pspec.pathSet = new string[] { "name", "hardware.memorySize", "hardware.systemInfo.model", "hardware.systemInfo.uuid", "hardware.systemInfo.vendor", //"hardware.biosInfo.biosVersion", //Since VI API 2.5 //"hardware.biosInfo.releaseDate", //Since VI API 2.5 "hardware.cpuPkg", "summary.hardware.memorySize", "summary.hardware.model", "summary.hardware.uuid", "summary.hardware.vendor", "summary.hardware.cpuMhz", "summary.hardware.cpuModel", "summary.hardware.numCpuCores", "summary.hardware.numCpuPkgs", "summary.hardware.numCpuThreads", "config.storageDevice.scsiLun", "config.network.pnic", "config.product.fullName", "config.product.version", "config.service.service[\"vmware-vpxa\"].running", "runtime.bootTime", "datastore", "vm" }; // Build the PropertyFilterSpec and set its PropertySpecficiation (propSet) // and ObjectSpecification (objectset) attributes to pspec and ospec respectively. PropertyFilterSpec pfspec = new PropertyFilterSpec(); pfspec.propSet = new PropertySpec[] { pspec }; pfspec.objectSet = new ObjectSpec[] { ospec }; // Retrieve the property values from the VI3 SDk web service. ObjectContent[] occoll = vim_svc.RetrieveProperties( vim_svc_content.propertyCollector, new PropertyFilterSpec[] { pfspec }); // go through results of the property retrieval if there were any. StringBuilder sb = new StringBuilder(); if (occoll != null) { DynamicProperty pc = null; foreach (ObjectContent oc in occoll) { sb.Append("<BDNA_ESX>"); //StringBuilder VM_Info = new StringBuilder("<VM>"); StringBuilder VM_Info = new StringBuilder(); DynamicProperty[] pcary = null; pcary = oc.propSet; for (int i = 0; i < pcary.Length; i++) { pc = pcary[i]; if (pc.val.GetType() == typeof(VimApi.ManagedObjectReference[])) { ManagedObjectReference[] dsList = (ManagedObjectReference[])pc.val; ManagedObjectReference dsRef = null; for (int j = 0; j < dsList.Length; j++) { dsRef = dsList[j]; if (dsRef.type.Equals("VirtualMachine")) { //Console.WriteLine("Mnged object type is: VM"); //sb.Append("<VM>"); StringBuilder VM_Info2 = getESXVMsData(dsRef); VM_Info.Append(VM_Info2).Append("<VM>"); //sb.Append(VM_Info); } else { //Managed object type is a datastore Object[] dsProps = getProperties(dsRef, new String[] { "summary.capacity", "summary.freeSpace", "summary.name", "summary.type", "summary.url" }); /* Put sb append under if logic so that it would not give any error if due to any reason all mentioned properties not get collected */ for (int c = 0; c < dsProps.Length; c++) { if (c == 0) { sb.Append("Datastore.capacity") .Append("<BDNA,1>") .Append(Convert.ToString(dsProps[0])) .Append("<BDNA,2>"); } else if (c == 1) { sb.Append("Datastore.freeSpace") .Append("<BDNA,1>") .Append(Convert.ToString(dsProps[1])) .Append("<BDNA,2>"); } else if (c == 2) { sb.Append("Datastore.name") .Append("<BDNA,1>") .Append(((String)dsProps[2])) .Append("<BDNA,2>"); } else if (c == 3) { sb.Append("Datastore.type") .Append("<BDNA,1>") .Append(((String)dsProps[3])) .Append("<BDNA,2>"); } else if (c == 4) { sb.Append("Datastore.url") .Append("<BDNA,1>") .Append(((String)dsProps[4])); } } sb.Append("<BDNA,>"); } } } else if (pc != null && pc.val.GetType().IsArray) { //if (pc != null && pc.val.GetType().IsArray) { /* * OUTPUT: * pc type is: XmlNode[] * pc type is2: * pc type is3: System.Xml.XmlNode[] * * CODE: * Console.WriteLine("pc type is: {0}", pc.val.GetType().Name); * Console.WriteLine("pc type is2: {0}", pc.val.GetType().ReflectedType ); * Console.WriteLine("pc type is3: {0}", pc.val.GetType().ToString()); * Console.WriteLine("pc type is4: {0}, {1}, {2},{3}", pc.val.GetType().UnderlyingSystemType, * pc.val.GetType().TypeInitializer, pc.val.GetType().TypeHandle, * pc.val.GetType().MakeArrayType() * ); */ if (pc.val.GetType() == typeof(VimApi.PhysicalNic[])) { // PhysicalNic: //PhysicalNic[] host_phyNic_info = (PhysicalNic[])oc.propSet[0].val; PhysicalNic[] host_phyNic_info = (PhysicalNic[])pc.val; foreach (PhysicalNic host_phyNic in host_phyNic_info) { /* VI API 2.0 */ sb.Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".device") .Append("<BDNA,1>") .Append(host_phyNic.device) .Append("<BDNA,>"); /* VI API 2.5 */ /* * sb.Append(oc.propSet[i].name).Append(".mac") * .Append("<BDNA,1>") * .Append(host_phyNic.mac) * * .Append("<BDNA,2>") * .Append(oc.propSet[i].name).Append(".device") * .Append("<BDNA,1>") * .Append(host_phyNic.device) * * .Append("<BDNA,>"); */ } } if (pc.val.GetType() == typeof(VimApi.ScsiLun[])) { //ScsiLun: //ScsiLun[] host_storage_info = (ScsiLun[])oc.propSet[1].val; ScsiLun[] host_storage_info = (ScsiLun[])pc.val; foreach (ScsiLun host_stroage in host_storage_info) { if (host_stroage.deviceType.Equals("disk")) { sb.Append(oc.propSet[i].name).Append(".HostScsiDisk.capacity.block") .Append("<BDNA,1>") .Append(((HostScsiDisk)host_stroage).capacity.block) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".HostScsiDisk.capacity.blockSize") .Append("<BDNA,1>") .Append(((HostScsiDisk)host_stroage).capacity.blockSize) .Append("<BDNA,2>"); } sb.Append(oc.propSet[i].name).Append(".canonicalName") .Append("<BDNA,1>") .Append(host_stroage.canonicalName) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".deviceName") .Append("<BDNA,1>") .Append(host_stroage.deviceName) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".deviceType") .Append("<BDNA,1>") .Append(host_stroage.deviceType) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".model") .Append("<BDNA,1>") .Append(host_stroage.model) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".serialNumber") .Append("<BDNA,1>") .Append(host_stroage.serialNumber) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".vendor") .Append("<BDNA,1>") .Append(host_stroage.vendor) .Append("<BDNA,>"); } } if (pc.val.GetType() == typeof(VimApi.HostCpuPackage[])) { HostCpuPackage[] host_cpuPkg_info = (HostCpuPackage[])pc.val; foreach (HostCpuPackage host_cpuPkg in host_cpuPkg_info) { sb.Append(oc.propSet[i].name).Append(".description") .Append("<BDNA,1>") .Append(host_cpuPkg.description) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".hz") .Append("<BDNA,1>") .Append(host_cpuPkg.hz) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".index") .Append("<BDNA,1>") .Append(host_cpuPkg.index) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".threadId_NumberOfThread") .Append("<BDNA,1>") .Append(host_cpuPkg.threadId.Length) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".vendor") .Append("<BDNA,1>") .Append(host_cpuPkg.vendor) .Append("<BDNA,>"); } } } else { ///Property = oc.propSet[i].name and PropertyValue = oc.propSet[i].val /// [Note: Property and their value are seperated by <BDNA,1> delimiter ] sb.Append(oc.propSet[i].name) .Append("<BDNA,1>") .Append(oc.propSet[i].val) .Append("<BDNA,>"); } } // Append VMs data for a ESX host: sb.Append("<VMS>").Append(VM_Info).Append("</VMS>"); } } sb.Remove(0, 10); return(sb); }
private StringBuilder getEsxHostInfo() { // I name my tspecs so that they are self-explanatory. 'dc2hf' stands for 'Datacenter to Host Folder' TraversalSpec dc2hf = new TraversalSpec(); dc2hf.type = "Datacenter"; dc2hf.path = "hostFolder"; dc2hf.selectSet = new SelectionSpec[] { new SelectionSpec() }; dc2hf.selectSet[0].name = "traverseChild"; TraversalSpec cr2host = new TraversalSpec(); cr2host.type = "ComputeResource"; cr2host.path = "host"; TraversalSpec f2c = new TraversalSpec(); f2c.type = "Folder"; f2c.name = "traverseChild"; f2c.path = "childEntity"; f2c.selectSet = new SelectionSpec[] { new SelectionSpec(), dc2hf, cr2host }; f2c.selectSet[0].name = f2c.name; // This is the Object Specification used in this search. ObjectSpec ospec = new ObjectSpec(); // We're starting this search with the service instance's rootFolder. ospec.obj = vim_svc_content.rootFolder; // Add the top-level tspec (the Folder-2-childEntity) to the ospec. ospec.selectSet = new SelectionSpec[] { f2c }; // This is the Property Specification use in this search. PropertySpec pspec = new PropertySpec(); pspec.type = "HostSystem"; // Do not collect all properties about this object. ///pspec.all = true; // Collect only the name property from this object. ///pspec.pathSet = new string[] { "name", "hardware.memorySize" }; // above only gives memorysize // pspec.pathSet = new string[] { "name"}; pspec.pathSet = new string[] { "name", "hardware.memorySize", "hardware.systemInfo.model", "hardware.systemInfo.uuid", "hardware.systemInfo.vendor", //"hardware.biosInfo.biosVersion", //Since VI API 2.5 //"hardware.biosInfo.releaseDate", //Since VI API 2.5 "hardware.cpuPkg", "summary.hardware.memorySize", "summary.hardware.model", "summary.hardware.uuid", "summary.hardware.vendor", "summary.hardware.cpuMhz", "summary.hardware.cpuModel", "summary.hardware.numCpuCores", "summary.hardware.numCpuPkgs", "summary.hardware.numCpuThreads", "config.storageDevice.scsiLun", "config.network.pnic", //"config.product.fullName", //"config.product.version", "config.service.service[\"vmware-vpxa\"].running", "runtime.bootTime" }; // Build the PropertyFilterSpec and set its PropertySpecficiation (propSet) // and ObjectSpecification (objectset) attributes to pspec and ospec respectively. PropertyFilterSpec pfspec = new PropertyFilterSpec(); pfspec.propSet = new PropertySpec[] { pspec }; pfspec.objectSet = new ObjectSpec[] { ospec }; // Retrieve the property values from the VI3 SDk web service. ObjectContent[] occoll = vim_svc.RetrieveProperties( vim_svc_content.propertyCollector, new PropertyFilterSpec[] { pfspec }); // go through results of the property retrieval if there were any. StringBuilder sb = new StringBuilder(); if (occoll != null) { DynamicProperty pc = null; foreach (ObjectContent oc in occoll) { DynamicProperty[] pcary = null; pcary = oc.propSet; for (int i = 0; i < pcary.Length; i++) { pc = pcary[i]; if (pc != null && pc.val.GetType().IsArray) { if (pc.val.GetType() == typeof(VimApi.PhysicalNic[])) { // PhysicalNic: //PhysicalNic[] host_phyNic_info = (PhysicalNic[])oc.propSet[0].val; PhysicalNic[] host_phyNic_info = (PhysicalNic[])pc.val; foreach (PhysicalNic host_phyNic in host_phyNic_info) { /* VI API 2.0 */ sb.Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".device") .Append("<BDNA,1>") .Append(host_phyNic.device) .Append("<BDNA,>"); /* VI API 2.5 */ /* * sb.Append(oc.propSet[i].name).Append(".mac") * .Append("<BDNA,1>") * .Append(host_phyNic.mac) * * .Append("<BDNA,2>") * .Append(oc.propSet[i].name).Append(".device") * .Append("<BDNA,1>") * .Append(host_phyNic.device) * * .Append("<BDNA,>"); */ } } if (pc.val.GetType() == typeof(VimApi.ScsiLun[])) { //ScsiLun: //ScsiLun[] host_storage_info = (ScsiLun[])oc.propSet[1].val; ScsiLun[] host_storage_info = (ScsiLun[])pc.val; foreach (ScsiLun host_stroage in host_storage_info) { if (host_stroage.deviceType.Equals("disk")) { sb.Append(oc.propSet[i].name).Append(".HostScsiDisk.capacity.block") .Append("<BDNA,1>") .Append(((HostScsiDisk)host_stroage).capacity.block) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".HostScsiDisk.capacity.blockSize") .Append("<BDNA,1>") .Append(((HostScsiDisk)host_stroage).capacity.blockSize) .Append("<BDNA,2>"); } sb.Append(oc.propSet[i].name).Append(".canonicalName") .Append("<BDNA,1>") .Append(host_stroage.canonicalName) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".deviceName") .Append("<BDNA,1>") .Append(host_stroage.deviceName) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".deviceType") .Append("<BDNA,1>") .Append(host_stroage.deviceType) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".model") .Append("<BDNA,1>") .Append(host_stroage.model) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".serialNumber") .Append("<BDNA,1>") .Append(host_stroage.serialNumber) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".vendor") .Append("<BDNA,1>") .Append(host_stroage.vendor) .Append("<BDNA,>"); } } if (pc.val.GetType() == typeof(VimApi.HostCpuPackage[])) { HostCpuPackage[] host_cpuPkg_info = (HostCpuPackage[])pc.val; foreach (HostCpuPackage host_cpuPkg in host_cpuPkg_info) { sb.Append(oc.propSet[i].name).Append(".description") .Append("<BDNA,1>") .Append(host_cpuPkg.description) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".hz") .Append("<BDNA,1>") .Append(host_cpuPkg.hz) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".index") .Append("<BDNA,1>") .Append(host_cpuPkg.index) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".threadId_NumberOfThread") .Append("<BDNA,1>") .Append(host_cpuPkg.threadId.Length) .Append("<BDNA,2>") .Append(oc.propSet[i].name).Append(".vendor") .Append("<BDNA,1>") .Append(host_cpuPkg.vendor) .Append("<BDNA,>"); } } } else { ///Property = oc.propSet[i].name and PropertyValue = oc.propSet[i].val /// [Note: Property and their value are seperated by <BDNA,1> delimiter ] sb.Append(oc.propSet[i].name) .Append("<BDNA,1>") .Append(oc.propSet[i].val) .Append("<BDNA,>"); } } } } return(sb); }
private StringBuilder getESXVMsData() { // I name my tspecs so that they are self-explanatory. 'dc2vmf' stands for 'Datacenter to vm Folder' TraversalSpec dc2vmf = new TraversalSpec(); dc2vmf.type = "Datacenter"; dc2vmf.path = "vmFolder"; dc2vmf.selectSet = new SelectionSpec[] { new SelectionSpec() }; dc2vmf.selectSet[0].name = "traverseChild"; TraversalSpec f2c = new TraversalSpec(); f2c.type = "Folder"; f2c.name = "traverseChild"; f2c.path = "childEntity"; f2c.selectSet = new SelectionSpec[] { new SelectionSpec(), dc2vmf }; f2c.selectSet[0].name = f2c.name; // This is the Object Specification used in this search. ObjectSpec ospec = new ObjectSpec(); // We're starting this search with the service instance's rootFolder. ospec.obj = vim_svc_content.rootFolder; // Add the top-level tspec (the Folder-2-childEntity) to the ospec. ospec.selectSet = new SelectionSpec[] { f2c }; // This is the Property Specification use in this search. PropertySpec pspec = new PropertySpec(); ////pspec.type = "HostSystem"; pspec.type = "VirtualMachine"; // Do not collect all properties about this object, only few selected properties. ///pspec.all = true; pspec.pathSet = new string[] { "name", "summary.config.guestFullName", "summary.config.guestId", "summary.config.memorySizeMB", "summary.config.numCpu", "summary.config.numVirtualDisks", "summary.config.uuid", "summary.config.vmPathName", "summary.runtime.powerState", "guest.hostName", "guest.ipAddress", "guest.toolsStatus", "guest.toolsVersion", "guest.net", "config.hardware.device" }; // Build the PropertyFilterSpec and set its PropertySpecficiation (propSet) // and ObjectSpecification (objectset) attributes to pspec and ospec respectively. PropertyFilterSpec pfspec = new PropertyFilterSpec(); pfspec.propSet = new PropertySpec[] { pspec }; pfspec.objectSet = new ObjectSpec[] { ospec }; // Retrieve the property values from the VI3 SDk web service. ObjectContent[] occoll = vim_svc.RetrieveProperties( vim_svc_content.propertyCollector, new PropertyFilterSpec[] { pfspec }); // go through results of the property retrieval if there were any. StringBuilder sb = new StringBuilder(); if (occoll != null) { DynamicProperty pc = null; foreach (ObjectContent oc in occoll) { DynamicProperty[] pcary = null; pcary = oc.propSet; sb.Append("<BDNA_ESX_VM>"); for (int i = 0; i < pcary.Length; i++) { pc = pcary[i]; if (pc != null && pc.val.GetType().IsArray) { if (pc.val.GetType() == typeof(VimApi.GuestNicInfo[])) { /* commented macaddress from guest.net (available only if VMware Tools running on VM), instead use macaddress from config.hardware.device * * GuestNicInfo[] guestNics = (GuestNicInfo[])pc.val; * foreach (GuestNicInfo nics in guestNics) { * //sb.Append(oc.propSet[i].name).Append(".macAddress") // OR * sb.Append(pc.name).Append(".macAddress") * .Append("<BDNA,1>") * .Append(nics.macAddress) * * .Append("<BDNA,2>") * * .Append(pc.name).Append(".ipAddress") * .Append("<BDNA,1>"); * //.Append(nics.ipAddress) // this gives System.String[] * int c = 0; * foreach (String ip in nics.ipAddress) { * if (c > 0) { * sb.Append(",").Append(ip); * } else { * sb.Append(ip); * c++; * } * } * sb.Append("<BDNA,>"); * } */ } else if (pc.val.GetType() == typeof(VimApi.VirtualDevice[])) { VirtualDevice[] vd = (VirtualDevice[])pc.val; foreach (VirtualDevice dev in vd) { if (dev.GetType().BaseType == typeof(VimApi.VirtualEthernetCard)) { sb.Append(pc.name).Append(".macAddress") .Append("<BDNA,1>") .Append(((VirtualEthernetCard)dev).macAddress) .Append("<BDNA,2>") .Append(pc.name).Append(".addressType") .Append("<BDNA,1>") .Append(((VirtualEthernetCard)dev).addressType); sb.Append("<BDNA,>"); } } } } else { sb.Append(oc.propSet[i].name) .Append("<BDNA,1>") .Append(oc.propSet[i].val) .Append("<BDNA,>"); } } } } return(sb); }
private StringBuilder getEsxDatastoreInfo() { // I name my tspecs so that they are self-explanatory. 'dc2hf' stands for 'Datacenter to Host Folder' TraversalSpec dc2hf = new TraversalSpec(); dc2hf.type = "Datacenter"; dc2hf.path = "hostFolder"; dc2hf.selectSet = new SelectionSpec[] { new SelectionSpec() }; dc2hf.selectSet[0].name = "traverseChild"; TraversalSpec cr2host = new TraversalSpec(); cr2host.type = "ComputeResource"; cr2host.path = "host"; TraversalSpec f2c = new TraversalSpec(); f2c.type = "Folder"; f2c.name = "traverseChild"; f2c.path = "childEntity"; f2c.selectSet = new SelectionSpec[] { new SelectionSpec(), dc2hf, cr2host }; f2c.selectSet[0].name = f2c.name; // This is the Object Specification used in this search. ObjectSpec ospec = new ObjectSpec(); // We're starting this search with the service instance's rootFolder. ospec.obj = vim_svc_content.rootFolder; // Add the top-level tspec (the Folder-2-childEntity) to the ospec. ospec.selectSet = new SelectionSpec[] { f2c }; // This is the Property Specification use in this search. PropertySpec pspec = new PropertySpec(); pspec.type = "HostSystem"; // Do not collect all properties about this object. //pspec.all = true; pspec.pathSet = new string[] { "datastore" }; // Build the PropertyFilterSpec and set its PropertySpecficiation (propSet) // and ObjectSpecification (objectset) attributes to pspec and ospec respectively. PropertyFilterSpec pfspec = new PropertyFilterSpec(); pfspec.propSet = new PropertySpec[] { pspec }; pfspec.objectSet = new ObjectSpec[] { ospec }; // Retrieve the property values from the VI3 SDk web service. ObjectContent[] occoll = vim_svc.RetrieveProperties( vim_svc_content.propertyCollector, new PropertyFilterSpec[] { pfspec }); // Print out the results of the property retrieval if there were any. StringBuilder sb = new StringBuilder(); if (occoll != null) { DynamicProperty pc = null; foreach (ObjectContent oc in occoll) { DynamicProperty[] pcary = null; pcary = oc.propSet; for (int i = 0; i < pcary.Length; i++) { pc = pcary[i]; if (pc.val.GetType() == typeof(VimApi.ManagedObjectReference[])) { ManagedObjectReference[] dsList = (ManagedObjectReference[])pc.val; ManagedObjectReference dsRef = null; for (int j = 0; j < dsList.Length; j++) { dsRef = dsList[j]; Object[] dsProps = getProperties(dsRef, new String[] { "summary.capacity", "summary.freeSpace", "summary.name", "summary.type", "summary.url" }); /* Put sb append under if logic so that it would not give any error if due to any reason all mentioned properties not get collected */ for (int c = 0; c < dsProps.Length; c++) { if (c == 0) { sb.Append("Datastore.capacity") .Append("<BDNA,1>") .Append(Convert.ToString(dsProps[0])) .Append("<BDNA,2>"); } else if (c == 1) { sb.Append("Datastore.freeSpace") .Append("<BDNA,1>") .Append(Convert.ToString(dsProps[1])) .Append("<BDNA,2>"); } else if (c == 2) { sb.Append("Datastore.name") .Append("<BDNA,1>") .Append(((String)dsProps[2])) .Append("<BDNA,2>"); } else if (c == 3) { sb.Append("Datastore.type") .Append("<BDNA,1>") .Append(((String)dsProps[3])) .Append("<BDNA,2>"); } else if (c == 4) { sb.Append("Datastore.url") .Append("<BDNA,1>") .Append(((String)dsProps[4])); } } sb.Append("<BDNA,>"); } } } } } return(sb); }
/// <summary> /// Return MOR /// </summary> /// <param name="folder">ManagedObjectReference</param> /// <param name="type">string</param> /// <param name="name">string</param> /// <returns>ManagedObjectReference</returns> public ManagedObjectReference GetMOREFsInFolderByType(ManagedObjectReference folder, string type, string name) { String propName = "name"; string[] type1 = new string[2]; type1[0] = type; ManagedObjectReference viewManager = cb._connection._sic.viewManager; ManagedObjectReference containerView = cb._connection._service.CreateContainerView(viewManager, folder, type1, true); PropertySpec propertySpec = new PropertySpec(); propertySpec.all = false; propertySpec.type = type; propertySpec.pathSet = new string[] { propName }; TraversalSpec ts = new TraversalSpec(); ts.name = "view"; ts.path = "view"; ts.skip = false; ts.type = "ContainerView"; // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.obj = containerView; objectSpec.skip = true; objectSpec.selectSet = new SelectionSpec[] { ts }; // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.propSet = new PropertySpec[] { propertySpec }; propertyFilterSpec.objectSet = new ObjectSpec[] { objectSpec }; PropertyFilterSpec[] filterspec = new PropertyFilterSpec[3]; filterspec[0] = propertyFilterSpec; ObjectContent[] ocary = cb._svcUtil.retrievePropertiesEx(cb._connection._sic.propertyCollector, filterspec); if (ocary == null || ocary.Length == 0) { return null; } ObjectContent oc = null; ManagedObjectReference mor = null; DynamicProperty[] propary = null; string propval = null; bool found = false; for (int oci = 0; oci < ocary.Length && !found; oci++) { oc = ocary[oci]; mor = oc.obj; propary = oc.propSet; if ((type == null) || (type != null && cb._svcUtil.typeIsA(type, mor.type))) { if (propary.Length > 0) { propval = (string)propary[0].val; } found = propval != null && name.Equals(propval); propval = null; } } if (!found) { mor = null; } return mor; }
/* * getProperties -- * * Retrieves the specified set of properties for the given managed object * reference into an array of result objects (returned in the same oder * as the property list). */ public static Object[] getProperties(ManagedObjectReference moRef, String[] properties) { // PropertySpec specifies what properties to // retrieve and from type of Managed Object PropertySpec pSpec = new PropertySpec(); pSpec.type = moRef.type; pSpec.pathSet = properties; // ObjectSpec specifies the starting object and // any TraversalSpecs used to specify other objects // for consideration ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = moRef; // PropertyFilterSpec is used to hold the ObjectSpec and // PropertySpec for the call PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { pSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; // retrieveProperties() returns the properties // selected from the PropertyFilterSpec //VimService service1 = new VimService(); ManagedObjectReference _svcRef1 = new ManagedObjectReference(); _svcRef1.type = "ServiceInstance"; _svcRef1.Value = "ServiceInstance"; ServiceContent sic1 = vim_svc.RetrieveServiceContent(_svcRef1); ObjectContent[] ocs = new ObjectContent[20]; ocs = vim_svc.RetrieveProperties(sic1.propertyCollector, new PropertyFilterSpec[] { pfSpec }); // Return value, one object for each property specified Object[] ret = new Object[properties.Length]; if (ocs != null) { for (int i = 0; i < ocs.Length; ++i) { ObjectContent oc = ocs[i]; DynamicProperty[] dps = oc.propSet; if (dps != null) { for (int j = 0; j < dps.Length; ++j) { DynamicProperty dp = dps[j]; // find property path index for (int p = 0; p < ret.Length; ++p) { if (properties[p].Equals(dp.name)) { ret[p] = dp.val; } } } } } } return(ret); }
static ObjectContent[] getVMInfo(ManagedObjectReference vmMoRef) { // This spec selects VirtualMachine information PropertySpec vmPropSpec = new PropertySpec(); vmPropSpec.type = "VirtualMachine"; vmPropSpec.pathSet = new String[]{"name","config.guestFullName","summary.quickStats.overallCpuUsage","summary.quickStats.hostMemoryUsage","summary.quickStats.guestMemoryUsage"}; PropertySpec hostPropSpec = new PropertySpec(); hostPropSpec.type = "HostSystem"; hostPropSpec.pathSet = new String[] { "name", "summary.quickStats.overallCpuUsage", "summary.quickStats.overallMemoryUsage", }; TraversalSpec hostTraversalSpec = new TraversalSpec(); hostTraversalSpec.type = "VirtualMachine"; hostTraversalSpec.path = "runtime.host"; ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = vmMoRef; oSpec.selectSet = new SelectionSpec[]{hostTraversalSpec}; PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[]{vmPropSpec,hostPropSpec}; pfSpec.objectSet = new ObjectSpec[]{oSpec}; return _service.RetrieveProperties(_sic.propertyCollector,new PropertyFilterSpec[]{pfSpec}); }
///<summary> /// Specifications to find all Networks in a Datacenter, /// list all VMs on each Network, /// list all Hosts on each Network ///</summary> private static ObjectContent[] getNetworkInfo( ManagedObjectReference dcMoRef) { // PropertySpec specifies what properties to // retrieve from what type of Managed Object // This spec selects the Network name PropertySpec networkPropSpec = new PropertySpec(); networkPropSpec.type = "Network"; networkPropSpec.pathSet = new String[] { "name" }; // This spec selects HostSystem information PropertySpec hostPropSpec = new PropertySpec(); hostPropSpec.type = "HostSystem"; hostPropSpec.pathSet = new String[] { "network", "name", "summary.hardware", "runtime.connectionState", "summary.overallStatus", "summary.quickStats" }; // This spec selects VirtualMachine information PropertySpec vmPropSpec = new PropertySpec(); vmPropSpec.type = "VirtualMachine"; vmPropSpec.pathSet = new String[] { "network", "name", "runtime.host", "runtime.powerState", "summary.overallStatus", "summary.quickStats" }; // The following TraversalSpec and SelectionSpec // objects create the following relationship: // // a. Datacenter -> network // b. Network -> host // c. Network -> vm // b. Traverse from a Network through the 'host' property TraversalSpec network2host = new TraversalSpec(); network2host.type = "Network"; network2host.path = "host"; // c. Traverse from a Network through the 'vm' property TraversalSpec network2vm = new TraversalSpec(); network2vm.type = "Network"; network2vm.path = "vm"; // a. Traverse from a Datacenter through // the 'network' property TraversalSpec dc2network = new TraversalSpec(); dc2network.type = "Datacenter"; dc2network.path = "network"; dc2network.selectSet = new SelectionSpec[] { // Add b. traversal network2host, // Add c. traversal network2vm }; // ObjectSpec specifies the starting object and // any TraversalSpecs used to specify other objects // for consideration ObjectSpec oSpec = new ObjectSpec(); oSpec.obj = dcMoRef; oSpec.skip = true; oSpec.selectSet = new SelectionSpec[] { dc2network }; // PropertyFilterSpec is used to hold the ObjectSpec and // PropertySpec for the call PropertyFilterSpec pfSpec = new PropertyFilterSpec(); pfSpec.propSet = new PropertySpec[] { networkPropSpec, hostPropSpec, vmPropSpec }; pfSpec.objectSet = new ObjectSpec[] { oSpec }; // RetrieveProperties() returns the properties // selected from the PropertyFilterSpec return _service.RetrieveProperties( _sic.propertyCollector, new PropertyFilterSpec[] { pfSpec }); }
public PropertyFilterSpec[] NetworkSearchFilter() { PropertySpec prop; List <PropertySpec> props = new List <PropertySpec>(); TraversalSpec trav = new TraversalSpec(); List <SelectionSpec> list = new List <SelectionSpec>(); SelectionSpec sel = new SelectionSpec(); List <SelectionSpec> selectset = new List <SelectionSpec>(); ObjectSpec objectspec = new ObjectSpec(); PropertyFilterSpec filter = new PropertyFilterSpec(); trav.name = "DatacenterTraversalSpec"; trav.type = "Datacenter"; trav.path = "networkFolder"; sel.name = "FolderTraversalSpec"; selectset.Add(sel); trav.selectSet = selectset.ToArray(); list.Add(trav); trav = new TraversalSpec(); trav.name = "FolderTraversalSpec"; trav.type = "Folder"; trav.path = "childEntity"; selectset.Clear(); sel = new SelectionSpec(); sel.name = "DatacenterTraversalSpec"; selectset.Add(sel); trav.selectSet = selectset.ToArray(); list.Add(trav); prop = new PropertySpec(); prop.type = "Datacenter"; prop.pathSet = new string[] { "networkFolder", "name" }; props.Add(prop); prop = new PropertySpec(); prop.type = "Folder"; prop.pathSet = new string[] { "childEntity", "name" }; props.Add(prop); prop = new PropertySpec(); prop.type = "VmwareDistributedVirtualSwitch"; prop.pathSet = new string[] { "portgroup", "name", "parent", "uuid" }; props.Add(prop); prop = new PropertySpec(); prop.type = "DistributedVirtualPortgroup"; prop.pathSet = new string[] { "name", "key" }; props.Add(prop); objectspec = new ObjectSpec(); objectspec.obj = _sic.rootFolder; objectspec.selectSet = list.ToArray(); filter = new PropertyFilterSpec(); filter.propSet = props.ToArray(); filter.objectSet = new ObjectSpec[] { objectspec }; PropertyFilterSpec[] _dvNetworkSearchFilters = new PropertyFilterSpec[] { filter }; return(_dvNetworkSearchFilters); }
private Object CreateFactory(ObjectSpec spec, PluggableObjectType type) { if (Log.IsDebugEnabled) { Log.Debug(".create Creating factory, spec=" + spec); } // Find the factory class for this pattern object Type factoryClass = null; IDictionary <String, Pair <Type, PluggableObjectEntry> > namespaceMap = _patternObjects.Pluggables.Get(spec.ObjectNamespace); if (namespaceMap != null) { Pair <Type, PluggableObjectEntry> pair = namespaceMap.Get(spec.ObjectName); if (pair != null) { if (pair.Second.PluggableType == type) { factoryClass = pair.First; } else { // invalid type: expecting observer, got guard if (type == PluggableObjectType.PATTERN_GUARD) { throw new PatternObjectException("Pattern observer function '" + spec.ObjectName + "' cannot be used as a pattern guard"); } else { throw new PatternObjectException("Pattern guard function '" + spec.ObjectName + "' cannot be used as a pattern observer"); } } } } if (factoryClass == null) { if (type == PluggableObjectType.PATTERN_GUARD) { String message = "Pattern guard name '" + spec.ObjectName + "' is not a known pattern object name"; throw new PatternObjectException(message); } else if (type == PluggableObjectType.PATTERN_OBSERVER) { String message = "Pattern observer name '" + spec.ObjectName + "' is not a known pattern object name"; throw new PatternObjectException(message); } else { throw new PatternObjectException("Pattern object type '" + type + "' not known"); } } Object result; try { result = Activator.CreateInstance(factoryClass); } catch (TypeInstantiationException ex) { String message = "Error invoking pattern object factory constructor for object '" + spec.ObjectName; message += "' using Activator.CreateInstance"; throw new PatternObjectException(message, ex); } catch (TargetInvocationException ex) { String message = "Error invoking pattern object factory constructor for object '" + spec.ObjectName; message += "' using Activator.CreateInstance"; throw new PatternObjectException(message, ex); } catch (MethodAccessException ex) { String message = "Error invoking pattern object factory constructor for object '" + spec.ObjectName; message += "', no invocation access for Activator.CreateInstance"; throw new PatternObjectException(message, ex); } catch (MemberAccessException ex) { String message = "Error invoking pattern object factory constructor for object '" + spec.ObjectName; message += "', no invocation access for Activator.CreateInstance"; throw new PatternObjectException(message, ex); } return(result); }
public IReadOnlyCollection <string> GetVirtualMachineNames() { EnsureConnected(); ServiceContent serviceContent = _connection.ServiceContent; ManagedObjectReference viewManager = serviceContent.viewManager; ManagedObjectReference propertyCollector = serviceContent.propertyCollector; ManagedObjectReference containerView = _connection.Service.CreateContainerView(viewManager, serviceContent.rootFolder, new[] { "VirtualMachine" }, true); TraversalSpec traversalSpec = new TraversalSpec() { name = "traverseEntities", type = "ContainerView", path = "view", skip = false }; ObjectSpec objectSpec = new ObjectSpec { obj = containerView, skip = true }; objectSpec.selectSet = new SelectionSpec[] { traversalSpec }; PropertySpec propertySpec = new PropertySpec() { type = "VirtualMachine", pathSet = new [] { "name" } }; PropertyFilterSpec filter = new PropertyFilterSpec() { objectSet = new[] { objectSpec }, propSet = new[] { propertySpec } }; RetrieveOptions retrieveOptions = new RetrieveOptions(); RetrieveResult properties = _connection.Service.RetrievePropertiesEx(propertyCollector, new[] { filter }, retrieveOptions); var virtualMachineNames = new List <string>(); if (properties != null) { foreach (ObjectContent objectContent in properties.objects) { DynamicProperty[] dynamicProperties = objectContent.propSet; if (dynamicProperties != null) { foreach (DynamicProperty dynamicProperty in dynamicProperties) { string virtualMachineName = (string)dynamicProperty.val; string path = dynamicProperty.name; virtualMachineNames.Add(virtualMachineName); } } } } return(virtualMachineNames); }
/// <summary> /// Get MOR of Datacenter /// </summary> /// <param name="dsMor">ManagedObjectReference</param> /// <returns>ManagedObjectReference</returns> private ManagedObjectReference getDatacenterOfDatastore(ManagedObjectReference dsMor) { ManagedObjectReference datacenter = null; // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.all = false; propertySpec.type = "Datacenter"; propertySpec.pathSet = new string[] { "name" }; // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.obj = dsMor; objectSpec.skip = true; objectSpec.selectSet = buildTraversalSpecForDatastoreToDatacenter(); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.propSet = new PropertySpec[] { propertySpec }; propertyFilterSpec.objectSet = new ObjectSpec[] { objectSpec }; PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] { propertyFilterSpec }; ObjectContent[] oCont = cb._svcUtil.retrievePropertiesEx(cb._connection._sic.propertyCollector, propertyFilterSpecs); if (oCont != null) { foreach (ObjectContent oc in oCont) { datacenter = oc.obj; break; } } return datacenter; }
private async Task LoadCache() { var plan = new TraversalSpec { name = "FolderTraverseSpec", type = "Folder", path = "childEntity", selectSet = new SelectionSpec[] { new TraversalSpec() { type = "Datacenter", path = "networkFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "Datacenter", path = "vmFolder", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "Datacenter", path = "datastore", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, new TraversalSpec() { type = "Folder", path = "childEntity", selectSet = new SelectionSpec[] { new SelectionSpec { name = "FolderTraverseSpec" } } }, } }; var props = new PropertySpec[] { new PropertySpec { type = "DistributedVirtualSwitch", pathSet = new string[] { "name", "uuid", "config.uplinkPortgroup" } }, new PropertySpec { type = "DistributedVirtualPortgroup", pathSet = new string[] { "name", "host", "config.distributedVirtualSwitch" } }, new PropertySpec { type = "Network", pathSet = new string[] { "name", "host" } }, new PropertySpec { type = "VirtualMachine", pathSet = new string[] { "name", "config.uuid", "summary.runtime.powerState", "guest.net" } }, new PropertySpec { type = "Datastore", pathSet = new string[] { "name", "browser" } } }; ObjectSpec objectspec = new ObjectSpec(); objectspec.obj = _sic.rootFolder; objectspec.selectSet = new SelectionSpec[] { plan }; PropertyFilterSpec filter = new PropertyFilterSpec(); filter.propSet = props; filter.objectSet = new ObjectSpec[] { objectspec }; PropertyFilterSpec[] filters = new PropertyFilterSpec[] { filter }; _logger.LogInformation($"Starting RetrieveProperties at {DateTime.UtcNow}"); RetrievePropertiesResponse response = await _client.RetrievePropertiesAsync(_props, filters); _logger.LogInformation($"Finished RetrieveProperties at {DateTime.UtcNow}"); _logger.LogInformation($"Starting LoadMachineCache at {DateTime.UtcNow}"); await LoadMachineCache(response.returnval.FindType("VirtualMachine")); _logger.LogInformation($"Finished LoadMachineCache at {DateTime.UtcNow}"); _logger.LogInformation($"Starting LoadNetworkCache at {DateTime.UtcNow}"); LoadNetworkCache( response.returnval.FindType("DistributedVirtualSwitch"), response.returnval.Where(o => o.obj.type.EndsWith("Network") || o.obj.type.EndsWith("DistributedVirtualPortgroup")).ToArray()); _logger.LogInformation($"Finished LoadNetworkCache at {DateTime.UtcNow}"); _logger.LogInformation($"Starting LoadDatastoreCache at {DateTime.UtcNow}"); LoadDatastoreCache(response.returnval.FindType("Datastore")); _logger.LogInformation($"Finished LoadDatastoreCache at {DateTime.UtcNow}"); }
/** * Create Property Collector Filter to get names of all * ScheduledTasks the ScheduledTaskManager has. * * @return PropertyFilterSpec to get properties */ private PropertyFilterSpec createTaskPropertyFilterSpec() { // The traversal spec traverses the "scheduledTask" property of // ScheduledTaskManager to get names of ScheduledTask ManagedEntities // A Traversal Spec allows traversal into a ManagedObjects // using a single attribute of the managedObject TraversalSpec scheduledTaskTraversal = new TraversalSpec(); scheduledTaskTraversal.type = _scheduleManager.type; scheduledTaskTraversal.path = "scheduledTask"; // We want to get values of the scheduleTask property // of the scheduledTaskManager, which are the ScheduledTasks // so we set skip = false. scheduledTaskTraversal.skip = false; scheduledTaskTraversal.skipSpecified = true; // no further traversal needed once we get to scheduled task list scheduledTaskTraversal.selectSet = new SelectionSpec[] { }; scheduledTaskTraversal.name = "scheduleManagerToScheduledTasks"; // Setup a PropertySpec to return names of Scheduled Tasks so // we can find the named ScheduleTask ManagedEntity to delete // Name is an attribute of ScheduledTaskInfo so // the path set will contain "info.name" PropertySpec propSpec = new PropertySpec(); propSpec.all = false; propSpec.allSpecified = true; propSpec.pathSet = new String[] { "info.name" }; propSpec.type = "ScheduledTask"; // PropertySpecs are wrapped in a PropertySpec array // since we only have a propertySpec for the ScheduledTask, // the only values we will get back are names of scheduledTasks PropertySpec[] propSpecArray = new PropertySpec[] { propSpec }; // Create an Object Spec to specify the starting or root object // and the SelectionSpec to traverse to each ScheduledTask in the // array of scheduledTasks in the ScheduleManager ObjectSpec objSpec = new ObjectSpec(); objSpec.obj = _scheduleManager; objSpec.selectSet = new SelectionSpec[] { scheduledTaskTraversal }; // Set skip = true so properties of ScheduledTaskManager // are not returned, and only values of info.name property of // each ScheduledTask is returned objSpec.skip = true; objSpec.skipSpecified = true; // ObjectSpecs used in PropertyFilterSpec are wrapped in an array ObjectSpec[] objSpecArray = new ObjectSpec[] { objSpec }; // Create the PropertyFilter spec with // ScheduledTaskManager as "root" object PropertyFilterSpec spec = new PropertyFilterSpec(); spec.propSet = propSpecArray; spec.objectSet = objSpecArray; return(spec); }
/// <summary> /// Return MOR /// </summary> /// <param name="folder">ManagedObjectReference</param> /// <param name="type">string</param> /// <param name="name">string</param> /// <returns>ManagedObjectReference</returns> public ManagedObjectReference GetMOREFsInFolderByType(ManagedObjectReference folder, string type, string name) { String propName = "name"; string[] type1 = new string[2]; type1[0] = type; ManagedObjectReference viewManager = cb._connection._sic.viewManager; ManagedObjectReference containerView = cb._connection._service.CreateContainerView(viewManager, folder, type1, true); PropertySpec propertySpec = new PropertySpec(); propertySpec.all = false; propertySpec.type = type; propertySpec.pathSet = new string[] { propName }; TraversalSpec ts = new TraversalSpec(); ts.name = "view"; ts.path = "view"; ts.skip = false; ts.type = "ContainerView"; // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.obj = containerView; objectSpec.skip = true; objectSpec.selectSet = new SelectionSpec[] { ts }; // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.propSet = new PropertySpec[] { propertySpec }; propertyFilterSpec.objectSet = new ObjectSpec[] { objectSpec }; PropertyFilterSpec[] filterspec = new PropertyFilterSpec[3]; filterspec[0] = propertyFilterSpec; ObjectContent[] ocary = cb._svcUtil.retrievePropertiesEx(cb._connection._sic.propertyCollector, filterspec); if (ocary == null || ocary.Length == 0) { return(null); } ObjectContent oc = null; ManagedObjectReference mor = null; DynamicProperty[] propary = null; string propval = null; bool found = false; for (int oci = 0; oci < ocary.Length && !found; oci++) { oc = ocary[oci]; mor = oc.obj; propary = oc.propSet; if ((type == null) || (type != null && cb._svcUtil.typeIsA(type, mor.type))) { if (propary.Length > 0) { propval = (string)propary[0].val; } found = propval != null && name.Equals(propval); propval = null; } } if (!found) { mor = null; } return(mor); }