VMware: check if folder already exists before creation (#41588)

* check if folder already exists before creation
* added integration test
This commit is contained in:
Orr 2018-07-30 08:41:42 +03:00 committed by Abhijeet Kasurde
parent 249a6aae22
commit 1e4831e744
2 changed files with 22 additions and 4 deletions

View file

@ -158,12 +158,12 @@ class VmwareFolderManager(PyVmomi):
try: try:
if parent_folder: if parent_folder:
folder = self.get_folder_by_name(folder_name=parent_folder) folder = self.get_folder_by_name(folder_name=parent_folder)
if folder: if folder and not self.get_folder_by_name(folder_name=folder_name, parent_folder=folder):
folder.CreateFolder(folder_name) 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)
else: elif folder is None:
self.module.fail_json(msg="Failed to find the parent folder %s" self.module.fail_json(msg="Failed to find the parent folder %s"
" for folder %s" % (parent_folder, folder_name)) " for folder %s" % (parent_folder, folder_name))
else: else:
@ -208,13 +208,13 @@ class VmwareFolderManager(PyVmomi):
" exception %s " % to_native(e)) " exception %s " % to_native(e))
self.module.exit_json(**results) self.module.exit_json(**results)
def get_folder_by_name(self, folder_name): def get_folder_by_name(self, folder_name, parent_folder=None):
""" """
Function to get managed object of folder by name Function to 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]) 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:
return folder return folder

View file

@ -82,12 +82,30 @@
- datastore - datastore
- network - network
- name: Recreate all types of sub folder
vcenter_folder:
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance.json.username }}"
password: "{{ vcsim_instance.json.password }}"
validate_certs: no
datacenter: "{{ dc1 }}"
folder_name: "sub_{{ item }}_folder"
parent_folder: 'vm_folder'
state: present
register: recreate_folders
with_items:
- vm
- host
- datastore
- network
- debug: msg="{{ all_folder_results }}" - debug: msg="{{ all_folder_results }}"
- name: ensure everything for {{ dc1 }} - name: ensure everything for {{ dc1 }}
assert: assert:
that: that:
- all_folder_results.changed - all_folder_results.changed
- not recreate_folders.changed
#- name: Delete all types of folder #- name: Delete all types of folder
# vcenter_folder: # vcenter_folder: