我正在使用C#和VMware.Vim vsphere API进行一些搜索,并且在大多数情况下,它工作得很好,只是我无法弄清楚如何限制数据中心中的特定群集。
例如:
NameValueCollection filter = new NameValueCollection();
filter.Add("Name", vmName2LookFor);
var ret = vimClient.FindEntityViews(typeof(VMware.Vim.VirtualMachine), null, filter, viewProperties);
var VMs = ret.Cast<VMware.Vim.VirtualMachine>().ToList();
我已经看到了使用父属性递归爬树直到达到群集级别的建议,但是在获得更多结果时就扩展而言,这似乎是一个糟糕的主意。
我也看到了有关使用TraversalSpec的参考,但是我真的找不到关于它的任何文档。
有人可以帮我吗?
您如何以高性能的方式获取群集中的所有VM,并且/或者是否有任何文档,或者可以给我一些使用TraversalSpec的示例?
答案 0 :(得分:0)
我最终只是为整个数据中心构建一个host => cluster映射,然后根据该映射检查vm.Runtime.Host属性,以确保vm确实在特定集群下运行。
这最终很有效,但是下面是我有关如何使用TraversalSpec的问题的答案。下面是代码,下面是解释。
public SelectionSpec[] buildTraversalFromDatacenterToVM() {
// Recurse through all ResourcePools
TraversalSpec rpToVm = new TraversalSpec() {
Name = "rpToVm",
Type = "ResourcePool",
Path = "vm",
Skip = false
};
// Recurse through all ResourcePools
TraversalSpec rpToRp = new TraversalSpec() {
Name = "rpToRp",
Type = "ResourcePool",
Path = "resourcePool",
Skip = false,
SelectSet = new[] {
new SelectionSpec() { Name = "rpToRp" },
new SelectionSpec() { Name = "rpToVm" }
}
};
// Traversal through ResourcePool branch
TraversalSpec crToRp = new TraversalSpec() {
Name = "crToRp",
Type = "ComputeResource",
Path = "resourcePool",
Skip = false,
SelectSet = new[] {
new SelectionSpec() { Name = "rpToRp" },
new SelectionSpec() { Name = "rpToVm" },
}
};
// Traversal through host branch
TraversalSpec crToH = new TraversalSpec();
crToH.Name = "crToH";
crToH.Type = "ComputeResource";
crToH.Path = "host";
crToH.Skip = false;
// Traversal through hostFolder branch
TraversalSpec dcToHf = new TraversalSpec() {
Name = "dcToHf",
Type = "Datacenter",
Path = "hostFolder",
Skip = false,
SelectSet = new[] { new SelectionSpec() { Name = "visitFolders" } }
};
// Traversal through vmFolder branch
TraversalSpec dcToVmf = new TraversalSpec() {
Name = "dcToVmf",
Type = "Datacenter",
Path = "vmFolder",
Skip = false,
SelectSet = new[] { new SelectionSpec() { Name = "visitFolders" } }
};
// Recurse through all Hosts
TraversalSpec HToVm = new TraversalSpec() {
Name = "HToVm",
Type = "HostSystem",
Path = "vm",
Skip = false,
SelectSet = new[] { new SelectionSpec() { Name = "visitFolders" } }
};
// Recurse through the folders
TraversalSpec visitFolders = new TraversalSpec();
visitFolders.Name = "visitFolders";
visitFolders.Type = "Folder";
visitFolders.Path = "childEntity";
visitFolders.Skip = false;
visitFolders.SelectSet = new[] {
new SelectionSpec() { Name ="visitFolders" },
new SelectionSpec() { Name ="dcToHf" },
new SelectionSpec() { Name ="dcToVmf" },
new SelectionSpec() { Name ="crToH" },
new SelectionSpec() { Name ="crToRp" },
new SelectionSpec() { Name ="HToVm" },
new SelectionSpec() { Name ="rpToVm" },
new SelectionSpec() { Name ="HToVm" },
};
return new SelectionSpec[] { visitFolders, dcToVmf, dcToHf, crToH, crToRp, rpToRp, HToVm, rpToVm };
}
public PropertyFilterSpec[] GetPropertyFilterSpec(ManagedObjectReference root,PropertySpec[] propSpecs) {
if (typeinfo == null || typeinfo.Length == 0) {
return null;
}
var selectionSpecs = buildTraversalFromDatacenterToVM();
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.PropSet = propSpecs;
spec.ObjectSet = new ObjectSpec[] {
new ObjectSpec() {
Obj = root,
Skip = false,
SelectSet = selectionSpecs
}
};
return new PropertyFilterSpec[] { spec }
}
以及使用上述功能的方法
// NOTE: setting All to true will return everything for the object, setting it to false
// and filling out the PathSet will instead only return the properties specified
// for performance. For example, below for the HostSystem objects, everything
// on the object will be null except for the name and the parent members
//
var propSpecs = new[] {
new PropertySpec() {
All = true,
Type = "VirtualMachine"
},
new PropertySpec() {
All = false,
Type = "HostSystem",
PathSet = new[]{ "name", "parent" }
}
};
VimClient vimClient = new VMware.Vim.VimClientImpl();
var serviceContent = vimClient.Connect(hostname, VMware.Vim.CommunicationProtocol.Https, null);
var userSession = vimClient.Login(un, pwd);
var propertyCollector = new PropertyCollector(vimClient, serviceContent.PropertyCollector);
var specs = GetPropertyFilterSpec(serviceContent.RootFolder,propSpecs);
ObjectContent[] objContents = propertyCollectionr.RetrieveProperties(specs);
现在解释。
由于vmware基础结构基本上是一个图,因此TraversalSpec描述了如何爬该图。分配的名称是用户定义的(任何您想要的名称),但可用于引用特定的递归规范。换句话说,它很像引用或指针。例如,VisitFolders规范通过selectionSpec引用自身。这意味着它可以支持嵌套文件夹(多个级别,而不仅仅是1个)。
请注意,您可以比我走得更远(进入数据存储库等),但是我不需要走那么远。
一旦您描述了如何在图形中进行爬网,就必须告诉它要返回哪些属性,这就是propertySpec的作用。
PropertySpec有2个用途。 1.它充当准过滤器,如果您没有该实体的PropertySpec,则vmware API不会为其返回任何内容。如果您确实有PropertySpec,则它将仅返回您要求的属性。
因此在上面的示例中,它将返回遍历中遇到的VM的所有属性,并且仅返回遍历中遇到的所有主机的名称和父属性。因为我没有为文件夹指定任何属性,所以请求将遍历文件夹,但不会为它们返回任何内容。
这是有关vmware层次结构的文档
以及各种规格类型的文档