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

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

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

 1 List<String> arguments = new List<string>();
 2 arguments.AddRange(
 3     new string[]{
 4     "--url", "https://192.168.0.160/sdk",
 5     "--username", "administrator@vsphere.local", 
 6     "--password", "P@ssw0rd", 
 7     //"--datacentername","dc",
 8     //"--srcname", "", 
 9     //"--vmname", "",
10     //"--hostname","",
11     //"--storagename","",
12     //"--disksize","",
13     //"--privateip","",
14     //"--privatesubnetmask", "",
15     //"--cpunums","",
16     //"--memorymb","",
17     "--disablesso", "true",
18     "--ignorecert", "true"
19 });
20 try
21 {
22     System.Text.StringBuilder sb = new System.Text.StringBuilder();
23 
24     AppUtil.AppUtil util = AppUtil.AppUtil.initialize("SearchIndex", constructOptions(), arguments.ToArray());
25     util.connect();
26   
27     ManagedObjectReference hostMor = util._svcUtil.GetDecendentMoRef(null, "HostSystem", "192.168.0.161");
28     sb.AppendLine(string.Format("host is {0}<br />", hostMor.Value));
29     ManagedObjectReference dcMor = util._svcUtil.getEntityByName("Datacenter", "dc");
30     sb.AppendLine(string.Format("dc is {0}<br />", dcMor.Value));
31 
32     ManagedObjectReference sMor = new ManagedObjectReference();
33     sMor.type = "ServiceInstance";
34     sMor.Value = "ServiceInstance";
35     VimService service = new VimService();
36     service.Url = util.get_option("url");
37     service.CookieContainer = new System.Net.CookieContainer();
38     ServiceContent sc = service.RetrieveServiceContent(sMor);
39     service.Login(sc.sessionManager, util.get_option("username"), util.get_option("password"), null);
40 
41     ManagedObjectReference searchMor = sc.searchIndex;
42     ManagedObjectReference dcMor2 = service.FindByInventoryPath(searchMor, "dc");
43     sb.AppendLine(string.Format("dc2 is {0}<br />", dcMor2.Value));
44 
45     util.disConnect();
46     service.Dispose();
47 
48     Response.Write(sb.ToString());
49 
50 }
51 catch (Exception ex) { Response.Write(ex.ToString()); }

 

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

1

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

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

 

posted @ 2014-03-31 12:37  猫不理饼  阅读(3582)  评论(8编辑  收藏  举报