From 5005c353ef1c64a5a224a074eeed02bd7f8eddff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Thu, 23 May 2019 06:51:22 -0400 Subject: [PATCH] VMware: vcenter_folder: print full path of the new folder (#55237) Print the full path of the freshly created folder in the output string. --- changelogs/fragments/vcenter_folder.yaml | 3 ++ lib/ansible/module_utils/vmware.py | 9 ++++ .../modules/cloud/vmware/vcenter_folder.py | 53 ++++++++++--------- 3 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 changelogs/fragments/vcenter_folder.yaml diff --git a/changelogs/fragments/vcenter_folder.yaml b/changelogs/fragments/vcenter_folder.yaml new file mode 100644 index 0000000000..037190bf4b --- /dev/null +++ b/changelogs/fragments/vcenter_folder.yaml @@ -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 diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index 1dc5a40241..acee268bcd 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -1398,3 +1398,12 @@ class PyVmomi(object): else: result = self._jsonify(obj) 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 diff --git a/lib/ansible/modules/cloud/vmware/vcenter_folder.py b/lib/ansible/modules/cloud/vmware/vcenter_folder.py index 0e10aa1bca..ea738ec0e7 100644 --- a/lib/ansible/modules/cloud/vmware/vcenter_folder.py +++ b/lib/ansible/modules/cloud/vmware/vcenter_folder.py @@ -122,11 +122,12 @@ EXAMPLES = r''' RETURN = r''' result: - description: - - string stating about result - returned: success - type: str - sample: "Folder 'sub_network_folder' of type 'vm' created under vm_folder successfully." + description: The detail about the new folder + returned: On success + type: complex + contains: + path: the full path of the new folder + msg: string stating about result ''' try: @@ -163,7 +164,7 @@ class VmwareFolderManager(PyVmomi): folder_type = self.module.params.get('folder_type') folder_name = self.module.params.get('folder_name') parent_folder = self.module.params.get('parent_folder', None) - results = dict(changed=False, result=dict()) + results = {'changed': False, 'result': {}} if state == 'present': # Check if the folder already exists p_folder_obj = None @@ -181,8 +182,9 @@ class VmwareFolderManager(PyVmomi): folder_type=folder_type, parent_folder=p_folder_obj) if child_folder_obj: - results['result'] = "Folder %s already exists under" \ - " parent folder %s" % (folder_name, parent_folder) + 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) self.module.exit_json(**results) else: folder_obj = self.get_folder(datacenter_name=datacenter_name, @@ -190,26 +192,29 @@ class VmwareFolderManager(PyVmomi): folder_type=folder_type) 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) # Create a new folder try: if parent_folder and p_folder_obj: if self.module.check_mode: - results['result'] = "Folder '%s' of type '%s' under '%s' will be created." % \ - (folder_name, folder_type, parent_folder) + results['msg'] = "Folder '%s' of type '%s' under '%s' will be created." % \ + (folder_name, folder_type, parent_folder) else: - p_folder_obj.CreateFolder(folder_name) - results['result'] = "Folder '%s' of type '%s' under '%s' created" \ - " successfully." % (folder_name, folder_type, parent_folder) + new_folder = p_folder_obj.CreateFolder(folder_name) + 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) results['changed'] = True elif not parent_folder and not p_folder_obj: 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: - self.datacenter_folder_type[folder_type].CreateFolder(folder_name) - results['result'] = "Folder '%s' of type '%s' created successfully." % (folder_name, folder_type) + new_folder = self.datacenter_folder_type[folder_type].CreateFolder(folder_name) + 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 except vim.fault.DuplicateName as duplicate_name: # To be consistent with the other vmware modules, We decided to accept this error @@ -217,8 +222,8 @@ class VmwareFolderManager(PyVmomi): # User will have to take care of this exception # https://github.com/ansible/ansible/issues/35388#issuecomment-362283078 results['changed'] = False - results['result'] = "Failed to create folder as another object has same name" \ - " in the same target folder : %s" % to_native(duplicate_name.msg) + results['msg'] = "Failed to create folder as another object has same name" \ + " in the same target folder : %s" % to_native(duplicate_name.msg) except vim.fault.InvalidName as invalid_name: self.module.fail_json(msg="Failed to create folder as folder name is not a valid " "entity name : %s" % to_native(invalid_name.msg)) @@ -251,24 +256,24 @@ class VmwareFolderManager(PyVmomi): if parent_folder: if self.module.check_mode: results['changed'] = True - results['result'] = "Folder '%s' of type '%s' under '%s' will be removed." % \ - (folder_name, folder_type, parent_folder) + results['msg'] = "Folder '%s' of type '%s' under '%s' will be removed." % \ + (folder_name, folder_type, parent_folder) else: if folder_type == 'vm': task = folder_obj.UnregisterAndDestroy() else: task = folder_obj.Destroy() - results['changed'], results['result'] = wait_for_task(task=task) + results['changed'], results['msg'] = wait_for_task(task=task) else: if self.module.check_mode: 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: if folder_type == 'vm': task = folder_obj.UnregisterAndDestroy() else: 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: self.module.fail_json(msg="Failed to remove folder as another client" " modified folder before this operation : %s" % to_native(concurrent_access.msg))