获取VMWare数据存储区资源清册数据(资源清册遍历)
我正在尝试从VMware ESXi服务器中的数据存储中获取典型属性(容量、可用空间、名称)。我在获取TraversalSpec、ObjectSpec和PropertySpec时遇到问题 有人能告诉我我做错了什么吗获取VMWare数据存储区资源清册数据(资源清册遍历),vmware,datastore,esxi,Vmware,Datastore,Esxi,我正在尝试从VMware ESXi服务器中的数据存储中获取典型属性(容量、可用空间、名称)。我在获取TraversalSpec、ObjectSpec和PropertySpec时遇到问题 有人能告诉我我做错了什么吗 public void GetDataStoreValues() { PropertyFilterSpec spec = GetDataStoreQuery(); ObjectContent[] objectContent = _service.RetrievePro
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;
}
此外,对象类型名称是否区分大小写?当我看样品时,我似乎看到了各种各样的情况
谢谢您的建议。第一个问题:您可以使用以下代码获取数据存储的属性。我在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;
}
公共无效测试()
{
var properties=GetProperties(
新ManagedObjectReference{type=“Datastore”,Value=”“},
新[]{“summary.capacity”、“summary.freeSpace”、“summary.name”});
}
私有列表GetProperties(ManagedObjectReference objectRef,字符串[]属性)
{
var typesAndProperties=newdictionary{{objectRef.type,properties};
var objectContents=RetrieveResults(typesAndProperties,新列表{objectRef});
返回ExtractDynamicProperties(objectRef,objectContents);
}
私有列表检索结果(字典类型和属性,列表对象引用)
{
var result=新列表();
var tSpec=new-TraversalSpec{path=“view”,skip=false};
var oSpec=newobjectspec{skip=true,selectSet=newselectionspec[]{tSpec};
oSpec.obj=service.CreateListView(serviceContent.viewManager,objectReferences.ToArray());
tSpec.type=“ListView”;
var fSpec=新属性过滤器spec
{
objectSet=new[]{oSpec},
propSet=typesAndProperties.Keys.Select(typeName=>newpropertyspec{type=typeName,pathSet=typesAndProperties[typeName]})
};
PropertyFilterSpec[]pfs={fSpec};
var retrieveResult=service.RetrievePropertiesEx(serviceContent.propertyCollector,pfs,new RetrieveOptions());
if(retrieveResult!=null)
{
result.AddRange(retrieveResult.objects);
而(!String.IsNullOrEmpty(retrieveResult.token))
{
retrieveResult=service.ContinueRetrievePropertiesEx(serviceContent.propertyCollector,retrieveResult.token);
result.AddRange(retrieveResult.objects);
}
服务视图(oSpec.obj);
}
返回结果;
}
私有静态列表提取器DynamicProperties(ManagedObjectReference objectRef、IEnumerable objectContents)
{
var result=新列表();
foreach(objectContents中的var objectContent)
{
如果(objectContent.propSet==null)继续;
如果(objectContent.obj==null)继续;
如果(objectContent.obj.type!=objectRef.type | | objectContent.obj.Value!=objectRef.Value)继续;
result.AddRange(objectContent.propSet);
}
返回结果;
}
如何运行示例:
VimService
类的对象初始化service
,按serviceContent
类的对象初始化serviceContent
服务登录vCenter或ESX
。您可以使用查找其数据存储的键。要获取数据存储对象的描述,请转到MOB中的以下链接:内容->根文件夹->子实体->数据存储文件夹->子实体。页面顶部的“托管对象ID”值是正确的键(如datastore-46
)第二个问题:是的,ManagedObjectReference的类型区分大小写。我为RetrieverResult返回null:(我认为可能与datastore-46有关,但即使将其设置为本地datastore名称也没有帮助(而且我无法硬编码datastore名称)我更新了答案并描述了如何获取数据存储密钥。这只是获取数据存储属性的示例。如果需要获取数据存储列表,可以使用MOB、SDK文档和我的GetProperties()实现方法。这真的不是一项困难的任务。很抱歉,我是一个noob,但什么是MOB?托管对象?我一直在读数据存储不是托管对象。我想我的问题是我不理解遍历是如何完成的,文档对我的蜘蛛网大脑没有帮助:(MOB是托管对象浏览器,请参阅我答案中的链接。它是一种基于web的实用工具,用于探索数据对象(如数据存储、数据中心、虚拟机、权限等)和从web浏览器调用这些对象的方法。使用此工具,您可以查看托管对象的所有键和类型,然后在代码示例中使用它们来创建ManagedObjectReference。您还可以查看对象的属性和对象之间的关系。在使用TraversalSpec的初始阶段,细微差别并不重要。Using ServiceContent您可以获取任何manager和searchIndex、rootFolder对象(请阅读关于这些对象的SDK文档!)。然后您可以使用VimService调用这些对象的任何方法,并使用GetProperties()获取任何属性。希望它能帮助您开始使用VMware API。如果您遇到问题,请写下来,我会尽力提供帮助。