mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-22 01:39:58 -05:00
Update deploy_ovf_template.py
This commit is contained in:
parent
00fd5eb3e6
commit
285a06b261
@ -18,7 +18,7 @@ __vcenter_version__ = 'VMware Cloud on AWS'
|
|||||||
|
|
||||||
from com.vmware.content.library_client import Item
|
from com.vmware.content.library_client import Item
|
||||||
from com.vmware.vcenter.ovf_client import LibraryItem
|
from com.vmware.vcenter.ovf_client import LibraryItem
|
||||||
from com.vmware.vcenter_client import ResourcePool
|
from com.vmware.vcenter_client import ResourcePool, Folder
|
||||||
from vmware.vapi.vsphere.client import create_vsphere_client
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
||||||
|
|
||||||
from samples.vsphere.common import sample_cli, sample_util
|
from samples.vsphere.common import sample_cli, sample_util
|
||||||
@ -34,24 +34,37 @@ class DeployOvfTemplate:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
|
self.vm_name = 'deployed-vm-' + str(generate_random_uuid())
|
||||||
|
|
||||||
parser = sample_cli.build_arg_parser()
|
parser = sample_cli.build_arg_parser()
|
||||||
|
parser.add_argument('--libitemname',
|
||||||
|
required=True,
|
||||||
|
help='(Required) The name of the library item to deploy. '
|
||||||
|
'The library item should contain an OVF package.')
|
||||||
|
|
||||||
|
parser.add_argument('--vmname',
|
||||||
|
default=self.vm_name,
|
||||||
|
help='(Optional) The name of the Virtual Machine to be deployed. '
|
||||||
|
'Default: "{}"'.format(self.vm_name))
|
||||||
|
|
||||||
parser.add_argument('--resourcepoolname',
|
parser.add_argument('--resourcepoolname',
|
||||||
default='Compute-ResourcePool',
|
default='Compute-ResourcePool',
|
||||||
help='The name of the resource pool to be used.')
|
help='(Optional) The name of the resource pool to be used. '
|
||||||
|
'Default: "Compute-ResourcePool"')
|
||||||
|
|
||||||
parser.add_argument('--libitemname',
|
parser.add_argument('--foldername',
|
||||||
required=True,
|
default='Workloads',
|
||||||
help='The name of the library item to deploy.'
|
help='(Optional) The name of the folder to be used. '
|
||||||
'The library item should contain an OVF package.')
|
'Default: "Workloads"')
|
||||||
|
|
||||||
args = sample_util.process_cli_args(parser.parse_args())
|
args = sample_util.process_cli_args(parser.parse_args())
|
||||||
|
|
||||||
self.vm_id = None
|
self.vm_id = None
|
||||||
self.vm_name = 'deployed-vm-' + str(generate_random_uuid())
|
|
||||||
|
|
||||||
self.lib_item_name = args.libitemname
|
self.lib_item_name = args.libitemname
|
||||||
|
self.vm_name = args.vmname
|
||||||
self.resourcepoolname = args.resourcepoolname
|
self.resourcepoolname = args.resourcepoolname
|
||||||
|
self.foldername = args.foldername
|
||||||
self.cleardata = args.cleardata
|
self.cleardata = args.cleardata
|
||||||
|
|
||||||
# Connect to vAPI Endpoint on vCenter Server
|
# Connect to vAPI Endpoint on vCenter Server
|
||||||
@ -61,16 +74,27 @@ class DeployOvfTemplate:
|
|||||||
|
|
||||||
def deploy_ovf_template(self):
|
def deploy_ovf_template(self):
|
||||||
|
|
||||||
# Build the deployment target with resource pool ID
|
# Build the deployment target with resource pool ID and folder ID
|
||||||
filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname]))
|
rp_filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname]))
|
||||||
resource_pool_summaries = self.client.vcenter.ResourcePool.list(filter_spec)
|
resource_pool_summaries = self.client.vcenter.ResourcePool.list(rp_filter_spec)
|
||||||
if not resource_pool_summaries:
|
if not resource_pool_summaries:
|
||||||
raise ValueError("Resource pool with name '{}' not found".
|
raise ValueError("Resource pool with name '{}' not found".
|
||||||
format(self.resourcepoolname))
|
format(self.resourcepoolname))
|
||||||
resource_pool_id = resource_pool_summaries[0].resource_pool
|
resource_pool_id = resource_pool_summaries[0].resource_pool
|
||||||
print('Resource pool ID: {}'.format(resource_pool_id))
|
print('Resource pool ID: {}'.format(resource_pool_id))
|
||||||
|
|
||||||
|
folder_filter_spec = Folder.FilterSpec(names=set([self.foldername]))
|
||||||
|
folder_summaries = self.client.vcenter.Folder.list(folder_filter_spec)
|
||||||
|
if not folder_summaries:
|
||||||
|
raise ValueError("Folder with name '{}' not found".
|
||||||
|
format(self.foldername))
|
||||||
|
folder_id = folder_summaries[0].folder
|
||||||
|
print('Folder ID: {}'.format(folder_id))
|
||||||
|
|
||||||
deployment_target = LibraryItem.DeploymentTarget(
|
deployment_target = LibraryItem.DeploymentTarget(
|
||||||
resource_pool_id=resource_pool_id)
|
resource_pool_id=resource_pool_id,
|
||||||
|
folder_id=folder_id
|
||||||
|
)
|
||||||
|
|
||||||
# Find the library item
|
# Find the library item
|
||||||
find_spec = Item.FindSpec(name=self.lib_item_name)
|
find_spec = Item.FindSpec(name=self.lib_item_name)
|
||||||
@ -108,8 +132,8 @@ class DeployOvfTemplate:
|
|||||||
|
|
||||||
# The type and ID of the target deployment is available in the deployment result.
|
# The type and ID of the target deployment is available in the deployment result.
|
||||||
if result.succeeded:
|
if result.succeeded:
|
||||||
print('Deployment successful. Result resource: {}, ID: {}'
|
print('Deployment successful. VM Name: "{}", ID: "{}"'
|
||||||
.format(result.resource_id.type, result.resource_id.id))
|
.format(self.vm_name, result.resource_id.id))
|
||||||
self.vm_id = result.resource_id.id
|
self.vm_id = result.resource_id.id
|
||||||
error = result.error
|
error = result.error
|
||||||
if error is not None:
|
if error is not None:
|
||||||
|
Loading…
Reference in New Issue
Block a user