@@ -182,9 +182,27 @@ def clone(self):
182
182
cluster = self .get_obj ([vim .ClusterComputeResource ],
183
183
ip_settings [0 ]['cluster' ]
184
184
)
185
- # use same root resource pool that my desired cluster uses
186
- resource_pool = cluster .resourcePool
185
+
186
+ resource_pool_str = self .config ['resource_pool' ]
187
+
188
+ # resource_pool setting in config file takes priority over the
189
+ # default 'Resources' pool
190
+ if resource_pool_str == 'Resources' and ('resource_pool' in ip_settings [key ]):
191
+ resource_pool_str = ip_settings [key ]['resource_pool' ]
192
+
193
+
194
+ resource_pool = self .get_resource_pool (cluster , resource_pool_str )
195
+
196
+ if resource_pool is None :
197
+ print "Error: Unable to find Resource Pool '%s'" % resource_pool_str
198
+ sys .exit (1 )
199
+
187
200
datastore = self .get_obj ([vim .Datastore ], ip_settings [0 ]['datastore' ])
201
+
202
+ if datastore is None :
203
+ print "Error: Unable to find Datastore '%s'" % ip_settings [0 ]['datastore' ]
204
+ sys .exit (1 )
205
+
188
206
template_vm = self .get_vm_failfast (self .config ['template' ], False , 'Template VM' )
189
207
190
208
# Relocation spec
@@ -201,7 +219,7 @@ def clone(self):
201
219
# don't clone nic devices from template
202
220
for device in template_vm .config .hardware .device :
203
221
if hasattr (device , 'addressType' ):
204
- # this is a VirtualEthernetCard, so we'll delete it,
222
+ # this is a VirtualEthernetCard, so we'll delete it
205
223
nic = vim .vm .device .VirtualDeviceSpec ()
206
224
nic .operation = vim .vm .device .VirtualDeviceSpec .Operation .remove
207
225
nic .device = device
@@ -241,7 +259,7 @@ def clone(self):
241
259
guest_map .adapter .ip .ipAddress = str (ip_settings [key ]['ip' ])
242
260
guest_map .adapter .subnetMask = str (ip_settings [key ]['subnet_mask' ])
243
261
244
- # these may not be set for certain IPs, e.g. storage IPs
262
+ # these may not be set for certain IPs
245
263
try :
246
264
guest_map .adapter .gateway = ip_settings [key ]['gateway' ]
247
265
except :
@@ -484,14 +502,56 @@ def send_email(self):
484
502
s .quit ()
485
503
486
504
'''
487
- Get the vsphere object associated with a given text name
505
+ Find a resource pool given a pool name for desired cluster
488
506
'''
489
- def get_obj (self , vimtype , name ):
490
- obj = None
507
+ def get_resource_pool (self , cluster , pool_name ):
508
+ pool_obj = None
509
+
510
+ # get a list of all resource pools in this cluster
511
+ cluster_pools_list = cluster .resourcePool .resourcePool
512
+
513
+ # get list of all resource pools with a given text name
514
+ pool_selections = self .get_obj ([vim .ResourcePool ], pool_name , return_all = True )
515
+
516
+ # get the first pool that exists in a given cluster
517
+ for p in pool_selections :
518
+ if p in cluster_pools_list :
519
+ pool_obj = p
520
+ break
521
+
522
+ return pool_obj
523
+
524
+ '''
525
+ Get the vsphere object associated with a given text name
526
+ '''
527
+ def get_obj (self , vimtype , name , return_all = False ):
528
+ obj = list ()
491
529
container = self .content .viewManager .CreateContainerView (
492
530
self .content .rootFolder , vimtype , True )
531
+
493
532
for c in container .view :
494
533
if c .name == name :
534
+ if return_all is False :
535
+ return c
536
+ break
537
+ else :
538
+ obj .append (c )
539
+
540
+ if len (obj ) > 0 :
541
+ return obj
542
+ else :
543
+ # for backwards-compat
544
+ return None
545
+
546
+ '''
547
+ Get the vsphere object associated with a given MoId
548
+ '''
549
+ def get_obj_by_moid (self , vimtype , moid ):
550
+ obj = None
551
+ container = self .content .viewManager .CreateContainerView (
552
+ self .content .rootFolder , vimtype , True )
553
+ for c in container .view :
554
+ if c ._GetMoId () == moid :
495
555
obj = c
496
556
break
497
557
return obj
@@ -582,12 +642,8 @@ def WaitForTasks(self, tasks):
582
642
if change .name == 'info' :
583
643
state = change .val .state
584
644
elif change .name == 'info.state' :
585
- state = change .val
586
- else :
587
- continue
588
-
589
- if not str (task ) in taskList :
590
- continue
645
+ if not str (task ) in taskList :
646
+ continue
591
647
592
648
if state == vim .TaskInfo .State .success :
593
649
# Remove task from taskList
0 commit comments