vmware获取主机、数据中心等对象ManagedObjectReference

时间:2023-12-29 13:24:56

在vmware的api中提供以下列表中的对象,称作ManagedObjectReference,包括虚拟机信息、主机、数据中心等等一些信息,我们可以通过vcenter的web api得到。

下面我们来通过它的api获取dc。

 List<String> arguments = new List<string>();
arguments.AddRange(
new string[]{
"--url", "https://192.168.0.160/sdk",
"--username", "administrator@vsphere.local",
"--password", "P@ssw0rd",
//"--datacentername","dc",
//"--srcname", "",
//"--vmname", "",
//"--hostname","",
//"--storagename","",
//"--disksize","",
//"--privateip","",
//"--privatesubnetmask", "",
//"--cpunums","",
//"--memorymb","",
"--disablesso", "true",
"--ignorecert", "true"
});
try
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(); AppUtil.AppUtil util = AppUtil.AppUtil.initialize("SearchIndex", constructOptions(), arguments.ToArray());
util.connect();
  
ManagedObjectReference hostMor = util._svcUtil.GetDecendentMoRef(null, "HostSystem", "192.168.0.161");
sb.AppendLine(string.Format("host is {0}<br />", hostMor.Value));
ManagedObjectReference dcMor = util._svcUtil.getEntityByName("Datacenter", "dc");
sb.AppendLine(string.Format("dc is {0}<br />", dcMor.Value)); ManagedObjectReference sMor = new ManagedObjectReference();
sMor.type = "ServiceInstance";
sMor.Value = "ServiceInstance";
VimService service = new VimService();
service.Url = util.get_option("url");
service.CookieContainer = new System.Net.CookieContainer();
ServiceContent sc = service.RetrieveServiceContent(sMor);
service.Login(sc.sessionManager, util.get_option("username"), util.get_option("password"), null); ManagedObjectReference searchMor = sc.searchIndex;
ManagedObjectReference dcMor2 = service.FindByInventoryPath(searchMor, "dc");
sb.AppendLine(string.Format("dc2 is {0}<br />", dcMor2.Value)); util.disConnect();
service.Dispose(); Response.Write(sb.ToString()); }
catch (Exception ex) { Response.Write(ex.ToString()); }

上述代码中我们运用了两种不同的代码方式获取数据中心对象,下面是它的运行结果,我们可以看到vcenter在内部用了别称。

vmware获取主机、数据中心等对象ManagedObjectReference

在vcenter server中封装了http://ip/sdk的api,开发者可以直接调用。

每个实体下面具有一些属性,文档可以查询Data Object ManagedObjectReference。