Python pyvmomi operation VMware (two): get data center, cluster, host, virtual machine data and object specific attribute fields

First use pyVim to connect to vcenter to obtain the operable objects of vcenter:
# -*- coding: utf-8 -*-
from pyVim.connect import SmartConnectNoSSL


class VmManage(object):

    def __init__(self, host, user, password, port, ssl):
        self.host = host
        self.user = user
        self.pwd = password
        self.port = port
        self.sslContext = ssl
        try:
            self.client = SmartConnectNoSSL(host=host,
                                            user=user,
                                            pwd=password,
                                            port=443
                                            )
            self.content = self.client.RetrieveContent()
            self.result = True
        except Exception as e:
            self.result = False
            self.message = e
Add a method to obtain an object in the VmManage class object:
    def _get_all_objs(self, obj_type, folder=None):
        """
        根据对象类型获取这一类型的所有对象
        """
        if folder is None:
            container = self.content.viewManager.CreateContainerView(self.content.rootFolder, obj_type, True)
        else:
            container = self.content.viewManager.CreateContainerView(folder, obj_type, True)
        return container.view


   def _get_obj(self, obj_type, name):
        """
        根据对象类型和名称来获取具体对象
        """
        obj = None
        content = self.client.RetrieveContent()
        container = content.viewManager.CreateContainerView(content.rootFolder, obj_type, True)
        for c in container.view:
            if c.name == name:
                obj = c
                break
        return obj
Get data center object:
    def get_datacenters(self):
        """
       返回所有的数据中心
        """
        return self._get_all_objs([vim.Datacenter])


    def get_datacenter_by_name(self, datacenter_name):
        """
       根据数据中心名称获取数据中心对象
        """
        return self._get_all_objs([vim.Datacenter], datacenter_name)
Get cluster, host, virtual machine
集群对象类型:[vim.ClusterComputeResource]
宿主机对象类型:[vim.HostSystem]
虚拟机对象:[vim.VirtualMachine]
Call the test:
if __name__ == '__main__':
    ip = '192.10.1.1'
    user = 'xxxxxxxx'
    password = 'xxxx'
    port = 443
    vm = VmManage(host=ip,
                  user=user,
                  password=password,
                  port=port, ssl=None)
    cluster_objs = vm._get_all_objs([vim.ClusterComputeResource])
    print cluster_objs

Get the test results of the cluster object:

(ManagedObject) [
   'vim.ClusterComputeResource:domain-c400',
   'vim.ClusterComputeResource:domain-c70'
]

Next, how should I get the attributes of these clusters and how to find the specific fields corresponding to these attributes?

print dir(cluster_objs[0])
注意:在这里可以对获取的对象组中的一个对象查看具有的属性

View the result of the attribute field of the cluster object:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42631707/article/details/100659941