VMware: vcenter_folder nested folder absent fix (#65283)

- no longer try to create folder which exists.
- correctly remove nested folders with absent.
This commit is contained in:
Jan Meerkamp 2020-02-14 16:18:34 -05:00 committed by GitHub
parent 7000c51c06
commit 1d370bd189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 12 deletions

View file

@ -24,6 +24,7 @@ version_added: '2.5'
author:
- Abhijeet Kasurde (@Akasurde)
- Christian Kotte (@ckotte) <christian.kotte@gmx.de>
- Jan Meerkamp (@meerkampdvv)
notes:
- Tested on vSphere 6.5
requirements:
@ -47,7 +48,6 @@ options:
description:
- Name of the parent folder under which new folder needs to be created.
- This is case sensitive parameter.
- Please specify unique folder name as there is no way to detect duplicate names.
- "If user wants to create a folder under '/DC0/vm/vm_folder', this value will be 'vm_folder'."
- "If user wants to create a folder under '/DC0/vm/folder1/folder2', this value will be 'folder1/folder2'."
required: False
@ -191,7 +191,14 @@ class VmwareFolderManager(PyVmomi):
if not part_folder_obj:
self.module.fail_json(msg="Could not find folder %s" % part)
p_folder_obj = part_folder_obj
child_folder_obj = self.get_folder(datacenter_name=datacenter_name,
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:
p_folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=parent_folder,
@ -259,18 +266,34 @@ class VmwareFolderManager(PyVmomi):
# Check if the folder already exists
p_folder_obj = None
if parent_folder:
p_folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=parent_folder,
folder_type=folder_type)
if "/" in parent_folder:
parent_folder_parts = parent_folder.strip('/').split('/')
p_folder_obj = None
for part in parent_folder_parts:
part_folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=part,
folder_type=folder_type,
parent_folder=p_folder_obj)
if not part_folder_obj:
self.module.fail_json(msg="Could not find folder %s" % part)
p_folder_obj = part_folder_obj
folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=folder_name,
folder_type=folder_type,
parent_folder=p_folder_obj)
else:
p_folder_obj = self.get_folder(datacenter_name=datacenter_name,
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)
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
folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=folder_name,
folder_type=folder_type,
parent_folder=p_folder_obj)
# Check if folder exists under parent folder
folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=folder_name,
folder_type=folder_type,
parent_folder=p_folder_obj)
else:
folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=folder_name,

View file

@ -126,6 +126,18 @@
register: yet_another_level
- debug: var=yet_another_level
- name: Recreate a 3rd level of directory
vcenter_folder:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
datacenter: "{{ dc1 }}"
folder_name: yet_another_level
parent_folder: vm_folder/sub_vm_folder
state: present
register: recreate_yet_another_level
- debug: var=recreate_yet_another_level
- debug:
msg: "{{ all_folder_results }}"
@ -135,12 +147,26 @@
that:
- all_folder_results.changed
- not recreate_folders.changed
- not recreate_yet_another_level.changed
## Testcase: Delete all types of folder
#
# Doesn't work with vcsim. Looks like UnregisterAndDestroy isn't supported.
- when: vcsim is not defined
block:
- name: Remove a 3rd level of directory
vcenter_folder:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
datacenter: "{{ dc1 }}"
folder_name: yet_another_level
parent_folder: vm_folder/sub_vm_folder
state: absent
register: remove_yet_another_level
- debug: var=remove_yet_another_level
- name: Delete all types of folder
vcenter_folder:
hostname: "{{ vcenter_hostname }}"