2222except ImportError :
2323 import urllib .request as urllib2
2424
25+ import atexit
26+
2527from com .vmware .content .library_client import Item
2628from com .vmware .vcenter .ovf_client import LibraryItem
2729from pyVmomi import vim
2830
31+ from samples .vsphere .common import sample_cli
32+ from samples .vsphere .common import sample_util
2933from samples .vsphere .common .id_generator import generate_random_uuid
30- from samples .vsphere .common .sample_base import SampleBase
34+ from samples .vsphere .common .service_manager import ServiceManager
3135from samples .vsphere .common .vim .helpers .vim_utils import (
3236 get_obj , get_obj_by_moId , poweron_vm , poweroff_vm , delete_object )
3337from samples .vsphere .contentlibrary .lib .cls_api_client import ClsApiClient
3438from samples .vsphere .contentlibrary .lib .cls_api_helper import ClsApiHelper
3539
3640
37- class DeployOvfTemplate ( SampleBase ) :
41+ class DeployOvfTemplate :
3842 """
3943 Demonstrates the workflow to deploy an OVF library item to a resource pool.
4044 Note: the sample needs an existing library item with an OVF template
4145 and an existing cluster with resources for deploying the VM.
4246 """
4347
4448 def __init__ (self ):
45- SampleBase .__init__ (self , self .__doc__ )
4649 self .servicemanager = None
4750 self .client = None
4851 self .helper = None
@@ -51,29 +54,37 @@ def __init__(self):
5154 self .vm_obj = None
5255 self .vm_name = None
5356
54- def _options (self ):
55- self .argparser .add_argument ('-clustername' ,
56- '--clustername' ,
57- help = 'The name of the cluster to be used.' )
58- self .argparser .add_argument ('-libitemname' ,
59- '--libitemname' ,
60- help = 'The name of the library item to deploy.'
61- 'The library item should contain an OVF package.' )
62-
63- def _setup (self ):
64- self .cluster_name = self .args .clustername
65- assert self .cluster_name is not None
57+ def setup (self ):
58+ parser = sample_cli .build_arg_parser ()
59+ parser .add_argument ('-n' , '--vm_name' ,
60+ action = 'store' ,
61+ help = 'Name of the testing vm' )
62+ parser .add_argument ('-clustername' ,
63+ '--clustername' ,
64+ help = 'The name of the cluster to be used.' )
65+ parser .add_argument ('-libitemname' ,
66+ '--libitemname' ,
67+ help = 'The name of the library item to deploy.'
68+ 'The library item should contain an OVF package.' )
69+ args = sample_util .process_cli_args (parser .parse_args ())
70+ self .lib_item_name = args .libitemname
71+ self .cluster_name = args .clustername
72+ self .vm_name = args .vm_name
73+
74+ self .servicemanager = ServiceManager (args .server ,
75+ args .username ,
76+ args .password ,
77+ args .skipverification )
78+ self .servicemanager .connect ()
79+ atexit .register (self .servicemanager .disconnect )
6680
67- self .lib_item_name = self .args .libitemname
68- assert self .lib_item_name is not None
69-
70- self .servicemanager = self .get_service_manager ()
7181 self .client = ClsApiClient (self .servicemanager )
72- self .helper = ClsApiHelper (self .client , self .skip_verification )
82+ self .helper = ClsApiHelper (self .client , args .skipverification )
83+
7384 # Default VM name
7485 self .vm_name = 'vm-' + str (generate_random_uuid ())
7586
76- def _execute (self ):
87+ def execute (self ):
7788
7889 # Find the cluster's resource pool moid
7990 cluster_obj = get_obj (self .servicemanager .content ,
@@ -141,7 +152,7 @@ def deploy_ovf_template(self, lib_item_id, ovf_summary, deployment_target):
141152 for error in result .error .errors :
142153 print ('OVF error: {}' .format (error .message ))
143154
144- def _cleanup (self ):
155+ def cleanup (self ):
145156 if self .vm_obj is not None :
146157 # Power off the VM and wait for the power off operation to complete
147158 poweroff_vm (self .servicemanager .content , self .vm_obj )
@@ -151,7 +162,9 @@ def _cleanup(self):
151162
152163def main ():
153164 deploy_ovf_sample = DeployOvfTemplate ()
154- deploy_ovf_sample .main ()
165+ deploy_ovf_sample .setup ()
166+ deploy_ovf_sample .execute ()
167+ deploy_ovf_sample .cleanup ()
155168
156169
157170if __name__ == '__main__' :
0 commit comments