Sanitize folder path from the very start (#19929)

- If an absolute path is provided, ensure it starts with /vm
- Also ensure there are no trailing slashes

This gets rid of a few locations where the same was being done.
It also fixes the cases of multiple trailing slashes, or ending up with
/vm/ instead of /vm.
pull/19654/merge
Dag Wieers 8 years ago committed by jctanner
parent ad1e066540
commit 00031e454e

@ -532,10 +532,7 @@ class PyVmomiHelper(object):
if isinstance(folder, tuple): if isinstance(folder, tuple):
folder = folder[1] folder = folder[1]
if inpath == '/': thispath = os.path.join(inpath, folder['name'])
thispath = '/vm'
else:
thispath = os.path.join(inpath, folder['name'])
if thispath not in self.foldermap['paths']: if thispath not in self.foldermap['paths']:
self.foldermap['paths'][thispath] = [] self.foldermap['paths'][thispath] = []
@ -602,14 +599,9 @@ class PyVmomiHelper(object):
if uuid: if uuid:
vm = self.content.searchIndex.FindByUuid(uuid=uuid, vmSearch=True) vm = self.content.searchIndex.FindByUuid(uuid=uuid, vmSearch=True)
elif folder: elif folder:
if self.params['folder'].endswith('/'):
self.params['folder'] = self.params['folder'][0:-1]
# Build the absolute folder path to pass into the search method # Build the absolute folder path to pass into the search method
if self.params['folder'].startswith('/vm'): if self.params['folder'].startswith('/'):
searchpath = '%(datacenter)s%(folder)s' % self.params searchpath = '%(datacenter)s%(folder)s' % self.params
elif self.params['folder'].startswith('/'):
searchpath = '%(datacenter)s/vm%(folder)s' % self.params
else: else:
# need to look for matching absolute path # need to look for matching absolute path
if not self.folders: if not self.folders:
@ -1195,8 +1187,6 @@ class PyVmomiHelper(object):
# find matching folders # find matching folders
if self.params['folder'].startswith('/'): if self.params['folder'].startswith('/'):
if not self.params['folder'].startswith('/vm'):
self.params['folder'] = '/vm' + self.params['folder']
folders = [x for x in self.foldermap['fvim_by_path'].items() if x[0] == self.params['folder']] folders = [x for x in self.foldermap['fvim_by_path'].items() if x[0] == self.params['folder']]
else: else:
folders = [x for x in self.foldermap['fvim_by_path'].items() if x[0].endswith(self.params['folder'])] folders = [x for x in self.foldermap['fvim_by_path'].items() if x[0].endswith(self.params['folder'])]
@ -1206,7 +1196,7 @@ class PyVmomiHelper(object):
self.module.fail_json(msg='No folder matched the path: %(folder)s' % self.params) self.module.fail_json(msg='No folder matched the path: %(folder)s' % self.params)
elif len(folders) > 1: elif len(folders) > 1:
self.module.fail_json( self.module.fail_json(
msg='too many folders matched "%s", please give the full path starting with /vm/' % self.params[ msg='Too many folders matched "%s", please give the full path starting with /vm/' % self.params[
'folder']) 'folder'])
# grab the folder vim object # grab the folder vim object
@ -1677,6 +1667,11 @@ def main():
result = {'failed': False, 'changed': False} result = {'failed': False, 'changed': False}
# Prepend /vm if it was missing from the folder path, also strip trailing slashes
if not module.params['folder'].startswith('/vm') and module.params['folder'].startswith('/'):
module.params['folder'] = '/vm%(folder)s' % module.params
module.params['folder'] = module.params['folder'].rstrip('/')
# Fail check, customize require template to be defined # Fail check, customize require template to be defined
if module.params["customize"] and not module.params['template']: if module.params["customize"] and not module.params['template']:
module.fail_json(msg="customize option is only valid when template option is defined") module.fail_json(msg="customize option is only valid when template option is defined")

Loading…
Cancel
Save