From ab845fac9f841d825c8fec4a614d91701d7c6ead Mon Sep 17 00:00:00 2001 From: Igshaan Mesias Date: Mon, 5 Sep 2022 10:52:10 +0200 Subject: [PATCH] Fix KeyError when Cluster Parameter Group is specified in rds_cluster.py (#1417) Fix KeyError when Cluster Parameter Group is specified in rds_cluster.py SUMMARY Fix KeyError when comparing state. Fixes: #1409 ISSUE TYPE Bugfix Pull Request COMPONENT NAME rds_cluster.py Reviewed-by: Alina Buzachis Reviewed-by: Markus Bergholz (cherry picked from commit b3bc68931de04fb2d8558b01c45988710d24cec3) --- .../1417-cluster-param-group-keyerror.yml | 2 + plugins/modules/rds_cluster.py | 5 ++ .../roles/rds_cluster/defaults/main.yml | 1 + .../roles/rds_cluster/tasks/test_modify.yml | 68 ++++++++++++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/1417-cluster-param-group-keyerror.yml diff --git a/changelogs/fragments/1417-cluster-param-group-keyerror.yml b/changelogs/fragments/1417-cluster-param-group-keyerror.yml new file mode 100644 index 00000000000..823c2c85b96 --- /dev/null +++ b/changelogs/fragments/1417-cluster-param-group-keyerror.yml @@ -0,0 +1,2 @@ +bugfixes: +- rds_cluster - fixes bug where specifiying an rds cluster parameter group raises a `KeyError` (https://github.com/ansible-collections/community.aws/pull/1417). diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 68e0ef17fc9..e774e7a66cd 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -833,6 +833,11 @@ def changing_cluster_options(modify_params, current_cluster): if desired_vpc_sgs: changing_params['VpcSecurityGroupIds'] = desired_vpc_sgs + desired_db_cluster_parameter_group = modify_params.pop("DBClusterParameterGroupName", None) + if desired_db_cluster_parameter_group: + if desired_db_cluster_parameter_group != current_cluster["DBClusterParameterGroup"]: + changing_params["DBClusterParameterGroupName"] = desired_db_cluster_parameter_group + for param in modify_params: if modify_params[param] != current_cluster[param]: changing_params[param] = modify_params[param] diff --git a/tests/integration/targets/rds_cluster/roles/rds_cluster/defaults/main.yml b/tests/integration/targets/rds_cluster/roles/rds_cluster/defaults/main.yml index f1fe2137b08..06ac9f319d7 100644 --- a/tests/integration/targets/rds_cluster/roles/rds_cluster/defaults/main.yml +++ b/tests/integration/targets/rds_cluster/roles/rds_cluster/defaults/main.yml @@ -15,6 +15,7 @@ tags_create: new_cluster_id: 'ansible-test-cluster-{{ tiny_prefix }}-new' new_port: 1155 new_password: 'test-rds_password-new' +new_db_parameter_group_name: 'ansible-test-db-parameter-group-{{ tiny_prefix }}-new' # Tag cluster tags_patch: diff --git a/tests/integration/targets/rds_cluster/roles/rds_cluster/tasks/test_modify.yml b/tests/integration/targets/rds_cluster/roles/rds_cluster/tasks/test_modify.yml index b093029b54c..57d9e799289 100644 --- a/tests/integration/targets/rds_cluster/roles/rds_cluster/tasks/test_modify.yml +++ b/tests/integration/targets/rds_cluster/roles/rds_cluster/tasks/test_modify.yml @@ -14,12 +14,12 @@ that: - not _result_delete_db_cluster.changed ignore_errors: yes - + - name: Create a DB cluster rds_cluster: id: "{{ cluster_id }}" state: present - engine: "{{ engine}}" + engine: "{{ engine }}" username: "{{ username }}" password: "{{ password }}" register: _result_create_source_db_cluster @@ -154,6 +154,61 @@ - "'tags' in _result_modify_id" - "'vpc_security_groups' in _result_modify_id" + - name: Check if DB cluster parameter group exists + command: 'aws rds describe-db-cluster-parameter-groups --db-cluster-parameter-group-name {{ new_db_parameter_group_name }}' + environment: + AWS_ACCESS_KEY_ID: "{{ aws_access_key }}" + AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}" + AWS_SESSION_TOKEN: "{{ security_token | default('') }}" + AWS_DEFAULT_REGION: "{{ aws_region }}" + register: _result_check_db_parameter_group + ignore_errors: True + changed_when: _result_check_db_parameter_group.rc == 0 + + - name: Create DB cluster parameter group if not exists + command: 'aws rds create-db-cluster-parameter-group --db-cluster-parameter-group-name {{ new_db_parameter_group_name }} --db-parameter-group-family aurora5.6 --description "Test DB cluster parameter group"' + environment: + AWS_ACCESS_KEY_ID: "{{ aws_access_key }}" + AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}" + AWS_SESSION_TOKEN: "{{ security_token | default('') }}" + AWS_DEFAULT_REGION: "{{ aws_region }}" + register: _result_create_db_parameter_group + when: _result_check_db_parameter_group.rc != 0 + + - name: Modify DB cluster parameter group + rds_cluster: + id: "{{ new_cluster_id }}" + state: present + db_cluster_parameter_group_name: "{{ new_db_parameter_group_name }}" + apply_immediately: True + register: _result_modify_db_parameter_group_name + + - assert: + that: + - _result_modify_db_parameter_group_name.changed + - "'allocated_storage' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.allocated_storage == 1 + - "'cluster_create_time' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.copy_tags_to_snapshot == false + - "'db_cluster_arn' in _result_modify_db_parameter_group_name" + - "_result_modify_db_parameter_group_name.db_cluster_identifier == '{{ new_cluster_id }}'" + - "'db_cluster_parameter_group' in _result_modify_db_parameter_group_name" + - "'db_cluster_resource_id' in _result_modify_db_parameter_group_name" + - "'endpoint' in _result_modify_db_parameter_group_name" + - "'engine' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.engine == "{{ engine }}" + - "'engine_mode' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.engine_mode == "provisioned" + - "'engine_version' in _result_modify_db_parameter_group_name" + - "'master_username' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.master_username == "{{ username }}" + - "'port' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.db_cluster_parameter_group == "{{ new_db_parameter_group_name }}" + - "'status' in _result_modify_db_parameter_group_name" + - _result_modify_db_parameter_group_name.status == "available" + - "'tags' in _result_modify_db_parameter_group_name" + - "'vpc_security_groups' in _result_modify_db_parameter_group_name" + - name: Delete DB cluster without creating a final snapshot (CHECK MODE) rds_cluster: state: absent @@ -195,3 +250,12 @@ cluster_id: "{{ cluster_id }}" skip_final_snapshot: True ignore_errors: true + + - name: Delete cluster parameter group + command: 'aws rds delete-db-cluster-parameter-group --db-cluster-parameter-group-name {{ new_db_parameter_group_name }}' + environment: + AWS_ACCESS_KEY_ID: "{{ aws_access_key }}" + AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}" + AWS_SESSION_TOKEN: "{{ security_token | default('') }}" + AWS_DEFAULT_REGION: "{{ aws_region }}" + ignore_errors: true