0
我正在使用vSphere API檢索vCenter中的所有虛擬機。但是,我沒有發現任何官方文檔或代碼示例提及批處理/分頁請求。我的代碼如下:有沒有辦法通過vSphere API執行批處理?
VimClient client = new VimClient();
client.Connect(host, CommunicationProtocol.Https, 443);
client.Login(userName, password);
IList<EntityViewBase> vms = client.FindEntityViews(typeof(VirtualMachine), null, null, null);
我檢查了由ReSharper反編譯的源代碼。有趣的是,有一個可爲空的參數「beginEntity」;但是沒有辦法指定要檢索的實體的數量。他們是否完全沒有希望支持批次?
public List<EntityViewBase> FindEntityViews(Type viewType, ManagedObjectReference beginEntity, NameValueCollection filter, string[] properties)
{
IList<ViewBase> viewBaseList = (IList<ViewBase>) null;
ManagedObjectReference beginEntity1 = beginEntity ?? this._serviceContent.RootFolder;
string[] strArray = (string[]) null;
if (filter != null && filter.Count > 0)
{
string[] propertyList = new string[filter.Count];
filter.AllKeys.CopyTo((Array) propertyList, 0);
strArray = VimClient.ValidatePropertyPathList(viewType, propertyList);
}
ObjectContent[] objectContentArray = new PropertyCollector(this, this._serviceContent.PropertyCollector).RetrieveProperties(new PropertyFilterSpec[1]
{
EntityViewBase.GetSearchFilterSpec(this, beginEntity1, new PropertySpec()
{
All = new bool?(false),
Type = viewType.Name,
PathSet = strArray
})
});
List<ManagedObjectReference> managedObjectReferenceList = new List<ManagedObjectReference>();
if (objectContentArray != null)
{
foreach (ObjectContent objectContent in objectContentArray)
{
if (beginEntity == null || !objectContent.Obj.Value.Equals(beginEntity.Value) || !objectContent.Obj.Type.Equals(beginEntity.Type))
{
if (filter != null && filter.Count > 0)
{
DynamicProperty[] propSet = objectContent.PropSet;
if (propSet != null)
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
foreach (DynamicProperty dynamicProperty in propSet)
dictionary.Add(dynamicProperty.Name, dynamicProperty.Val);
if (dictionary.Count > 0 && VimClient.MatchProperyList(filter, viewType, (IDictionary<string, object>) dictionary))
managedObjectReferenceList.Add(objectContent.Obj);
}
}
else
managedObjectReferenceList.Add(objectContent.Obj);
}
}
}
if (managedObjectReferenceList.Count > 0)
viewBaseList = (IList<ViewBase>) this.GetViewsByMorefs((IEnumerable<ManagedObjectReference>) managedObjectReferenceList, properties);
List<EntityViewBase> entityViewBaseList = (List<EntityViewBase>) null;
if (viewBaseList != null)
{
entityViewBaseList = new List<EntityViewBase>();
foreach (ViewBase viewBase in (IEnumerable<ViewBase>) viewBaseList)
{
EntityViewBase entityViewBase = viewBase as EntityViewBase;
entityViewBaseList.Add(entityViewBase);
}
}
return entityViewBaseList;
}
public List<ViewBase> GetViewsByMorefs(IEnumerable<ManagedObjectReference> moRefs, params string[] properties)
{
Dictionary<string, PropertyFilterSpec> propertyFilterSpecList = new Dictionary<string, PropertyFilterSpec>();
foreach (ManagedObjectReference moRef in moRefs)
{
if (propertyFilterSpecList.ContainsKey(moRef.Type.ToLower()))
{
PropertyFilterSpec propertyFilterSpec = propertyFilterSpecList[moRef.Type.ToLower()];
propertyFilterSpec.ObjectSet = new List<ObjectSpec>((IEnumerable<ObjectSpec>) propertyFilterSpec.ObjectSet)
{
new ObjectSpec() { Obj = moRef }
}.ToArray();
}
else
{
PropertyFilterSpec resultPropertyFilterSpec;
Dictionary<string, List<string>> currentAllowedPropertyPath;
DynamicPropertyFilterSpecGenerator.Generate(moRef, properties, out resultPropertyFilterSpec, out currentAllowedPropertyPath);
propertyFilterSpecList.Add(moRef.Type.ToLower(), resultPropertyFilterSpec);
}
}
PropertyFilterSpec[] propertyFilterSpecArray = new PropertyFilterSpec[propertyFilterSpecList.Values.Count];
propertyFilterSpecList.Values.CopyTo(propertyFilterSpecArray, 0);
ObjectContent[] objectContent = new PropertyCollector(this, this._serviceContent.PropertyCollector).RetrieveProperties(propertyFilterSpecArray);
List<ViewBase> viewsByMorefs = this.GetViewsByMorefs(moRefs, objectContent, propertyFilterSpecList);
return viewsByMorefs;
}
private List<ViewBase> GetViewsByMorefs(IEnumerable<ManagedObjectReference> moRefs, ObjectContent[] objectContent, Dictionary<string, PropertyFilterSpec> propertyFilterSpecList)
{
List<ViewBase> viewBaseList = new List<ViewBase>();
List<string> stringList = new List<string>();
foreach (ManagedObjectReference moRef in moRefs)
stringList.Add(moRef.Value);
Dictionary<string, ObjectContent> objectContentList = new Dictionary<string, ObjectContent>();
foreach (ObjectContent objectContent1 in objectContent)
objectContentList.Add(objectContent1.Obj.Value, objectContent1);
Dictionary<string, ViewBase> generatedManagedObjectList = new Dictionary<string, ViewBase>();
foreach (ObjectContent objectContent1 in objectContent)
{
if (stringList.Contains(objectContent1.Obj.Value))
{
ConstructorInfo constructor = ViewBase.GetViewType(objectContent1.Obj.Type).GetConstructor(new Type[2]
{
typeof (VimClient),
typeof (ManagedObjectReference)
});
if (constructor != null)
{
ViewBase currentObject = (ViewBase) constructor.Invoke(new object[2]
{
(object) this,
(object) objectContent1.Obj
});
ViewBase.SetViewData(currentObject, (string) null, (Dictionary<string, List<string>>) null, objectContentList, generatedManagedObjectList);
viewBaseList.Add(currentObject);
}
}
}
return viewBaseList;
}
'RetrievePropertiesEx'和'ContinueRetrievePropertiesEx'適合我!然而,我需要實現我自己的'FindEntityViews'方法,它使用了很多非公共方法(比如'VimClient.ValidatePropertyPathList')。對此有何建議? –