Вопрос

I'm attempting to get typical properties (capacity, free space, name) from the DataStores in my VMware ESXi server. I'm having trouble getting the TraversalSpec, ObjectSpec and PropertySpecs.

Can someone please tell me what I'm doing wrong?

public void GetDataStoreValues()
{
    PropertyFilterSpec spec = GetDataStoreQuery();

    ObjectContent[] objectContent = _service.RetrieveProperties(_sic.propertyCollector, new[] { spec } );
    foreach (ObjectContent content in objectContent)
    {
        if (content.obj.type == "DataStore")
        {
            //... get values
        }
    }
}

private PropertyFilterSpec GetDataStoreQuery()
{
    try
    {
        // Traversal to get to the host from ComputeResource
        TraversalSpec tSpec = new TraversalSpec
        {
            name = "HStoDS",
            type = "HostSystem",
            path = "dataStore",
            skip = false
        };

        // Now create Object Spec
        var objectSpec = new ObjectSpec
        {
            obj = _sic.rootFolder,
            skip = true,
            selectSet = new SelectionSpec[] { tSpec }
        };
        var objectSpecs = new[] { objectSpec };

        // Create PropertyFilterSpec using the PropertySpec and ObjectPec
        // created above.
        // Create Property Spec
        string[] propertyArray = new[] {
                                        "summary.capacity"
                                        ,"summary.freeSpace"
                                        ,"summary.name"
                                      };
        var propertySpec = new PropertySpec
        {
            all = true,
            pathSet = propertyArray,
            type = "DataStore"
        };
        var propertySpecs = new[] { propertySpec };

        var propertyFilterSpec = new PropertyFilterSpec
        {
            propSet = propertySpecs,
            objectSet = objectSpecs
        };

        return propertyFilterSpec;
    }
    catch (Exception)
    {
    }
    return null;
}

Also, are object type names case sensitive? I seem to see all sorts of cases when I look at samples.

Thanks for any suggestions.

Это было полезно?

Решение

First question: You can use the following code to get properties of DataStore. I tested this code on vCenter 5.1

public void Test()
{
    var properties = GetProperties(
        new ManagedObjectReference { type = "Datastore", Value = "<your_datastore_key>" },
        new[] {"summary.capacity", "summary.freeSpace", "summary.name"});
}

private List<DynamicProperty> GetProperties(ManagedObjectReference objectRef, string[] properties)
{
    var typesAndProperties = new Dictionary<string, string[]> { { objectRef.type, properties } };
    var objectContents = RetrieveResults(typesAndProperties, new List<ManagedObjectReference> { objectRef });
    return ExtractDynamicProperties(objectRef, objectContents);
}

private List<ObjectContent> RetrieveResults(Dictionary<string, string[]> typesAndProperties, List<ManagedObjectReference> objectReferences)
{
    var result = new List<ObjectContent>();
    var tSpec = new TraversalSpec { path = "view", skip = false };
    var oSpec = new ObjectSpec { skip = true, selectSet = new SelectionSpec[] { tSpec } };
    oSpec.obj = service.CreateListView(serviceContent.viewManager, objectReferences.ToArray());
    tSpec.type = "ListView";

    var fSpec = new PropertyFilterSpec
    {
        objectSet = new[] { oSpec },
        propSet = typesAndProperties.Keys.Select(typeName => new PropertySpec { type = typeName, pathSet = typesAndProperties[typeName] }).ToArray()
    };

    PropertyFilterSpec[] pfs = { fSpec };
    var retrieveResult = service.RetrievePropertiesEx(serviceContent.propertyCollector, pfs, new RetrieveOptions());
    if (retrieveResult != null)
    {
        result.AddRange(retrieveResult.objects);
        while (!String.IsNullOrEmpty(retrieveResult.token))
        {
            retrieveResult = service.ContinueRetrievePropertiesEx(serviceContent.propertyCollector, retrieveResult.token);
            result.AddRange(retrieveResult.objects);
        }
        service.DestroyView(oSpec.obj);
    }
    return result;
}

private static List<DynamicProperty> ExtractDynamicProperties(ManagedObjectReference objectRef, IEnumerable<ObjectContent> objectContents)
{
    var result = new List<DynamicProperty>();
    foreach (var objectContent in objectContents)
    {
        if (objectContent.propSet == null) continue;
        if (objectContent.obj == null) continue;
        if (objectContent.obj.type != objectRef.type || objectContent.obj.Value != objectRef.Value) continue;
        result.AddRange(objectContent.propSet);
    }
    return result;
}

How to run the sample:

  1. Initialize the service by object of VimService class and the serviceContent by object of ServiceContent class.
  2. Login to vCenter or ESX using service.
  3. Replace <your_datastore_key> with Key of your datastore. You can use the Managed Object Browser to find keys of their datastores. To get a description of datastore object, go to following links in MOB: content -> rootFolder -> childEntity -> datastoreFolder -> childEntity. Value of "Managed Object ID" on top of page is correct Key (like datastore-46).

Second question: Yes, the type of ManagedObjectReference is case sensitive.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top