Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions lib/ansible/module_utils/vmware.py
Expand Up @@ -1382,3 +1382,12 @@ def to_json(self, obj, properties=None):
else:
result = self._jsonify(obj)
return result

def get_folder_path(self, cur):
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
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
53 changes: 29 additions & 24 deletions lib/ansible/modules/cloud/vmware/vcenter_folder.py
Expand Up @@ -122,11 +122,12 @@

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:
Expand Down Expand Up @@ -163,7 +164,7 @@ def ensure(self):
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
Expand All @@ -181,44 +182,48 @@ def ensure(self):
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,
folder_name=folder_name,
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
# and the playbook should simply carry on with other tasks.
# 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))
Expand Down Expand Up @@ -251,24 +256,24 @@ def ensure(self):
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))
Expand Down