VMware: vcenter_folder: print full path of the new folder (#55237)

Print the full path of the freshly created folder in the output string.
pull/56846/head
Gonéri Le Bouder 6 years ago committed by Abhijeet Kasurde
parent 0d842ff539
commit 5005c353ef

@ -0,0 +1,3 @@
minor_changes:
- vcenter_folder - returns a dict instead of a string, the previous output is now in the 'msg' key
- vcenter_folder - returns now the full path of the file in the new 'path' key

@ -1398,3 +1398,12 @@ class PyVmomi(object):
else: else:
result = self._jsonify(obj) result = self._jsonify(obj)
return result return result
def get_folder_path(self, cur):
full_path = '/' + cur.name
while hasattr(cur, 'parent') and cur.parent:
if cur.parent == self.content.rootFolder:
break
cur = cur.parent
full_path = '/' + cur.name + full_path
return full_path

@ -122,11 +122,12 @@ EXAMPLES = r'''
RETURN = r''' RETURN = r'''
result: result:
description: description: The detail about the new folder
- string stating about result returned: On success
returned: success type: complex
type: str contains:
sample: "Folder 'sub_network_folder' of type 'vm' created under vm_folder successfully." path: the full path of the new folder
msg: string stating about result
''' '''
try: try:
@ -163,7 +164,7 @@ class VmwareFolderManager(PyVmomi):
folder_type = self.module.params.get('folder_type') folder_type = self.module.params.get('folder_type')
folder_name = self.module.params.get('folder_name') folder_name = self.module.params.get('folder_name')
parent_folder = self.module.params.get('parent_folder', None) parent_folder = self.module.params.get('parent_folder', None)
results = dict(changed=False, result=dict()) results = {'changed': False, 'result': {}}
if state == 'present': if state == 'present':
# Check if the folder already exists # Check if the folder already exists
p_folder_obj = None p_folder_obj = None
@ -181,7 +182,8 @@ class VmwareFolderManager(PyVmomi):
folder_type=folder_type, folder_type=folder_type,
parent_folder=p_folder_obj) parent_folder=p_folder_obj)
if child_folder_obj: if child_folder_obj:
results['result'] = "Folder %s already exists under" \ results['result']['path'] = self.get_folder_path(child_folder_obj)
results['result']['msg'] = "Folder %s already exists under" \
" parent folder %s" % (folder_name, parent_folder) " parent folder %s" % (folder_name, parent_folder)
self.module.exit_json(**results) self.module.exit_json(**results)
else: else:
@ -190,26 +192,29 @@ class VmwareFolderManager(PyVmomi):
folder_type=folder_type) folder_type=folder_type)
if folder_obj: if folder_obj:
results['result'] = "Folder %s already exists" % folder_name results['result']['path'] = self.get_folder_path(folder_obj)
results['result']['msg'] = "Folder %s already exists" % folder_name
self.module.exit_json(**results) self.module.exit_json(**results)
# Create a new folder # Create a new folder
try: try:
if parent_folder and p_folder_obj: if parent_folder and p_folder_obj:
if self.module.check_mode: if self.module.check_mode:
results['result'] = "Folder '%s' of type '%s' under '%s' will be created." % \ results['msg'] = "Folder '%s' of type '%s' under '%s' will be created." % \
(folder_name, folder_type, parent_folder) (folder_name, folder_type, parent_folder)
else: else:
p_folder_obj.CreateFolder(folder_name) new_folder = p_folder_obj.CreateFolder(folder_name)
results['result'] = "Folder '%s' of type '%s' under '%s' created" \ results['result']['path'] = self.get_folder_path(new_folder)
results['result']['msg'] = "Folder '%s' of type '%s' under '%s' created" \
" successfully." % (folder_name, folder_type, parent_folder) " successfully." % (folder_name, folder_type, parent_folder)
results['changed'] = True results['changed'] = True
elif not parent_folder and not p_folder_obj: elif not parent_folder and not p_folder_obj:
if self.module.check_mode: if self.module.check_mode:
results['result'] = "Folder '%s' of type '%s' will be created." % (folder_name, folder_type) results['msg'] = "Folder '%s' of type '%s' will be created." % (folder_name, folder_type)
else: else:
self.datacenter_folder_type[folder_type].CreateFolder(folder_name) new_folder = self.datacenter_folder_type[folder_type].CreateFolder(folder_name)
results['result'] = "Folder '%s' of type '%s' created successfully." % (folder_name, folder_type) results['result']['msg'] = "Folder '%s' of type '%s' created successfully." % (folder_name, folder_type)
results['result']['path'] = self.get_folder_path(new_folder)
results['changed'] = True results['changed'] = True
except vim.fault.DuplicateName as duplicate_name: except vim.fault.DuplicateName as duplicate_name:
# To be consistent with the other vmware modules, We decided to accept this error # To be consistent with the other vmware modules, We decided to accept this error
@ -217,7 +222,7 @@ class VmwareFolderManager(PyVmomi):
# User will have to take care of this exception # User will have to take care of this exception
# https://github.com/ansible/ansible/issues/35388#issuecomment-362283078 # https://github.com/ansible/ansible/issues/35388#issuecomment-362283078
results['changed'] = False results['changed'] = False
results['result'] = "Failed to create folder as another object has same name" \ results['msg'] = "Failed to create folder as another object has same name" \
" in the same target folder : %s" % to_native(duplicate_name.msg) " in the same target folder : %s" % to_native(duplicate_name.msg)
except vim.fault.InvalidName as invalid_name: except vim.fault.InvalidName as invalid_name:
self.module.fail_json(msg="Failed to create folder as folder name is not a valid " self.module.fail_json(msg="Failed to create folder as folder name is not a valid "
@ -251,24 +256,24 @@ class VmwareFolderManager(PyVmomi):
if parent_folder: if parent_folder:
if self.module.check_mode: if self.module.check_mode:
results['changed'] = True results['changed'] = True
results['result'] = "Folder '%s' of type '%s' under '%s' will be removed." % \ results['msg'] = "Folder '%s' of type '%s' under '%s' will be removed." % \
(folder_name, folder_type, parent_folder) (folder_name, folder_type, parent_folder)
else: else:
if folder_type == 'vm': if folder_type == 'vm':
task = folder_obj.UnregisterAndDestroy() task = folder_obj.UnregisterAndDestroy()
else: else:
task = folder_obj.Destroy() task = folder_obj.Destroy()
results['changed'], results['result'] = wait_for_task(task=task) results['changed'], results['msg'] = wait_for_task(task=task)
else: else:
if self.module.check_mode: if self.module.check_mode:
results['changed'] = True results['changed'] = True
results['result'] = "Folder '%s' of type '%s' will be removed." % (folder_name, folder_type) results['msg'] = "Folder '%s' of type '%s' will be removed." % (folder_name, folder_type)
else: else:
if folder_type == 'vm': if folder_type == 'vm':
task = folder_obj.UnregisterAndDestroy() task = folder_obj.UnregisterAndDestroy()
else: else:
task = folder_obj.Destroy() task = folder_obj.Destroy()
results['changed'], results['result'] = wait_for_task(task=task) results['changed'], results['msg'] = wait_for_task(task=task)
except vim.fault.ConcurrentAccess as concurrent_access: except vim.fault.ConcurrentAccess as concurrent_access:
self.module.fail_json(msg="Failed to remove folder as another client" self.module.fail_json(msg="Failed to remove folder as another client"
" modified folder before this operation : %s" % to_native(concurrent_access.msg)) " modified folder before this operation : %s" % to_native(concurrent_access.msg))

Loading…
Cancel
Save