From e6804cefe6650b559af56b72dc304ebc9c82b66b Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 17 Sep 2021 20:48:03 +0200 Subject: [PATCH] Add support for check_mode --- .../723-elasticache_subnet_group-boto3.yml | 1 + plugins/modules/elasticache_subnet_group.py | 16 +- .../elasticache_subnet_group/tasks/main.yml | 150 +++++++++++++++++- 3 files changed, 158 insertions(+), 9 deletions(-) diff --git a/changelogs/fragments/723-elasticache_subnet_group-boto3.yml b/changelogs/fragments/723-elasticache_subnet_group-boto3.yml index 91171b03e54..fc291737da9 100644 --- a/changelogs/fragments/723-elasticache_subnet_group-boto3.yml +++ b/changelogs/fragments/723-elasticache_subnet_group-boto3.yml @@ -1,3 +1,4 @@ minor_changes: - elasticache_subnet_group - module migrated to boto3 AWS SDK (https://github.com/ansible-collections/community.aws/pull/723). - elasticache_subnet_group - add return values (https://github.com/ansible-collections/community.aws/pull/723). +- elasticache_subnet_group - add support for check_mode (https://github.com/ansible-collections/community.aws/pull/723). diff --git a/plugins/modules/elasticache_subnet_group.py b/plugins/modules/elasticache_subnet_group.py index c5ef1d0f606..5e813b64ad1 100644 --- a/plugins/modules/elasticache_subnet_group.py +++ b/plugins/modules/elasticache_subnet_group.py @@ -139,6 +139,10 @@ def get_subnet_group(name): def create_subnet_group(name, description, subnets): + + if module.check_mode: + return True + try: if not description: description = name @@ -168,6 +172,9 @@ def update_subnet_group(subnet_group, name, description, subnets): if not update_params: return False + if module.check_mode: + return True + try: client.modify_cache_subnet_group( aws_retry=True, @@ -181,6 +188,10 @@ def update_subnet_group(subnet_group, name, description, subnets): def delete_subnet_group(name): + + if module.check_mode: + return True + try: client.delete_cache_subnet_group( aws_retry=True, @@ -206,7 +217,10 @@ def main(): global module global client - module = AnsibleAWSModule(argument_spec=argument_spec) + module = AnsibleAWSModule( + argument_spec=argument_spec, + supports_check_mode=True, + ) state = module.params.get('state') name = module.params.get('name').lower() diff --git a/tests/integration/targets/elasticache_subnet_group/tasks/main.yml b/tests/integration/targets/elasticache_subnet_group/tasks/main.yml index 5f938966e4b..cc82948a1ce 100644 --- a/tests/integration/targets/elasticache_subnet_group/tasks/main.yml +++ b/tests/integration/targets/elasticache_subnet_group/tasks/main.yml @@ -57,6 +57,23 @@ subnet_id_d: '{{ vpc_subnet_create.results[3].subnet.id }}' # ============================================================ + - name: Create Subnet Group - check_mode + elasticache_subnet_group: + state: present + name: '{{ group_name }}' + description: '{{ description_default }}' + subnets: + - '{{ subnet_id_a }}' + - '{{ subnet_id_b }}' + check_mode: True + register: create_group + + - name: Check result - Create Subnet Group - check_mode + assert: + that: + - create_group is successful + - create_group is changed + - name: Create Subnet Group elasticache_subnet_group: state: present @@ -88,6 +105,23 @@ - create_group.cache_subnet_group.arn.startswith('arn:') - create_group.cache_subnet_group.arn.endswith(group_name) + - name: Create Subnet Group - idempotency - check_mode + elasticache_subnet_group: + state: present + name: '{{ group_name }}' + description: '{{ description_default }}' + subnets: + - '{{ subnet_id_a }}' + - '{{ subnet_id_b }}' + check_mode: True + register: create_group + + - name: Check result - Create Subnet Group - idempotency - check_mode + assert: + that: + - create_group is successful + - create_group is not changed + - name: Create Subnet Group - idempotency elasticache_subnet_group: state: present @@ -121,14 +155,33 @@ # ============================================================ + - name: Update Subnet Group Description - check_mode + elasticache_subnet_group: + state: present + name: '{{ group_name }}' + description: '{{ description_updated }}' + ## No longer mandatory + # subnets: + # - '{{ subnet_id_a }}' + # - '{{ subnet_id_b }}' + check_mode: True + register: update_description + + - name: Check result - Update Subnet Group Description - check_mode + assert: + that: + - update_description is successful + - update_description is changed + - name: Update Subnet Group Description elasticache_subnet_group: state: present name: '{{ group_name }}' description: '{{ description_updated }}' - subnets: - - '{{ subnet_id_a }}' - - '{{ subnet_id_b }}' + ## No longer mandatory + # subnets: + # - '{{ subnet_id_a }}' + # - '{{ subnet_id_b }}' register: update_description - name: Check result - Update Subnet Group Description @@ -152,14 +205,33 @@ - update_description.cache_subnet_group.arn.startswith('arn:') - update_description.cache_subnet_group.arn.endswith(group_name) + - name: Update Subnet Group Description - idempotency - check_mode + elasticache_subnet_group: + state: present + name: '{{ group_name }}' + description: '{{ description_updated }}' + ## No longer mandatory + # subnets: + # - '{{ subnet_id_a }}' + # - '{{ subnet_id_b }}' + check_mode: True + register: update_description + + - name: Check result - Update Subnet Group Description - idempotency - check_mode + assert: + that: + - update_description is successful + - update_description is not changed + - name: Update Subnet Group Description - idempotency elasticache_subnet_group: state: present name: '{{ group_name }}' description: '{{ description_updated }}' - subnets: - - '{{ subnet_id_a }}' - - '{{ subnet_id_b }}' + ## No longer mandatory + # subnets: + # - '{{ subnet_id_a }}' + # - '{{ subnet_id_b }}' register: update_description - name: Check result - Update Subnet Group Description - idempotency @@ -185,11 +257,30 @@ # ============================================================ + - name: Update Subnet Group subnets - check_mode + elasticache_subnet_group: + state: present + name: '{{ group_name }}' + ## No longer mandatory + # description: '{{ description_updated }}' + subnets: + - '{{ subnet_id_c }}' + - '{{ subnet_id_d }}' + check_mode: True + register: update_subnets + + - name: Check result - Update Subnet Group subnets - check_mode + assert: + that: + - update_subnets is successful + - update_subnets is changed + - name: Update Subnet Group subnets elasticache_subnet_group: state: present name: '{{ group_name }}' - description: '{{ description_updated }}' + ## No longer mandatory + # description: '{{ description_updated }}' subnets: - '{{ subnet_id_c }}' - '{{ subnet_id_d }}' @@ -216,11 +307,30 @@ - update_subnets.cache_subnet_group.arn.startswith('arn:') - update_subnets.cache_subnet_group.arn.endswith(group_name) + - name: Update Subnet Group subnets - idempotency - check_mode + elasticache_subnet_group: + state: present + name: '{{ group_name }}' + ## No longer mandatory + # description: '{{ description_updated }}' + subnets: + - '{{ subnet_id_c }}' + - '{{ subnet_id_d }}' + check_mode: True + register: update_subnets + + - name: Check result - Update Subnet Group subnets - idempotency - check_mode + assert: + that: + - update_subnets is successful + - update_subnets is not changed + - name: Update Subnet Group subnets - idempotency elasticache_subnet_group: state: present name: '{{ group_name }}' - description: '{{ description_updated }}' + ## No longer mandatory + # description: '{{ description_updated }}' subnets: - '{{ subnet_id_c }}' - '{{ subnet_id_d }}' @@ -249,6 +359,18 @@ # ============================================================ + - name: Delete Subnet Group - check_mode + elasticache_subnet_group: + state: absent + name: '{{ group_name }}' + check_mode: True + register: delete_group + + - name: Check result - Delete Subnet Group - check_mode + assert: + that: + - delete_group is changed + - name: Delete Subnet Group elasticache_subnet_group: state: absent @@ -260,6 +382,18 @@ that: - delete_group is changed + - name: Delete Subnet Group - idempotency - check_mode + elasticache_subnet_group: + state: absent + name: '{{ group_name }}' + check_mode: True + register: delete_group + + - name: Check result - Delete Subnet Group - idempotency - check_mode + assert: + that: + - delete_group is not changed + - name: Delete Subnet Group - idempotency elasticache_subnet_group: state: absent