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:
from pyVmomi import vim
except ImportError as e:
except ImportError as import_err:
pass
from ansible.module_utils.basic import AnsibleModule
@ -145,11 +145,16 @@ class VmwareFolderManager(PyVmomi):
if self.datacenter_obj is None:
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):
"""
Function to manage internal state management
Returns:
Manage internal state management
"""
state = self.module.params.get('state')
folder_type = self.module.params.get('folder_type')
@ -157,26 +162,40 @@ class VmwareFolderManager(PyVmomi):
parent_folder = self.module.params.get('parent_folder', None)
results = dict(changed=False, result=dict())
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
try:
if parent_folder:
folder = self.get_folder_by_name(folder_name=parent_folder)
if folder and not self.get_folder_by_name(folder_name=folder_name, parent_folder=folder):
folder.CreateFolder(folder_name)
if parent_folder and p_folder_obj:
p_folder_obj.CreateFolder(folder_name)
results['changed'] = True
results['result'] = "Folder '%s' of type '%s' created under %s" \
" successfully." % (folder_name, folder_type, parent_folder)
elif folder is None:
self.module.fail_json(msg="Failed to find the parent folder %s"
" 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)
elif not parent_folder and not p_folder_obj:
self.datacenter_folder_type[folder_type].CreateFolder(folder_name)
results['changed'] = True
results['result'] = "Folder '%s' of type '%s' created successfully" % (folder_name, folder_type)
except vim.fault.DuplicateName as duplicate_name:
@ -195,7 +214,7 @@ class VmwareFolderManager(PyVmomi):
" exception : %s " % to_native(general_exc))
self.module.exit_json(**results)
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:
try:
task = folder_obj.UnregisterAndDestroy()
@ -205,21 +224,22 @@ class VmwareFolderManager(PyVmomi):
" modified folder before this operation : %s" % to_native(concurrent_access.msg))
except vim.fault.InvalidState as invalid_state:
self.module.fail_json(msg="Failed to remove folder as folder is in"
" invalid state" % to_native(invalid_state.msg))
except Exception as e:
" invalid state : %s" % to_native(invalid_state.msg))
except Exception as gen_exec:
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)
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
"""
folder_objs = get_all_objs(self.content, [vim.Folder], parent_folder)
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 None