<s> import atexit <EOL> from getpass import getpass <EOL> from pyVim import connect <EOL> """<STR_LIT>""" <EOL> class VVC ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hostname ) : <EOL> """<STR_LIT>""" <EOL> self . hostname = hostname <EOL> def connect ( self , username , password = None ) : <EOL> """<STR_LIT>""" <EOL> if not password : <EOL> password = getpass ( "<STR_LIT>" . format ( self . hostname ) ) <EOL> self . service_instance = connect . SmartConnect ( host = self . hostname , <EOL> user = username , <EOL> pwd = password , <EOL> port = <NUM_LIT> ) <EOL> atexit . register ( connect . Disconnect , self . service_instance ) <EOL> def get_first_level_of_vm_folders ( self ) : <EOL> content = self . service_instance . RetrieveContent ( ) <EOL> children = content . rootFolder . childEntity <EOL> for child in children : <EOL> if hasattr ( child , "<STR_LIT>" ) : <EOL> yield child . vmFolder <EOL> def get_all_vms ( self ) : <EOL> """<STR_LIT>""" <EOL> for folder in self . get_first_level_of_vm_folders ( ) : <EOL> for vm in get_all_vms_in_folder ( folder ) : <EOL> yield vm <EOL> class ESX ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , raw_esx ) : <EOL> self . raw_esx = raw_esx <EOL> self . name = raw_esx . name <EOL> def __eq__ ( self , other ) : <EOL> return self . name == other . name <EOL> def __hash__ ( self ) : <EOL> return int ( "<STR_LIT>" . join ( ( str ( ord ( c ) ) for c in self . name ) ) ) <EOL> def __getattr__ ( self , attribute ) : <EOL> return getattr ( self . raw_esx , attribute ) <EOL> def get_number_of_cores ( self ) : <EOL> """<STR_LIT>""" <EOL> resources_on_esx = self . raw_esx . licensableResource . resource <EOL> for resource in resources_on_esx : <EOL> if resource . key == "<STR_LIT>" : <EOL> return resource . value <EOL> message = "<STR_LIT>" <EOL> raise RuntimeError ( message . format ( self . name , resources_on_esx ) ) <EOL> class VM ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , raw_vm ) : <EOL> self . raw_vm = raw_vm <EOL> self . name = raw_vm . name <EOL> def __getattr__ ( self , attribute ) : <EOL> return getattr ( self . raw_vm , attribute ) <EOL> def get_first_network_interface_matching ( self , predicate ) : <EOL> """<STR_LIT>""" <EOL> for network in self . raw_vm . network : <EOL> if predicate ( network ) : <EOL> return network <EOL> return None <EOL> def get_esx_host ( self ) : <EOL> return ESX ( self . raw_vm . runtime . host ) <EOL> def get_all_vms_in_folder ( folder ) : <EOL> vm_or_folders = folder . childEntity <EOL> for vm_or_folder in vm_or_folders : <EOL> if hasattr ( vm_or_folder , "<STR_LIT>" ) : <EOL> for vm in get_all_vms_in_folder ( vm_or_folder ) : <EOL> yield vm <EOL> else : <EOL> yield VM ( vm_or_folder ) </s>
