<s> from pyVmomi import vim <EOL> def get_managed_entity ( content , vimtype , moid = None , name = None ) : <EOL> if not name and not moid : <EOL> return <EOL> container = content . viewManager . CreateContainerView ( <EOL> content . rootFolder , [ vimtype ] , True ) <EOL> count = <NUM_LIT:0> <EOL> for entity in container . view : <EOL> if moid and entity . _moId == moid : <EOL> results = entity <EOL> count += <NUM_LIT:1> <EOL> elif name and entity . name == name : <EOL> results = entity <EOL> count += <NUM_LIT:1> <EOL> if count >= <NUM_LIT:2> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> elif count == <NUM_LIT:1> : <EOL> return results <EOL> if name : <EOL> raise Exception ( "<STR_LIT>" <EOL> % ( vimtype , name ) ) <EOL> elif moid : <EOL> raise Exception ( "<STR_LIT>" <EOL> % ( vimtype , moid ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> % vimtype ) <EOL> def get_managed_entities ( content , vimtype ) : <EOL> container = content . viewManager . CreateContainerView ( <EOL> content . rootFolder , [ vimtype ] , True ) <EOL> return container <EOL> def get_datacenter ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . Datacenter , moid = moid , name = name ) <EOL> def get_cluster ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . ClusterComputeResource , <EOL> moid = moid , name = name ) <EOL> def get_folder ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . Folder , <EOL> moid = moid , name = name ) <EOL> def get_resource_pool ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . ResourcePool , <EOL> moid = moid , name = name ) <EOL> def get_datastore_cluster ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . StoragePod , <EOL> moid = moid , name = name ) <EOL> def get_datastore ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . Datastore , <EOL> moid = moid , name = name ) <EOL> def get_network ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . Network , <EOL> moid = moid , name = name ) <EOL> def get_virtualmachine ( content , moid = None , name = None ) : <EOL> return get_managed_entity ( content , vim . VirtualMachine , <EOL> moid = moid , name = name ) <EOL> def get_virtualmachines ( content ) : <EOL> return get_managed_entities ( content , vim . VirtualMachine ) <EOL> def get_task ( content , moid = None ) : <EOL> return get_managed_entity ( content , vim . Task , <EOL> moid = moid , name = None ) </s>
