简体   繁体   中英

Unable to construct python class

I have the following functions, that are working within the boundaries on the Python script

The loop_vcenters function will return a dictinary with cluster: host subs_per_socket function will return the number of sockets for esx

def loop_vcenters(vcenters):
    #------------------------- connect to vcenter ----------------------------------------
    si = SmartConnect(host = vcenters,user = 'username',pwd = 'password' ,sslContext=context)
    # disconnect from vcenter one done
    atexit.register(Disconnect, si)
    #get the object content from vcenter
    content = si.RetrieveContent()
    #list clusters
    cluster_host_dic_list=[]
    cluster_name = ""
    for cluster_obj in get_obj(content, vim.ComputeResource):
        cluster=cluster_obj.name
        hosts=[]
        for host in cluster_obj.host:
            hosts.append(host.name)
        #create dictinary ,key=cluster value=esx
        cluster_dic={cluster:hosts}
        cluster_host_dic_list.append(cluster_dic)
    return cluster_host_dic_list

def subs_per_socket(host):
    shost = content.searchIndex.FindByDnsName(dnsName=host, vmSearch=False)
    socket_count = shost.summary.hardware.numCpuPkgs
    sub_per_socket = socket_count/2
    return sub_per_socket

I want to put both functions into a class, but I cannot figure out how

class loop_vcenters:    
    def hosts_dic(self,name):
        si = SmartConnect(host = vcenters,user = 'username',pwd = 'password' ,sslContext=context)
        atexit.register(Disconnect, si)
        content = si.RetrieveContent()
        cluster_host_dic_list=[]
        cluster_name = ""
        for cluster_obj in get_obj(content, vim.ComputeResource):
            cluster=cluster_obj.name
            hosts=[]
            for host in cluster_obj.host:
                hosts.append(host.name)
            cluster_dic={cluster:hosts}
            cluster_host_dic_list.append(cluster_dic)
      return cluster_host_dic_list
  1. I am unable to get the host dictionary like the loop_vcenters function returned.
d = loop_vcenters('vcenters')
Traceback (most recent call last):
File "", line 1, in 
  File "", line 5, in __init__
NameError: global name 'vcenters' is not defined
  1. How can I add the subs_per_socket(host) function to the class?
d = loop_vcenters('vcenters')

You are calling the class loop_vcenters with an argument when no init is defined.

File "", line 5, in __init__

NameError: global name 'vcenters' is not defined

If you want to pass the argument to host_dic, you should be calling

d = loop_vcenters.host_dic('vcenters')

which will return cluster_host_dic_list to the variable d.

To add subs_per_socket(host), just define it under the class just as you did the other function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM