VMware: vcenter_folder checks nested parent folder path (#62741)

added possibility to create Subfolder by defining full path below datacenter

Closes: #64442
This commit is contained in:
Jan Meerkamp 2019-11-22 15:25:42 +01:00 committed by Gonéri Le Bouder
parent d05d5cfc39
commit 315cc2f3ea
2 changed files with 43 additions and 15 deletions

View file

@ -49,6 +49,7 @@ options:
- 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
type: str
folder_type:
@ -179,23 +180,36 @@ 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
if not p_folder_obj:
self.module.fail_json(msg="Parent folder %s does not exist" % parent_folder)
else:
p_folder_obj = self.get_folder(datacenter_name=datacenter_name,
folder_name=parent_folder,
folder_type=folder_type)
# Check if folder exists under parent folder
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']['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)
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(datacenter_name=datacenter_name,
folder_name=folder_name,
folder_type=folder_type,
parent_folder=p_folder_obj)
if child_folder_obj:
results['result']['path'] = self.get_folder_path(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(datacenter_name=datacenter_name,
folder_name=folder_name,

View file

@ -113,6 +113,20 @@
- datastore
- network
- name: Create 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: yet_another_level
- debug: var=yet_another_level
- debug:
msg: "{{ all_folder_results }}"