兩年前的一個老項目了,基於VMware Infrastructure 3.5的,整理一下當時的技術資料。至於VMware Infrastructure是什麼以及它能幹什麼,不詳細介紹了,感興趣的同窗能夠本身百度一下。瀏覽器
一、經過什麼方式訪問VI中心session
VMware Infrastructure對外提供WebService,供第三方應用調用,以實現針對主機、虛擬機等資源的控制。針對.Net,提供了一個VimService2003.dll,開發時須要把它加入項目引用。異步
二、如何查看VI集羣中各資源的信息ide
除了使用Vmware Infrastructure Client之外,可使用瀏覽器查看,地址是http://localhost:8080/mob。 其中的8080是vfxd服務設置的監聽端口。spa
三、如何登陸VI中心對象
- public class ViDemo
- {
- //如下是VI開發中會用到的全部對象
- private VimService m_Service;
- private ServiceContent m_Content;
- private ManagedObjectReference m_SvcRef;
- private ManagedObjectReference m_Collector;
- private UserSession m_Session;
- /// <summary>
- /// 登陸,耗時會比較長
- /// </summary>
- public void Connect()
- {
- m_SvcRef = new ManagedObjectReference();
- m_SvcRef.type = "ServiceInstance";
- m_SvcRef.Value = "ServiceInstance";
- m_Service = new VimService();
- m_Service.Url = "http://localhost:8080/sdk";
- m_Content = m_Service.RetrieveServiceContent(m_SvcRef);
- m_Collector = m_Content.propertyCollector;
- if(m_Content.sessionManager != null)
- {
- m_Session = m_Service.Login(m_Content.sessionManager, "USER", "PASS", null);
- }
- }
- /// <summary>
- /// 註銷
- /// </summary>
- public void Disconnect()
- {
- if(m_Service != null)
- {
- m_Service.Logout(m_Content.sessionManager);
- m_Service.Dispose();
- m_Service = null;
- m_Content = null;
- m_Session = null;
- }
- }
- }
四、虛擬機開機及關機資源
- /// <summary>
- /// 虛擬機關機
- /// </summary>
- public void PowerOff()
- {
- //虛擬機的資源路徑,格式是「中心/vm/虛擬機名稱」,本例中中心叫DataCenter,虛擬機是vm100
- //不用關心虛擬機在哪臺主機上,由於對VI來講,集羣是一個總體
- string path = "DataCenter/vm/vm100";
- //根據虛擬機的資源路徑獲取資源的引用
- ManagedObjectReference vmRef = m_Service.FindByInventoryPath(m_Content.searchIndex, path);
- if(vmRef != null)
- {
- //調用服務上的PowerOffVM_Task來關閉虛擬機(異步),此處沒有等待任務完成
- //若是是開機,調用PowerOnVM_Task方法
- ManagedObjectReference taskRef = m_Service.PowerOffVM_Task(vmRef);
- }
- }
未完待續……開發