Skip to content

Commit 24e7a5c

Browse files
committed
Create a function that gets the absolute path to the VM.
Using a new function instead of an old block of code. Rewritten condition for getting a vm_obj.
1 parent dfb2f8a commit 24e7a5c

File tree

2 files changed

+54
-67
lines changed

2 files changed

+54
-67
lines changed

plugins/module_utils/vmware.py

+52-52
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,24 @@ def find_host_portgroup_by_name(host, portgroup_name):
373373
return None
374374

375375

376+
def get_folder_abs_path(dcpath, dcname, folder):
377+
if not dcpath.endswith('/'):
378+
dcpath += '/'
379+
380+
folder = folder.rstrip('/')
381+
382+
# Check for full path first in case it was already supplied
383+
if folder.startswith(dcpath + dcname + '/vm') or folder.startswith(dcpath + '/' + dcname + '/vm'):
384+
fullpath = folder
385+
elif folder.startswith('/vm/') or folder == '/vm':
386+
fullpath = "%s%s%s" % (dcpath, dcname, folder)
387+
elif folder.startswith('/'):
388+
fullpath = "%s%s/vm%s" % (dcpath, dcname, folder)
389+
else:
390+
fullpath = "%s%s/vm/%s" % (dcpath, dcname, folder)
391+
return fullpath
392+
393+
376394
def compile_folder_path_for_object(vobj):
377395
""" make a /vm/foo/bar/baz like folder path for an object """
378396

@@ -1217,62 +1235,44 @@ def get_vm(self):
12171235

12181236
# get_managed_objects_properties may return multiple virtual machine,
12191237
# following code tries to find user desired one depending upon the folder specified.
1220-
if len(vms) > 1:
1221-
# We have found multiple virtual machines, decide depending upon folder value
1222-
if self.params['folder'] is None:
1223-
self.module.fail_json(msg="Multiple virtual machines with same name [%s] found, "
1224-
"Folder value is a required parameter to find uniqueness "
1225-
"of the virtual machine" % self.params['name'],
1226-
details="Please see documentation of the vmware_guest module "
1227-
"for folder parameter.")
1228-
1238+
if len(vms) >= 1:
12291239
# Get folder path where virtual machine is located
12301240
# User provided folder where user thinks virtual machine is present
1231-
user_folder = self.params['folder']
1232-
# User defined datacenter
1233-
user_defined_dc = self.params['datacenter']
1234-
# User defined datacenter's object
1235-
datacenter_obj = find_datacenter_by_name(self.content, self.params['datacenter'])
1236-
# Get Path for Datacenter
1237-
dcpath = compile_folder_path_for_object(vobj=datacenter_obj)
1238-
1239-
# Nested folder does not return trailing /
1240-
if not dcpath.endswith('/'):
1241-
dcpath += '/'
1242-
1241+
user_folder = self.params.get('folder')
1242+
# We have found multiple virtual machines, decide depending upon folder value
12431243
if user_folder in [None, '', '/']:
1244-
# User provided blank value or
1245-
# User provided only root value, we fail
1246-
self.module.fail_json(msg="vmware_guest found multiple virtual machines with same "
1247-
"name [%s], please specify folder path other than blank "
1248-
"or '/'" % self.params['name'])
1249-
elif user_folder.startswith('/vm/'):
1250-
# User provided nested folder under VMware default vm folder i.e. folder = /vm/india/finance
1251-
user_desired_path = "%s%s%s" % (dcpath, user_defined_dc, user_folder)
1244+
if len(vms) == 1:
1245+
vm_obj = vms[0]
1246+
else:
1247+
# User provided blank value or
1248+
# User provided only root value, we fail
1249+
self.module.fail_json(msg="Multiple virtual machines with same name [%s] found, "
1250+
"Folder value is a required parameter to find uniqueness "
1251+
"of the virtual machine" % self.params['name'],
1252+
details="Please see documentation of the vmware_guest module "
1253+
"for folder parameter.")
12521254
else:
1253-
# User defined datacenter is not nested i.e. dcpath = '/' , or
1254-
# User defined datacenter is nested i.e. dcpath = '/F0/DC0' or
1255-
# User provided folder starts with / and datacenter i.e. folder = /ha-datacenter/ or
1256-
# User defined folder starts with datacenter without '/' i.e.
1257-
# folder = DC0/vm/india/finance or
1258-
# folder = DC0/vm
1259-
user_desired_path = user_folder
1260-
1261-
for vm in vms:
1262-
# Check if user has provided same path as virtual machine
1263-
actual_vm_folder_path = self.get_vm_path(content=self.content, vm_name=vm)
1264-
if not actual_vm_folder_path.startswith("%s%s" % (dcpath, user_defined_dc)):
1265-
continue
1266-
if user_desired_path in actual_vm_folder_path:
1267-
vm_obj = vm
1268-
break
1269-
elif vms:
1270-
# Unique virtual machine found.
1271-
actual_vm_folder_path = self.get_vm_path(content=self.content, vm_name=vms[0])
1272-
if self.params.get('folder') is None:
1273-
vm_obj = vms[0]
1274-
elif self.params['folder'] in actual_vm_folder_path:
1275-
vm_obj = vms[0]
1255+
# User defined datacenter
1256+
user_defined_dc = self.params['datacenter']
1257+
# User defined datacenter's object
1258+
datacenter_obj = find_datacenter_by_name(self.content, self.params['datacenter'])
1259+
# Get Path for Datacenter
1260+
dcpath = compile_folder_path_for_object(vobj=datacenter_obj)
1261+
1262+
# Nested folder does not return trailing /
1263+
if not dcpath.endswith('/'):
1264+
dcpath += '/'
1265+
1266+
user_desired_path = get_folder_abs_path(dcpath, user_defined_dc, user_folder)
1267+
1268+
for vm in vms:
1269+
# Check if user has provided same path as virtual machine
1270+
actual_vm_folder_path = self.get_vm_path(content=self.content, vm_name=vm)
1271+
if not actual_vm_folder_path.startswith("%s%s" % (dcpath, user_defined_dc)):
1272+
continue
1273+
if user_desired_path == actual_vm_folder_path:
1274+
vm_obj = vm
1275+
break
12761276
elif 'moid' in self.params and self.params['moid']:
12771277
vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.params['moid'], self.si._stub)
12781278
try:

plugins/modules/vmware_guest.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@
10551055
find_dvspg_by_name,
10561056
wait_for_vm_ip,
10571057
quote_obj_name,
1058+
get_folder_abs_path,
10581059
)
10591060
from ansible_collections.community.vmware.plugins.module_utils.vm_device_helper import PyVmomiDeviceHelper
10601061
from ansible_collections.community.vmware.plugins.module_utils.vmware_spbm import SPBM
@@ -2797,21 +2798,7 @@ def deploy_vm(self):
27972798

27982799
dcpath = compile_folder_path_for_object(datacenter)
27992800

2800-
# Nested folder does not have trailing /
2801-
if not dcpath.endswith('/'):
2802-
dcpath += '/'
2803-
2804-
# Check for full path first in case it was already supplied
2805-
if self.folder.startswith(
2806-
dcpath + self.params["datacenter"] + "/vm"
2807-
) or self.folder.startswith(dcpath + "/" + self.params["datacenter"] + "/vm"):
2808-
fullpath = self.folder
2809-
elif self.folder.startswith("/vm/") or self.folder == "/vm":
2810-
fullpath = "%s%s%s" % (dcpath, self.params["datacenter"], self.folder)
2811-
elif self.folder.startswith("/"):
2812-
fullpath = "%s%s/vm%s" % (dcpath, self.params["datacenter"], self.folder)
2813-
else:
2814-
fullpath = "%s%s/vm/%s" % (dcpath, self.params["datacenter"], self.folder)
2801+
fullpath = get_folder_abs_path(dcpath, self.params['datacenter'], self.folder)
28152802

28162803
f_obj = self.content.searchIndex.FindByInventoryPath(fullpath)
28172804

0 commit comments

Comments
 (0)