2013-07-01 23 views
2

我試圖從我的VMware ESXi服務器中的DataStores獲取典型屬性(容量,可用空間,名稱)。我無法獲取TraversalSpec,ObjectSpec和PropertySpecs。獲取VMWare DataStore庫存數據(庫存遍歷)

有人可以告訴我我做錯了什麼嗎?

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; 
} 

此外,對象類型名稱是否區分大小寫?當我看樣品時,我似乎看到了各種各樣的情況。

感謝您的任何建議。

回答

1

第一個問題:您可以使用下面的代碼來獲取DataStore的屬性。我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; 
} 

如何運行樣品:

  1. 通過ServiceContent類的對象初始化service通過VimService類的對象和serviceContent
  2. 使用service登錄到vCenter或ESX。
  3. 用您的數據存儲區的密鑰替換<your_datastore_key>。您可以使用Managed Object Browser來查找其數據存儲的密鑰。要獲得數據存儲對象的描述,請轉到MOB中的以下鏈接:content - > rootFolder - > childEntity - > datastoreFolder - > childEntity。頁面頂部的「託管對象ID」的值是正確的密鑰(如datastore-46)。

第二個問題:是的,ManagedObjectReference的類型區分大小寫。

+0

我得到null爲retrieveResult :(我想也許它與datastore-46有關,但即使將它設置爲我的本地數據存儲名稱也沒有幫助(並且我無法硬編碼數據存儲名稱) – DougN

+0

我更新了我的答案,並介紹瞭如何獲取數據存儲區密鑰,只是示例獲取數據存儲區的屬性,如果您需要獲取數據存儲區列表,可以使用MOB,SDK文檔和我的GetProperties()方法實現。 –

+0

對不起,我是這麼一個noob,但是什麼是MOB?管理對象?我一直在閱讀那個Datastore不是一個管理對象。我認爲我的問題是我只是不理解遍歷完成,並且文檔沒有幫助我的cob-webbed大腦:( – DougN