From 26d38dd6421851084c5980444f8aa079f60eef88 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 21 May 2019 15:55:43 +0530 Subject: [PATCH] VMware: Add folder param in vmware_datastore_cluster * Make datacenter as alias and optional * Add folder param to place datastore cluster in specific folder * Updated examples * Updated tests Fixes: #48010 Signed-off-by: Abhijeet Kasurde --- ...10-vmware_datastore_cluster-add_folder.yml | 2 + .../rst/porting_guides/porting_guide_2.9.rst | 4 +- .../cloud/vmware/vmware_datastore_cluster.py | 63 ++++++++++++++++--- .../vmware_datastore_cluster/tasks/main.yml | 15 +++++ 4 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/48010-vmware_datastore_cluster-add_folder.yml diff --git a/changelogs/fragments/48010-vmware_datastore_cluster-add_folder.yml b/changelogs/fragments/48010-vmware_datastore_cluster-add_folder.yml new file mode 100644 index 00000000000..4299b0d5aa5 --- /dev/null +++ b/changelogs/fragments/48010-vmware_datastore_cluster-add_folder.yml @@ -0,0 +1,2 @@ +minor_changes: + - Add folder option in vmware_datastore_cluster to place datastore cluster in specific folder (https://github.com/ansible/ansible/issues/48010). diff --git a/docs/docsite/rst/porting_guides/porting_guide_2.9.rst b/docs/docsite/rst/porting_guides/porting_guide_2.9.rst index d87a40f3645..3ae6550bfad 100644 --- a/docs/docsite/rst/porting_guides/porting_guide_2.9.rst +++ b/docs/docsite/rst/porting_guides/porting_guide_2.9.rst @@ -66,8 +66,8 @@ No notable changes Noteworthy module changes ------------------------- -* `vmware_dvswitch ` accepts `folder` parameter to place dvswitch in user defined folder. - This option makes `datacenter` as optional parameter. +* `vmware_dvswitch ` accepts `folder` parameter to place dvswitch in user defined folder. This option makes `datacenter` as an optional parameter. +* `vmware_datastore_cluster ` accepts `folder` parameter to place datastore cluster in user defined folder. This option makes `datacenter` as an optional parameter. Plugins diff --git a/lib/ansible/modules/cloud/vmware/vmware_datastore_cluster.py b/lib/ansible/modules/cloud/vmware/vmware_datastore_cluster.py index 87e1c41cf83..4b7fb293e6a 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_datastore_cluster.py +++ b/lib/ansible/modules/cloud/vmware/vmware_datastore_cluster.py @@ -34,7 +34,10 @@ options: datacenter_name: description: - The name of the datacenter. - required: True + - You must specify either a C(datacenter_name) or a C(folder). + - Mutually exclusive with C(folder) parameter. + required: False + aliases: [ datacenter ] datastore_cluster_name: description: - The name of the datastore cluster. @@ -44,6 +47,22 @@ options: - If the datastore cluster should be present or absent. choices: [ present, absent ] default: present + folder: + description: + - Destination folder, absolute path to place datastore cluster in. + - The folder should include the datacenter. + - This parameter is case sensitive. + - You must specify either a C(folder) or a C(datacenter_name). + - 'Examples:' + - ' folder: /datacenter1/datastore' + - ' folder: datacenter1/datastore' + - ' folder: /datacenter1/datastore/folder1' + - ' folder: datacenter1/datastore/folder1' + - ' folder: /folder1/datacenter1/datastore' + - ' folder: folder1/datacenter1/datastore' + - ' folder: /folder1/datacenter1/datastore/folder2' + required: False + version_added: '2.9' extends_documentation_fragment: vmware.documentation ''' @@ -58,6 +77,15 @@ EXAMPLES = ''' state: present delegate_to: localhost +- name: Create datastore cluster using folder + vmware_datastore_cluster: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + folder: '/{{ datacenter_name }}/datastore/ds_folder' + datastore_cluster_name: '{{ datastore_cluster_name }}' + state: present + delegate_to: localhost - name: Delete datastore cluster vmware_datastore_cluster: @@ -86,17 +114,25 @@ from ansible.module_utils._text import to_native class VMwareDatastoreClusterManager(PyVmomi): def __init__(self, module): super(VMwareDatastoreClusterManager, self).__init__(module) - datacenter_name = self.params.get('datacenter_name') - self.datacenter_obj = self.find_datacenter_by_name(datacenter_name) - if not self.datacenter_obj: - self.module.fail_json(msg="Failed to find datacenter '%s' required" - " for managing datastore cluster." % datacenter_name) + folder = self.params['folder'] + if folder: + self.folder_obj = self.content.searchIndex.FindByInventoryPath(folder) + if not self.folder_obj: + self.module.fail_json(msg="Failed to find the folder specified by %(folder)s" % self.params) + else: + datacenter_name = self.params.get('datacenter_name') + datacenter_obj = self.find_datacenter_by_name(datacenter_name) + if not datacenter_obj: + self.module.fail_json(msg="Failed to find datacenter '%s' required" + " for managing datastore cluster." % datacenter_name) + self.folder_obj = datacenter_obj.datastoreFolder + self.datastore_cluster_name = self.params.get('datastore_cluster_name') self.datastore_cluster_obj = self.find_datastore_cluster_by_name(self.datastore_cluster_name) def ensure(self): """ - Function to manage internal state of datastore cluster + Manage internal state of datastore cluster """ results = dict(changed=False, result='') @@ -122,7 +158,7 @@ class VMwareDatastoreClusterManager(PyVmomi): # Create datastore cluster if not self.module.check_mode: try: - self.datacenter_obj.datastoreFolder.CreateStoragePod(name=self.datastore_cluster_name) + self.folder_obj.CreateStoragePod(name=self.datastore_cluster_name) except Exception as generic_exc: self.module.fail_json(msg="Failed to create datstore cluster" " '%s' due to %s" % (self.datastore_cluster_name, @@ -138,14 +174,21 @@ def main(): argument_spec = vmware_argument_spec() argument_spec.update( dict( - datacenter_name=dict(type='str', required=True), + datacenter_name=dict(type='str', required=False, aliases=['datacenter']), datastore_cluster_name=dict(type='str', required=True), state=dict(default='present', choices=['present', 'absent'], type='str'), + folder=dict(type='str', required=False) ) ) module = AnsibleModule( argument_spec=argument_spec, - supports_check_mode=True + supports_check_mode=True, + mutually_exclusive=[ + ['datacenter_name', 'folder'], + ], + required_one_of=[ + ['datacenter_name', 'folder'], + ] ) datastore_cluster_mgr = VMwareDatastoreClusterManager(module) diff --git a/test/integration/targets/vmware_datastore_cluster/tasks/main.yml b/test/integration/targets/vmware_datastore_cluster/tasks/main.yml index 60548ee22ca..456f432c773 100644 --- a/test/integration/targets/vmware_datastore_cluster/tasks/main.yml +++ b/test/integration/targets/vmware_datastore_cluster/tasks/main.yml @@ -37,6 +37,21 @@ that: - not add_dsc.changed +- name: Add a datastore cluster using folder + vmware_datastore_cluster: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + folder: "/F0/{{ dc1 }}/datastore/F0" + datastore_cluster_name: DSC2 + state: present + register: add_dsc_folder_check + +- assert: + that: + - add_dsc_folder_check.changed + - name: Delete a datastore cluster to datacenter (check-mode) vmware_datastore_cluster: &delete_datastore_cluster hostname: '{{ vcenter_hostname }}'