2018-05-25 05:20:27 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
* *******************************************************
|
|
|
|
* Copyright VMware, Inc. 2018. All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
* *******************************************************
|
|
|
|
*
|
|
|
|
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
|
|
|
|
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
|
|
|
|
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
|
|
|
|
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'VMware, Inc.'
|
|
|
|
__vcenter_version__ = 'VMware Cloud on AWS'
|
|
|
|
|
|
|
|
from com.vmware.content.library_client import Item
|
|
|
|
from com.vmware.vcenter.ovf_client import LibraryItem
|
2018-08-23 18:35:03 -04:00
|
|
|
from com.vmware.vcenter_client import ResourcePool, Folder
|
2018-05-25 05:20:27 -04:00
|
|
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
|
|
|
|
|
|
|
from samples.vsphere.common import sample_cli, sample_util
|
|
|
|
from samples.vsphere.common.id_generator import generate_random_uuid
|
|
|
|
|
|
|
|
|
|
|
|
class DeployOvfTemplate:
|
|
|
|
"""
|
|
|
|
Demonstrates the workflow to deploy an OVF library item to
|
|
|
|
a resource pool in VMware Cloud on AWS.
|
|
|
|
Note: the sample needs an existing library item with an OVF template
|
|
|
|
and an existing resource pool with resources for deploying the VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2018-08-23 18:35:03 -04:00
|
|
|
self.vm_name = 'deployed-vm-' + str(generate_random_uuid())
|
2018-05-25 05:20:27 -04:00
|
|
|
|
2018-08-23 18:35:03 -04:00
|
|
|
parser = sample_cli.build_arg_parser()
|
2018-05-25 05:20:27 -04:00
|
|
|
parser.add_argument('--libitemname',
|
|
|
|
required=True,
|
2018-08-23 18:35:03 -04:00
|
|
|
help='(Required) The name of the library item to deploy. '
|
2018-05-25 05:20:27 -04:00
|
|
|
'The library item should contain an OVF package.')
|
|
|
|
|
2018-08-23 18:35:03 -04:00
|
|
|
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',
|
|
|
|
default='Compute-ResourcePool',
|
|
|
|
help='(Optional) The name of the resource pool to be used. '
|
|
|
|
'Default: "Compute-ResourcePool"')
|
|
|
|
|
|
|
|
parser.add_argument('--foldername',
|
|
|
|
default='Workloads',
|
|
|
|
help='(Optional) The name of the folder to be used. '
|
|
|
|
'Default: "Workloads"')
|
|
|
|
|
2018-05-25 05:20:27 -04:00
|
|
|
args = sample_util.process_cli_args(parser.parse_args())
|
|
|
|
|
|
|
|
self.vm_id = None
|
|
|
|
self.lib_item_name = args.libitemname
|
2018-08-23 18:35:03 -04:00
|
|
|
self.vm_name = args.vmname
|
2018-05-25 05:20:27 -04:00
|
|
|
self.resourcepoolname = args.resourcepoolname
|
2018-08-23 18:35:03 -04:00
|
|
|
self.foldername = args.foldername
|
2018-05-25 05:20:27 -04:00
|
|
|
self.cleardata = args.cleardata
|
|
|
|
|
|
|
|
# Connect to vAPI Endpoint on vCenter Server
|
|
|
|
self.client = create_vsphere_client(server=args.server,
|
|
|
|
username=args.username,
|
|
|
|
password=args.password)
|
|
|
|
|
|
|
|
def deploy_ovf_template(self):
|
|
|
|
|
2018-08-23 18:35:03 -04:00
|
|
|
# Build the deployment target with resource pool ID and folder ID
|
|
|
|
rp_filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname]))
|
|
|
|
resource_pool_summaries = self.client.vcenter.ResourcePool.list(rp_filter_spec)
|
2018-05-25 05:20:27 -04:00
|
|
|
if not resource_pool_summaries:
|
|
|
|
raise ValueError("Resource pool with name '{}' not found".
|
|
|
|
format(self.resourcepoolname))
|
|
|
|
resource_pool_id = resource_pool_summaries[0].resource_pool
|
|
|
|
print('Resource pool ID: {}'.format(resource_pool_id))
|
2018-08-23 18:35:03 -04:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2018-05-25 05:20:27 -04:00
|
|
|
deployment_target = LibraryItem.DeploymentTarget(
|
2018-08-23 18:35:03 -04:00
|
|
|
resource_pool_id=resource_pool_id,
|
|
|
|
folder_id=folder_id
|
|
|
|
)
|
2018-05-25 05:20:27 -04:00
|
|
|
|
|
|
|
# Find the library item
|
|
|
|
find_spec = Item.FindSpec(name=self.lib_item_name)
|
|
|
|
lib_item_ids = self.client.content.library.Item.find(find_spec)
|
|
|
|
if not lib_item_ids:
|
|
|
|
raise ValueError("Library item with name '{}' not found".
|
|
|
|
format(self.lib_item_name))
|
|
|
|
lib_item_id = lib_item_ids[0]
|
|
|
|
print('Library item ID: {}'.format(lib_item_id))
|
|
|
|
ovf_summary = self.client.vcenter.ovf.LibraryItem.filter(
|
|
|
|
ovf_library_item_id=lib_item_id,
|
|
|
|
target=deployment_target)
|
|
|
|
print('Found an OVF template: {} to deploy.'.format(ovf_summary.name))
|
|
|
|
|
|
|
|
# Build the deployment spec
|
|
|
|
deployment_spec = LibraryItem.ResourcePoolDeploymentSpec(
|
|
|
|
name=self.vm_name,
|
|
|
|
annotation=ovf_summary.annotation,
|
|
|
|
accept_all_eula=True,
|
|
|
|
network_mappings=None,
|
|
|
|
storage_mappings=None,
|
|
|
|
storage_provisioning=None,
|
|
|
|
storage_profile_id=None,
|
|
|
|
locale=None,
|
|
|
|
flags=None,
|
|
|
|
additional_parameters=None,
|
|
|
|
default_datastore_id=None)
|
|
|
|
|
|
|
|
# Deploy the ovf template
|
|
|
|
result = self.client.vcenter.ovf.LibraryItem.deploy(
|
|
|
|
lib_item_id,
|
|
|
|
deployment_target,
|
|
|
|
deployment_spec,
|
|
|
|
client_token=generate_random_uuid())
|
|
|
|
|
|
|
|
# The type and ID of the target deployment is available in the deployment result.
|
|
|
|
if result.succeeded:
|
2018-08-23 18:35:03 -04:00
|
|
|
print('Deployment successful. VM Name: "{}", ID: "{}"'
|
|
|
|
.format(self.vm_name, result.resource_id.id))
|
2018-05-25 05:20:27 -04:00
|
|
|
self.vm_id = result.resource_id.id
|
|
|
|
error = result.error
|
|
|
|
if error is not None:
|
|
|
|
for warning in error.warnings:
|
|
|
|
print('OVF warning: {}'.format(warning.message))
|
|
|
|
|
|
|
|
else:
|
|
|
|
print('Deployment failed.')
|
|
|
|
for error in result.error.errors:
|
|
|
|
print('OVF error: {}'.format(error.message))
|
|
|
|
|
|
|
|
def delete_vm(self):
|
|
|
|
if self.cleardata:
|
|
|
|
self.client.vcenter.VM.delete(self.vm_id)
|
|
|
|
print('VM ({}) is deleted'.format(self.vm_id))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
deploy_ovf_sample = DeployOvfTemplate()
|
|
|
|
deploy_ovf_sample.deploy_ovf_template()
|
|
|
|
deploy_ovf_sample.delete_vm()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|