resourcepoolid - ranjanm1/terravm GitHub Wiki
#!/usr/bin/env python """ Return Cluster Roor resourcePool Name """ from pyVmomi import vim from pyVim.connect import SmartConnect, Disconnect import atexit import argparse import getpass
def get_args(): """ Get arguments from CLI """ parser = argparse.ArgumentParser( description='Arguments for talking to vCenter')
parser.add_argument('-s', '--host',
required=True,
action='store',
help='vSpehre service to connect to')
parser.add_argument('-o', '--port',
type=int,
default=443,
action='store',
help='Port to connect on')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='Username to use')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use')
parser.add_argument('-v', '--vm-name',
required=False,
action='store',
help='Name of the VM you wish to make')
parser.add_argument('--template',
required=False,
action='store',
help='Name of the template/VM \
you are cloning from')
parser.add_argument('--datacenter-name',
required=False,
action='store',
default=None,
help='Name of the Datacenter you\
wish to use. If omitted, the first\
datacenter will be used.')
parser.add_argument('--vm-folder',
required=False,
action='store',
default=None,
help='Name of the VMFolder you wish\
the VM to be dumped in. If left blank\
The datacenter VM folder will be used')
parser.add_argument('--datastore-name',
required=False,
action='store',
default=None,
help='Datastore you wish the VM to end up on\
If left blank, VM will be put on the same \
datastore as the template')
parser.add_argument('--datastorecluster-name',
required=False,
action='store',
default=None,
help='Datastorecluster (DRS Storagepod) you wish the VM to end up on \
Will override the datastore-name parameter.')
parser.add_argument('--cluster-name',
required=False,
action='store',
default=None,
help='Name of the cluster you wish the VM to\
end up on. If left blank the first cluster found\
will be used')
parser.add_argument('--resource-pool',
required=False,
action='store',
default=None,
help='Resource Pool to use. If left blank the first\
resource pool found will be used')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass(
prompt='Enter password')
return args
def get_obj(content, vimtype, name): """ Return an object by name, if name is None the first found object is returned """ obj = None container = content.viewManager.CreateContainerView( content.rootFolder, vimtype, True) for c in container.view: if name: if c.name == name: obj = c break else: obj = c break
return obj
def print_it( content, template, vm_name, si, datacenter_name, vm_folder, datastore_name, cluster_name, resource_pool):
# if None, get the first one
cluster = get_obj(content, [vim.ClusterComputeResource], cluster_name)
if resource_pool:
resource_pool = get_obj(content, [vim.ResourcePool], resource_pool)
else:
resource_pool = cluster.resourcePool
print resource_pool
def main(): """ Let this thing fly """ args = get_args()
# connect this thing
si = SmartConnect(
host=args.host,
user=args.user,
pwd=args.password,
port=args.port)
# disconnect this thing
atexit.register(Disconnect, si)
content = si.RetrieveContent()
print_it(content, args.template, args.vm_name, si, args.datacenter_name, args.vm_folder, args.datastore_name, args.cluster_name, args.resource_pool)
start this thing
if name == "main": main()