1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

vcenter_folder - fix error events being generated, incorrect parent folders (#45951)

* fix error events being generated when vmware folders exist
* fix incorrect parent folders being identified (wrong type)
This commit is contained in:
mrmagooey 2018-10-02 19:17:36 +10:00 committed by Abhijeet Kasurde
parent 6df1f6b203
commit b2dc7075df

View file

@ -129,7 +129,7 @@ result:
try: try:
from pyVmomi import vim from pyVmomi import vim
except ImportError as e: except ImportError as import_err:
pass pass
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -145,11 +145,16 @@ class VmwareFolderManager(PyVmomi):
if self.datacenter_obj is None: if self.datacenter_obj is None:
self.module.fail_json(msg="Failed to find datacenter %s" % datacenter_name) self.module.fail_json(msg="Failed to find datacenter %s" % datacenter_name)
self.datacenter_folder_type = {
'vm': self.datacenter_obj.vmFolder,
'host': self.datacenter_obj.hostFolder,
'datastore': self.datacenter_obj.datastoreFolder,
'network': self.datacenter_obj.networkFolder,
}
def ensure(self): def ensure(self):
""" """
Function to manage internal state management Manage internal state management
Returns:
""" """
state = self.module.params.get('state') state = self.module.params.get('state')
folder_type = self.module.params.get('folder_type') folder_type = self.module.params.get('folder_type')
@ -157,26 +162,40 @@ class VmwareFolderManager(PyVmomi):
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 = dict(changed=False, result=dict())
if state == 'present': if state == 'present':
# Check if the folder already exists
p_folder_obj = None
if parent_folder:
p_folder_obj = self.get_folder(folder_name=parent_folder,
folder_type=folder_type)
if not p_folder_obj:
self.module.fail_json(msg="Parent folder %s does not exist" % parent_folder)
# Check if folder exists under parent folder
child_folder_obj = self.get_folder(folder_name=folder_name,
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)
self.module.exit_json(**results)
else:
folder_obj = self.get_folder(folder_name=folder_name,
folder_type=folder_type)
if folder_obj:
results['result'] = "Folder %s already exists" % folder_name
self.module.exit_json(**results)
# Create a new folder # Create a new folder
try: try:
if parent_folder: if parent_folder and p_folder_obj:
folder = self.get_folder_by_name(folder_name=parent_folder) p_folder_obj.CreateFolder(folder_name)
if folder and not self.get_folder_by_name(folder_name=folder_name, parent_folder=folder):
folder.CreateFolder(folder_name)
results['changed'] = True results['changed'] = True
results['result'] = "Folder '%s' of type '%s' created under %s" \ results['result'] = "Folder '%s' of type '%s' created under %s" \
" successfully." % (folder_name, folder_type, parent_folder) " successfully." % (folder_name, folder_type, parent_folder)
elif folder is None: elif not parent_folder and not p_folder_obj:
self.module.fail_json(msg="Failed to find the parent folder %s" self.datacenter_folder_type[folder_type].CreateFolder(folder_name)
" for folder %s" % (parent_folder, folder_name))
else:
datacenter_folder_type = {
'vm': self.datacenter_obj.vmFolder,
'host': self.datacenter_obj.hostFolder,
'datastore': self.datacenter_obj.datastoreFolder,
'network': self.datacenter_obj.networkFolder,
}
datacenter_folder_type[folder_type].CreateFolder(folder_name)
results['changed'] = True results['changed'] = True
results['result'] = "Folder '%s' of type '%s' created successfully" % (folder_name, folder_type) results['result'] = "Folder '%s' of type '%s' created successfully" % (folder_name, folder_type)
except vim.fault.DuplicateName as duplicate_name: except vim.fault.DuplicateName as duplicate_name:
@ -195,7 +214,7 @@ class VmwareFolderManager(PyVmomi):
" exception : %s " % to_native(general_exc)) " exception : %s " % to_native(general_exc))
self.module.exit_json(**results) self.module.exit_json(**results)
elif state == 'absent': elif state == 'absent':
folder_obj = self.get_folder_by_name(folder_name=folder_name) folder_obj = self.get_folder(folder_name=folder_name, folder_type=folder_type)
if folder_obj: if folder_obj:
try: try:
task = folder_obj.UnregisterAndDestroy() task = folder_obj.UnregisterAndDestroy()
@ -205,21 +224,22 @@ class VmwareFolderManager(PyVmomi):
" modified folder before this operation : %s" % to_native(concurrent_access.msg)) " modified folder before this operation : %s" % to_native(concurrent_access.msg))
except vim.fault.InvalidState as invalid_state: except vim.fault.InvalidState as invalid_state:
self.module.fail_json(msg="Failed to remove folder as folder is in" self.module.fail_json(msg="Failed to remove folder as folder is in"
" invalid state" % to_native(invalid_state.msg)) " invalid state : %s" % to_native(invalid_state.msg))
except Exception as e: except Exception as gen_exec:
self.module.fail_json(msg="Failed to remove folder due to generic" self.module.fail_json(msg="Failed to remove folder due to generic"
" exception %s " % to_native(e)) " exception %s " % to_native(gen_exec))
self.module.exit_json(**results) self.module.exit_json(**results)
def get_folder_by_name(self, folder_name, parent_folder=None): def get_folder(self, folder_name, folder_type, parent_folder=None):
""" """
Function to get managed object of folder by name Get managed object of folder by name
Returns: Managed object of folder by name Returns: Managed object of folder by name
""" """
folder_objs = get_all_objs(self.content, [vim.Folder], parent_folder) folder_objs = get_all_objs(self.content, [vim.Folder], parent_folder)
for folder in folder_objs: for folder in folder_objs:
if folder.name == folder_name: if folder.name == folder_name and \
self.datacenter_folder_type[folder_type].childType == folder.childType:
return folder return folder
return None return None