From 8c000278af548173575e16c7208468f405dfc1c6 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Wed, 5 Jul 2023 14:21:55 -0400 Subject: [PATCH 01/20] added start and stop cluster --- plugins/module_utils/rds.py | 2 ++ plugins/modules/rds_cluster.py | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/plugins/module_utils/rds.py b/plugins/module_utils/rds.py index 2de153d0446..d4fb5380748 100644 --- a/plugins/module_utils/rds.py +++ b/plugins/module_utils/rds.py @@ -35,6 +35,8 @@ "remove_tags_from_resource", "list_tags_for_resource", "promote_read_replica_db_cluster", + "stop_db_cluster", + "start_db_cluster", ] instance_method_names = [ "create_db_instance", diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 7acf6717f56..9e35392a23e 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -25,9 +25,10 @@ # General module options state: description: Whether the snapshot should exist or not. - choices: ['present', 'absent'] + choices: ['present', 'absent', 'stopped'] default: 'present' type: str + version_added: 6.3.0 creation_source: description: Which source to use if creating from a template (an existing cluster, S3 bucket, or snapshot). choices: ['snapshot', 's3', 'cluster'] @@ -689,7 +690,6 @@ sample: sg-12345678 """ - try: import botocore except ImportError: @@ -917,13 +917,20 @@ def get_rds_method_attribute_name(cluster): method_name = None method_options_name = None - if state == "absent": + if state == "stopped": + if cluster and cluster["Status"] not in ["stopping", "stopped"]: + method_name = "stop_db_cluster" + method_options_name = "get_modify_options" + elif state == "absent": if cluster and cluster["Status"] not in ["deleting", "deleted"]: method_name = "delete_db_cluster" - method_options_name = "get_delete_options" + method_options_name = "" else: if cluster: - method_name = "modify_db_cluster" + if cluster["Status"] == "stopped" and state == "present": + method_name = "start_db_cluster" + else: + method_name = "modify_db_cluster" method_options_name = "get_modify_options" elif creation_source == "snapshot": method_name = "restore_db_cluster_from_snapshot" @@ -1030,6 +1037,12 @@ def changing_cluster_options(modify_params, current_cluster): if apply_immediately is not None: changing_params["ApplyImmediately"] = apply_immediately + if module.params["state"] == "present" and current_cluster["Status"] == "stopped": + changing_params["DBClusterIdentifier"] = db_cluster_id + + if module.params["state"] == "stopped": + changing_params["DBClusterIdentifier"] = db_cluster_id + return changing_params @@ -1087,7 +1100,7 @@ def main(): global client arg_spec = dict( - state=dict(choices=["present", "absent"], default="present"), + state=dict(choices=["present", "absent", "stopped"], default="present"), creation_source=dict(type="str", choices=["snapshot", "s3", "cluster"]), force_update_password=dict(type="bool", default=False), promote=dict(type="bool", default=False), From f9369f9df1769e2615b8d33b64792d7310fcc634 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Wed, 5 Jul 2023 14:41:50 -0400 Subject: [PATCH 02/20] added fragment --- changelogs/fragments/1647-test.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/1647-test.yml diff --git a/changelogs/fragments/1647-test.yml b/changelogs/fragments/1647-test.yml new file mode 100644 index 00000000000..23eea054d28 --- /dev/null +++ b/changelogs/fragments/1647-test.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - rds_cluster - add support for another ``type`` choice called ``stopped``. This stops the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). \ No newline at end of file From 1429ca662413279fd6661fa54b1cc7f2bad54288 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 7 Jul 2023 11:50:31 -0400 Subject: [PATCH 03/20] added integration test cases and added more description into fragment --- .../targets/rds_cluster_states/aliases | 3 + .../rds_cluster_states/defaults/main.yml | 5 + .../targets/rds_cluster_states/tasks/main.yml | 166 ++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 tests/integration/targets/rds_cluster_states/aliases create mode 100644 tests/integration/targets/rds_cluster_states/defaults/main.yml create mode 100644 tests/integration/targets/rds_cluster_states/tasks/main.yml diff --git a/tests/integration/targets/rds_cluster_states/aliases b/tests/integration/targets/rds_cluster_states/aliases new file mode 100644 index 00000000000..eef27d81165 --- /dev/null +++ b/tests/integration/targets/rds_cluster_states/aliases @@ -0,0 +1,3 @@ +time=20m +cloud/aws +rds_cluster \ No newline at end of file diff --git a/tests/integration/targets/rds_cluster_states/defaults/main.yml b/tests/integration/targets/rds_cluster_states/defaults/main.yml new file mode 100644 index 00000000000..ef5c551d49e --- /dev/null +++ b/tests/integration/targets/rds_cluster_states/defaults/main.yml @@ -0,0 +1,5 @@ +cluster_id: ansible-test-cluster-{{ tiny_prefix }} +username: testrdsusername +password: test-rds_password +engine: aurora +db_cluster_instance_class: db.r5.large \ No newline at end of file diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml new file mode 100644 index 00000000000..682465c47b9 --- /dev/null +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -0,0 +1,166 @@ +- module_defaults: + group/aws: + region: '{{ aws_region }}' + aws_access_key: '{{ aws_access_key }}' + aws_secret_key: '{{ aws_secret_key }}' + security_token: '{{ security_token | default(omit) }}' + block: + # ------------------------------------------------------------------------------------------ + # Create DB cluster + - name: Ensure the resource doesn't exist + rds_cluster: + id: '{{ cluster_id }}' + state: absent + engine: '{{ engine}}' + username: '{{ username }}' + password: '{{ password }}' + skip_final_snapshot: true + register: _result_delete_db_cluster + + - assert: + that: + - not _result_delete_db_cluster.changed + ignore_errors: yes + + - name: Create an Aurora-PostgreSQL DB cluster + rds_cluster: + id: '{{ cluster_id }}' + state: present + engine: aurora-postgresql + engine_mode: provisioned + db_cluster_instance_class: '{{ db_cluster_instance_class }}' + username: '{{ username }}' + password: '{{ password }}' + register: _result_create_source_db_cluster + + - assert: + that: + - _result_create_source_db_cluster.changed + - _result_create_source_db_cluster.changed + - "'allocated_storage' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.allocated_storage == 1 + - "'cluster_create_time' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.copy_tags_to_snapshot == false + - "'db_cluster_arn' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.db_cluster_identifier == '{{ cluster_id }}' + - "'db_cluster_parameter_group' in _result_create_source_db_cluster" + - "'db_cluster_resource_id' in _result_create_source_db_cluster" + - "'endpoint' in _result_create_source_db_cluster" + - "'engine' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.engine == "aurora-postgresql" + - "'engine_mode' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.engine_mode == "provisioned" + - "'engine_version' in _result_create_source_db_cluster" + - "'master_username' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.master_username == "{{ username }}" + - "'port' in _result_create_source_db_cluster" + - "'status' in _result_create_source_db_cluster" + - _result_create_source_db_cluster.status == "available" + - "'tags' in _result_create_source_db_cluster" + - "'vpc_security_groups' in _result_create_source_db_cluster" + + # ------------------------------------------------------------------------------------------ + # Test stopping db clusters + - name: Stop db clusters - checkmode + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: stopped + register: check_stopped_cluster + check_mode: yes + + - assert: + that: + - check_stopped_cluster.changed + + - name: Stop db clusters + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: stopped + register: stopped_cluster + + - assert: + that: + - stopped_cluster.changed + + - name: Wait until db clusters state is stopped + amazon.aws.rds_cluster_info: + cluster_id: '{{ cluster_id }}' + register: stopped_cluster_info + retries: 30 + delay: 60 + until: stopped_cluster_info.clusters[0].status == "stopped" + + - name: Stop db clusters (idempotence) - checkmode + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: stopped + register: check_stopped_cluster_idem + check_mode: yes + + - assert: + that: + - not check_stopped_cluster_idem.changed + + - name: Stop db clusters (idempotence) + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: stopped + register: stopped_cluster_idem + + - assert: + that: + - not stopped_cluster_idem.changed + + # ------------------------------------------------------------------------------------------ + # Test starting DB clusters + - name: Start db clusters - checkmode + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: started + register: started_cluster + check_mode: yes + + - assert: + that: + - started_cluster.changed + + - name: Start db clusters + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: started + register: started_cluster + + - assert: + that: + - started_cluster.changed + + - name: Start db clusters (idempotence) - checkmode + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: started + register: started_cluster + check_mode: yes + + - assert: + that: + - not started_cluster.changed + + - name: Start db clusters + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: started + register: started_cluster + + - assert: + that: + - not started_cluster.changed + + # ------------------------------------------------------------------------------------------ + # Cleanup starts here + always: + - name: Delete db cluster without creating a final snapshot + rds_cluster: + state: absent + cluster_id: '{{ cluster_id }}' + skip_final_snapshot: true + ignore_errors: true \ No newline at end of file From eef40af8da764f507f5e8b9570c207fba4fc03d6 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 7 Jul 2023 15:35:49 -0400 Subject: [PATCH 04/20] rename fragement and changed get_rds_method_attribute_name method --- ... => 1647-add-type-started-and-stopped.yml} | 1 + plugins/modules/rds_cluster.py | 25 ++++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) rename changelogs/fragments/{1647-test.yml => 1647-add-type-started-and-stopped.yml} (52%) diff --git a/changelogs/fragments/1647-test.yml b/changelogs/fragments/1647-add-type-started-and-stopped.yml similarity index 52% rename from changelogs/fragments/1647-test.yml rename to changelogs/fragments/1647-add-type-started-and-stopped.yml index 23eea054d28..0627f064c56 100644 --- a/changelogs/fragments/1647-test.yml +++ b/changelogs/fragments/1647-add-type-started-and-stopped.yml @@ -1,3 +1,4 @@ --- minor_changes: + - rds_cluster - add support for another ``type`` choice called ``started``. This starts the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). - rds_cluster - add support for another ``type`` choice called ``stopped``. This stops the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). \ No newline at end of file diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 9e35392a23e..0bd80225f0c 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -25,7 +25,7 @@ # General module options state: description: Whether the snapshot should exist or not. - choices: ['present', 'absent', 'stopped'] + choices: ['present', 'absent', 'started', 'stopped'] default: 'present' type: str version_added: 6.3.0 @@ -917,20 +917,21 @@ def get_rds_method_attribute_name(cluster): method_name = None method_options_name = None - if state == "stopped": + if state == "absent": + if cluster and cluster["Status"] not in ["deleting", "deleted"]: + method_name = "delete_db_cluster" + method_options_name = "get_delete_options" + elif state == "started": + if cluster and cluster["Status"] not in ["starting", "started"]: + method_name = "start_db_cluster" + method_options_name = "get_modify_options" + elif state == "stopped": if cluster and cluster["Status"] not in ["stopping", "stopped"]: method_name = "stop_db_cluster" method_options_name = "get_modify_options" - elif state == "absent": - if cluster and cluster["Status"] not in ["deleting", "deleted"]: - method_name = "delete_db_cluster" - method_options_name = "" else: if cluster: - if cluster["Status"] == "stopped" and state == "present": - method_name = "start_db_cluster" - else: - method_name = "modify_db_cluster" + method_name = "modify_db_cluster" method_options_name = "get_modify_options" elif creation_source == "snapshot": method_name = "restore_db_cluster_from_snapshot" @@ -1037,7 +1038,7 @@ def changing_cluster_options(modify_params, current_cluster): if apply_immediately is not None: changing_params["ApplyImmediately"] = apply_immediately - if module.params["state"] == "present" and current_cluster["Status"] == "stopped": + if module.params["state"] == "started": changing_params["DBClusterIdentifier"] = db_cluster_id if module.params["state"] == "stopped": @@ -1100,7 +1101,7 @@ def main(): global client arg_spec = dict( - state=dict(choices=["present", "absent", "stopped"], default="present"), + state=dict(choices=["present", "absent", "started", "stopped"], default="present"), creation_source=dict(type="str", choices=["snapshot", "s3", "cluster"]), force_update_password=dict(type="bool", default=False), promote=dict(type="bool", default=False), From 9ee6485e2543def2959502affcd3dbc22ba6b62f Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 7 Jul 2023 15:56:41 -0400 Subject: [PATCH 05/20] typo in the fragment --- changelogs/fragments/1647-add-type-started-and-stopped.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changelogs/fragments/1647-add-type-started-and-stopped.yml b/changelogs/fragments/1647-add-type-started-and-stopped.yml index 0627f064c56..5a13a5848c1 100644 --- a/changelogs/fragments/1647-add-type-started-and-stopped.yml +++ b/changelogs/fragments/1647-add-type-started-and-stopped.yml @@ -1,4 +1,4 @@ --- minor_changes: - - rds_cluster - add support for another ``type`` choice called ``started``. This starts the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). - - rds_cluster - add support for another ``type`` choice called ``stopped``. This stops the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). \ No newline at end of file + - rds_cluster - add support for another ``state`` choice called ``started``. This starts the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). + - rds_cluster - add support for another ``state`` choice called ``stopped``. This stops the rds cluster (https://github.com/ansible-collections/amazon.aws/pull/1647/files). \ No newline at end of file From fa9e73237ed5ccc0604fea63ca33ef5c7e04b28b Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 7 Jul 2023 17:43:03 -0400 Subject: [PATCH 06/20] changed function get_rds_method_attribute_name --- tests/integration/targets/rds_cluster_states/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index 682465c47b9..f18b5102b39 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -144,7 +144,7 @@ - assert: that: - not started_cluster.changed - + - name: Start db clusters amazon.aws.rds_cluster: cluster_id: '{{ cluster_id }}' From 7f45e5248bf7431c1f8e544633e090751dae6f39 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Mon, 10 Jul 2023 10:24:03 -0400 Subject: [PATCH 07/20] made changes to get_rds_method_attribute_name and more clarified check variable name --- plugins/modules/rds_cluster.py | 2 +- tests/integration/targets/rds_cluster_states/tasks/main.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 0bd80225f0c..b2eb376b630 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -922,7 +922,7 @@ def get_rds_method_attribute_name(cluster): method_name = "delete_db_cluster" method_options_name = "get_delete_options" elif state == "started": - if cluster and cluster["Status"] not in ["starting", "started"]: + if cluster and cluster["Status"] not in ["starting", "started", "available"]: method_name = "start_db_cluster" method_options_name = "get_modify_options" elif state == "stopped": diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index f18b5102b39..3fc804b6be6 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -138,12 +138,12 @@ amazon.aws.rds_cluster: cluster_id: '{{ cluster_id }}' state: started - register: started_cluster + register: check_started_cluster check_mode: yes - assert: that: - - not started_cluster.changed + - not check_started_cluster.changed - name: Start db clusters amazon.aws.rds_cluster: From ebeb15795ecf7db84f4997e684f921d7e096528f Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Tue, 11 Jul 2023 14:29:15 -0400 Subject: [PATCH 08/20] started and stopped can only be used with Aurora DB clusters --- plugins/modules/rds_cluster.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index b2eb376b630..cf1a00a97fc 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -1039,6 +1039,8 @@ def changing_cluster_options(modify_params, current_cluster): changing_params["ApplyImmediately"] = apply_immediately if module.params["state"] == "started": + if current_cluster["engine"] in ["mysql", "postgres"]: + module.fail_json("Only aurora clusters can use the state started or stopped") changing_params["DBClusterIdentifier"] = db_cluster_id if module.params["state"] == "stopped": From e665151b08115f001a3fc9f359d57ba5bd0b7c0b Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Tue, 11 Jul 2023 15:46:43 -0400 Subject: [PATCH 09/20] changes to the defaults --- .../rds_cluster_states/defaults/main.yml | 1 + .../targets/rds_cluster_states/tasks/main.yml | 82 ++++++++++++++++++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/tests/integration/targets/rds_cluster_states/defaults/main.yml b/tests/integration/targets/rds_cluster_states/defaults/main.yml index ef5c551d49e..97371a6f729 100644 --- a/tests/integration/targets/rds_cluster_states/defaults/main.yml +++ b/tests/integration/targets/rds_cluster_states/defaults/main.yml @@ -2,4 +2,5 @@ cluster_id: ansible-test-cluster-{{ tiny_prefix }} username: testrdsusername password: test-rds_password engine: aurora +error-engine: postgres db_cluster_instance_class: db.r5.large \ No newline at end of file diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index 3fc804b6be6..2c708cffe13 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -36,7 +36,6 @@ - assert: that: - _result_create_source_db_cluster.changed - - _result_create_source_db_cluster.changed - "'allocated_storage' in _result_create_source_db_cluster" - _result_create_source_db_cluster.allocated_storage == 1 - "'cluster_create_time' in _result_create_source_db_cluster" @@ -58,7 +57,7 @@ - _result_create_source_db_cluster.status == "available" - "'tags' in _result_create_source_db_cluster" - "'vpc_security_groups' in _result_create_source_db_cluster" - + # ------------------------------------------------------------------------------------------ # Test stopping db clusters - name: Stop db clusters - checkmode @@ -163,4 +162,81 @@ state: absent cluster_id: '{{ cluster_id }}' skip_final_snapshot: true - ignore_errors: true \ No newline at end of file + ignore_errors: true + + # # ------------------------------------------------------------------------------------------ + # # Give erros for Postgres DB cluster + # - name: Ensure the resource doesn't exist + # rds_cluster: + # id: PostgresCluster + # state: absent + # engine: postgres + # username: '{{ username }}' + # password: '{{ password }}' + # skip_final_snapshot: true + # register: _result_delete_db_cluster + + # - assert: + # that: + # - not _result_delete_db_cluster.changed + # ignore_errors: yes + + # - name: Create an Postgres DB cluster + # rds_cluster: + # id: PostgresCluster + # state: present + # engine: postgres + # engine_mode: provisioned + # allocated_storage: 1 + # iops: 1000 + # db_cluster_instance_class: '{{ db_cluster_instance_class }}' + # username: 'PostgresCluster' + # password: '{{ password }}' + # register: postgres_cluster + + + # - assert: + # that: + # - postgres_cluster.changed + # - "'allocated_storage' in postgres_cluster" + # - postgres_cluster.allocated_storage == 1 + # - "'cluster_create_time' in postgres_cluster" + # - postgres_cluster.copy_tags_to_snapshot == false + # - "'db_cluster_arn' in postgres_cluster" + # - postgres_cluster.db_cluster_identifier == "PostgresCluster" + # - "'db_cluster_parameter_group' in postgres_cluster" + # - "'db_cluster_resource_id' in postgres_cluster" + # - "'endpoint' in postgres_cluster" + # - "'engine' in postgres_cluster" + # - postgres_cluster.engine == "postgres" + # - "'engine_mode' in postgres_cluster" + # - postgres_cluster.engine_mode == "provisioned" + # - "'engine_version' in postgres_cluster" + # - "'master_username' in postgres_cluster" + # - postgres_cluster.master_username == "{{ username }}" + # - "'port' in postgres_cluster" + # - "'status' in postgres_cluster" + # - postgres_cluster.status == "available" + # - "'tags' in postgres_cluster" + # - "'vpc_security_groups' in postgres_cluster" + + + # - name: Stop Postgres DB cluster + # amazon.aws.rds_cluster: + # cluster_id: PostgresCluster + # state: stopped + # register: postgres_cluster + # ignore_errors: true + + # - assert: + # that: + # - postgres_cluster is failed + # - postgres_cluster.msg == "Only aurora clusters can use the state started or stopped" + + # always: + # - name: Delete db cluster without creating a final snapshot + # rds_cluster: + # state: absent + # cluster_id: postgres_cluster + # skip_final_snapshot: true + # ignore_errors: true \ No newline at end of file From b400509e1e3b03591ef1f793a237334b667c93d0 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Tue, 11 Jul 2023 16:07:27 -0400 Subject: [PATCH 10/20] updated aliases --- tests/integration/targets/rds_cluster_states/aliases | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/rds_cluster_states/aliases b/tests/integration/targets/rds_cluster_states/aliases index eef27d81165..b437e9df50d 100644 --- a/tests/integration/targets/rds_cluster_states/aliases +++ b/tests/integration/targets/rds_cluster_states/aliases @@ -1,3 +1,4 @@ -time=20m +time=30m cloud/aws -rds_cluster \ No newline at end of file +rds_cluster +rds_cluster_info \ No newline at end of file From 8463d0b33f25a2475609993abc81303bb658ed96 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 14 Jul 2023 12:28:52 -0400 Subject: [PATCH 11/20] only allow aurora to be stopped and started --- plugins/modules/rds_cluster.py | 6 +- .../rds_cluster_states/defaults/main.yml | 9 +- .../targets/rds_cluster_states/tasks/main.yml | 165 +++++++++--------- 3 files changed, 94 insertions(+), 86 deletions(-) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index cf1a00a97fc..ca4a80748eb 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -1039,11 +1039,13 @@ def changing_cluster_options(modify_params, current_cluster): changing_params["ApplyImmediately"] = apply_immediately if module.params["state"] == "started": - if current_cluster["engine"] in ["mysql", "postgres"]: - module.fail_json("Only aurora clusters can use the state started or stopped") + if current_cluster["Engine"] in ["mysql", "postgres"]: + module.fail_json("Only aurora clusters can use the state started") changing_params["DBClusterIdentifier"] = db_cluster_id if module.params["state"] == "stopped": + if current_cluster["Engine"] in ["mysql", "postgres"]: + module.fail_json("Only aurora clusters can use the state stopped") changing_params["DBClusterIdentifier"] = db_cluster_id return changing_params diff --git a/tests/integration/targets/rds_cluster_states/defaults/main.yml b/tests/integration/targets/rds_cluster_states/defaults/main.yml index 97371a6f729..f362c83d4c6 100644 --- a/tests/integration/targets/rds_cluster_states/defaults/main.yml +++ b/tests/integration/targets/rds_cluster_states/defaults/main.yml @@ -2,5 +2,10 @@ cluster_id: ansible-test-cluster-{{ tiny_prefix }} username: testrdsusername password: test-rds_password engine: aurora -error-engine: postgres -db_cluster_instance_class: db.r5.large \ No newline at end of file +db_cluster_instance_class: db.r5.large + +mysql_cluster_id: ansible-test-mysql-cluster-{{ tiny_prefix }} +mysql_engine: mysql +mysql_allocated_storage: 100 +mysql_iops: 1000 +mysql_db_cluster_instance_class: db.m5d.large \ No newline at end of file diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index 2c708cffe13..6d5fb341760 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -28,7 +28,6 @@ state: present engine: aurora-postgresql engine_mode: provisioned - db_cluster_instance_class: '{{ db_cluster_instance_class }}' username: '{{ username }}' password: '{{ password }}' register: _result_create_source_db_cluster @@ -116,12 +115,12 @@ amazon.aws.rds_cluster: cluster_id: '{{ cluster_id }}' state: started - register: started_cluster + register: check_started_cluster check_mode: yes - assert: that: - - started_cluster.changed + - check_started_cluster.changed - name: Start db clusters amazon.aws.rds_cluster: @@ -153,90 +152,92 @@ - assert: that: - not started_cluster.changed - + # ------------------------------------------------------------------------------------------ - # Cleanup starts here - always: - - name: Delete db cluster without creating a final snapshot + # Give errors for MySql DB cluster + - name: Ensure the resource doesn't exist rds_cluster: + id: '{{ mysql_cluster_id }}' state: absent - cluster_id: '{{ cluster_id }}' + engine: '{{ mysql_engine }}' + username: '{{ username }}' + password: '{{ password }}' skip_final_snapshot: true - ignore_errors: true - - # # ------------------------------------------------------------------------------------------ - # # Give erros for Postgres DB cluster - # - name: Ensure the resource doesn't exist - # rds_cluster: - # id: PostgresCluster - # state: absent - # engine: postgres - # username: '{{ username }}' - # password: '{{ password }}' - # skip_final_snapshot: true - # register: _result_delete_db_cluster + register: _result_delete_mysql_db_cluster - # - assert: - # that: - # - not _result_delete_db_cluster.changed - # ignore_errors: yes + - assert: + that: + - not _result_delete_mysql_db_cluster.changed + ignore_errors: yes - # - name: Create an Postgres DB cluster - # rds_cluster: - # id: PostgresCluster - # state: present - # engine: postgres - # engine_mode: provisioned - # allocated_storage: 1 - # iops: 1000 - # db_cluster_instance_class: '{{ db_cluster_instance_class }}' - # username: 'PostgresCluster' - # password: '{{ password }}' - # register: postgres_cluster - - - # - assert: - # that: - # - postgres_cluster.changed - # - "'allocated_storage' in postgres_cluster" - # - postgres_cluster.allocated_storage == 1 - # - "'cluster_create_time' in postgres_cluster" - # - postgres_cluster.copy_tags_to_snapshot == false - # - "'db_cluster_arn' in postgres_cluster" - # - postgres_cluster.db_cluster_identifier == "PostgresCluster" - # - "'db_cluster_parameter_group' in postgres_cluster" - # - "'db_cluster_resource_id' in postgres_cluster" - # - "'endpoint' in postgres_cluster" - # - "'engine' in postgres_cluster" - # - postgres_cluster.engine == "postgres" - # - "'engine_mode' in postgres_cluster" - # - postgres_cluster.engine_mode == "provisioned" - # - "'engine_version' in postgres_cluster" - # - "'master_username' in postgres_cluster" - # - postgres_cluster.master_username == "{{ username }}" - # - "'port' in postgres_cluster" - # - "'status' in postgres_cluster" - # - postgres_cluster.status == "available" - # - "'tags' in postgres_cluster" - # - "'vpc_security_groups' in postgres_cluster" + - name: Create an MySql DB cluster + rds_cluster: + id: '{{ mysql_cluster_id }}' + state: present + engine: '{{ mysql_engine }}' + engine_mode: provisioned + allocated_storage: '{{ mysql_allocated_storage }}' + iops: '{{ mysql_iops }}' + db_cluster_instance_class: '{{ mysql_db_cluster_instance_class }}' + username: '{{ username }}' + password: '{{ password }}' + ignore_errors: yes + register: mysql_cluster + + - debug: + var: mysql_cluster + + - assert: + that: + - mysql_cluster.changed + - "'allocated_storage' in mysql_cluster" + - mysql_cluster.allocated_storage == 100 + - "'cluster_create_time' in mysql_cluster" + - mysql_cluster.copy_tags_to_snapshot == false + - "'db_cluster_arn' in mysql_cluster" + - mysql_cluster.db_cluster_identifier == "{{ mysql_cluster_id }}" + - "'db_cluster_parameter_group' in mysql_cluster" + - "'db_cluster_resource_id' in mysql_cluster" + - "'endpoint' in mysql_cluster" + - "'engine' in mysql_cluster" + - mysql_cluster.engine == "{{ mysql_engine }}" + - "'engine_mode' in mysql_cluster" + - mysql_cluster.engine_mode == "provisioned" + - "'engine_version' in mysql_cluster" + - "'master_username' in mysql_cluster" + - mysql_cluster.master_username == "{{ username }}" + - "'port' in mysql_cluster" + - "'status' in mysql_cluster" + - mysql_cluster.status == "available" + - "'tags' in mysql_cluster" + - "'vpc_security_groups' in mysql_cluster" - # - name: Stop Postgres DB cluster - # amazon.aws.rds_cluster: - # cluster_id: PostgresCluster - # state: stopped - # register: postgres_cluster - # ignore_errors: true - - # - assert: - # that: - # - postgres_cluster is failed - # - postgres_cluster.msg == "Only aurora clusters can use the state started or stopped" - - # always: - # - name: Delete db cluster without creating a final snapshot - # rds_cluster: - # state: absent - # cluster_id: postgres_cluster - # skip_final_snapshot: true - # ignore_errors: true \ No newline at end of file + - name: Stop MySql DB cluster + amazon.aws.rds_cluster: + cluster_id: '{{ mysql_cluster_id }}' + state: stopped + register: mysql_cluster + ignore_errors: true + + - assert: + that: + - mysql_cluster is failed + - mysql_cluster.msg == "Only aurora clusters can use the state stopped" + + always: + # ------------------------------------------------------------------------------------------ + # Cleanup starts here + - name: Delete MySql db cluster without creating a final snapshot + rds_cluster: + state: absent + cluster_id: '{{ mysql_cluster_id }}' + skip_final_snapshot: true + ignore_errors: true + + - name: Delete Aurora-PostgreSql db cluster without creating a final snapshot + rds_cluster: + state: absent + cluster_id: '{{ cluster_id }}' + skip_final_snapshot: true + ignore_errors: true \ No newline at end of file From c2f455f6f627f10e256fb356fd817df73e47df84 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 14 Jul 2023 12:49:25 -0400 Subject: [PATCH 12/20] discarded check on start rds cluster --- .../targets/rds_cluster_states/tasks/main.yml | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index 6d5fb341760..967ac70fceb 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -110,18 +110,7 @@ - not stopped_cluster_idem.changed # ------------------------------------------------------------------------------------------ - # Test starting DB clusters - - name: Start db clusters - checkmode - amazon.aws.rds_cluster: - cluster_id: '{{ cluster_id }}' - state: started - register: check_started_cluster - check_mode: yes - - - assert: - that: - - check_started_cluster.changed - + # Test starting DB clusters - name: Start db clusters amazon.aws.rds_cluster: cluster_id: '{{ cluster_id }}' From 569698186237e346f01cd083f850a1f227414074 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 14 Jul 2023 14:14:27 -0400 Subject: [PATCH 13/20] add checkmode for starting db clusters --- .../targets/rds_cluster_states/tasks/main.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index 967ac70fceb..6d5fb341760 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -110,7 +110,18 @@ - not stopped_cluster_idem.changed # ------------------------------------------------------------------------------------------ - # Test starting DB clusters + # Test starting DB clusters + - name: Start db clusters - checkmode + amazon.aws.rds_cluster: + cluster_id: '{{ cluster_id }}' + state: started + register: check_started_cluster + check_mode: yes + + - assert: + that: + - check_started_cluster.changed + - name: Start db clusters amazon.aws.rds_cluster: cluster_id: '{{ cluster_id }}' From f46e124da83d2bd69706d345d2f53911f4bc2b4a Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Fri, 14 Jul 2023 17:03:56 -0400 Subject: [PATCH 14/20] remove debug statements in the integration test --- tests/integration/targets/rds_cluster_states/tasks/main.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/integration/targets/rds_cluster_states/tasks/main.yml b/tests/integration/targets/rds_cluster_states/tasks/main.yml index 6d5fb341760..945fa1abde0 100644 --- a/tests/integration/targets/rds_cluster_states/tasks/main.yml +++ b/tests/integration/targets/rds_cluster_states/tasks/main.yml @@ -184,9 +184,6 @@ ignore_errors: yes register: mysql_cluster - - debug: - var: mysql_cluster - - assert: that: - mysql_cluster.changed From d66e292de9707baeac4b25536509e6bd365fdde0 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Tue, 25 Jul 2023 17:38:08 -0400 Subject: [PATCH 15/20] included unit test for stop and start rds --- plugins/module_utils/rds.py | 2 ++ tests/unit/module_utils/test_rds.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/plugins/module_utils/rds.py b/plugins/module_utils/rds.py index d4fb5380748..054bc6fe1c2 100644 --- a/plugins/module_utils/rds.py +++ b/plugins/module_utils/rds.py @@ -104,6 +104,8 @@ def get_rds_method_attribute(method_name, module): resource = "cluster" if method_name == "delete_db_cluster": waiter = "cluster_deleted" + elif method_name == "stop_db_cluster": + waiter = "cluster_stopped" else: waiter = "cluster_available" # Handle retry codes diff --git a/tests/unit/module_utils/test_rds.py b/tests/unit/module_utils/test_rds.py index 3de8021470a..311f969354f 100644 --- a/tests/unit/module_utils/test_rds.py +++ b/tests/unit/module_utils/test_rds.py @@ -182,6 +182,36 @@ def test__wait_for_cluster_snapshot_status_failed(input, expected): ) ), ), + ( + "start_db_cluster", + { + "new_db_cluster_identifier": "test", + }, + *expected( + rds.Boto3ClientMethod( + name="start_db_cluster", + waiter="cluster_available", + operation_description="start DB cluster", + resource="cluster", + retry_codes=["InvalidDBClusterState"], + ) + ), + ), + ( + "stop_db_cluster", + { + "new_db_cluster_identifier": "test", + }, + *expected( + rds.Boto3ClientMethod( + name="stop_db_cluster", + waiter="cluster_stopped", + operation_description="stop DB cluster", + resource="cluster", + retry_codes=["InvalidDBClusterState"], + ) + ), + ), ( "fake_method", {"wait": False}, From 4844746ee23ee08459962bc6d0c939d1fc586fd2 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Tue, 25 Jul 2023 17:51:40 -0400 Subject: [PATCH 16/20] added description about stopped and started were added in version 6.3.0 and can only be used for Aurora rds cluster --- plugins/modules/rds_cluster.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index ca4a80748eb..90b2a86131c 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -24,7 +24,10 @@ options: # General module options state: - description: Whether the snapshot should exist or not. + description: + - Whether the snapshot should exist or not. + - C(started) and C(stopped) can only be used with aurora clusters + - Support for C(started) and C(stopped) was added in release 6.3.0. choices: ['present', 'absent', 'started', 'stopped'] default: 'present' type: str From d33641c00141c693bc47eb8d089e1e4c64db2f43 Mon Sep 17 00:00:00 2001 From: Taeho Park Date: Thu, 27 Jul 2023 15:25:01 -0400 Subject: [PATCH 17/20] fixed rds.py --- plugins/module_utils/rds.py | 2 -- plugins/modules/rds_cluster.py | 4 ++-- tests/unit/module_utils/test_rds.py | 36 ++++++++++++++--------------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/plugins/module_utils/rds.py b/plugins/module_utils/rds.py index 054bc6fe1c2..d4fb5380748 100644 --- a/plugins/module_utils/rds.py +++ b/plugins/module_utils/rds.py @@ -104,8 +104,6 @@ def get_rds_method_attribute(method_name, module): resource = "cluster" if method_name == "delete_db_cluster": waiter = "cluster_deleted" - elif method_name == "stop_db_cluster": - waiter = "cluster_stopped" else: waiter = "cluster_available" # Handle retry codes diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 90b2a86131c..1aac8ae6d48 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -24,7 +24,7 @@ options: # General module options state: - description: + description: - Whether the snapshot should exist or not. - C(started) and C(stopped) can only be used with aurora clusters - Support for C(started) and C(stopped) was added in release 6.3.0. @@ -867,7 +867,7 @@ def get_restore_s3_options(params_dict): def get_restore_snapshot_options(params_dict): options = [ "AvailabilityZones", - "BacktrackWindow", + "BacktrackWindoc", "DBClusterIdentifier", "DBSubnetGroupName", "DatabaseName", diff --git a/tests/unit/module_utils/test_rds.py b/tests/unit/module_utils/test_rds.py index 311f969354f..eb90acca77b 100644 --- a/tests/unit/module_utils/test_rds.py +++ b/tests/unit/module_utils/test_rds.py @@ -138,75 +138,75 @@ def test__wait_for_cluster_snapshot_status_failed(input, expected): ), ), ( - "restore_db_cluster_from_snapshot", + "start_db_cluster", { "new_db_cluster_identifier": "test", }, *expected( rds.Boto3ClientMethod( - name="restore_db_cluster_from_snapshot", + name="start_db_cluster", waiter="cluster_available", - operation_description="restore DB cluster from snapshot", + operation_description="start DB cluster", resource="cluster", - retry_codes=["InvalidDBClusterSnapshotState"], + retry_codes=["InvalidDBClusterState"], ) ), ), ( - "modify_db_cluster", + "stop_db_cluster", { "new_db_cluster_identifier": "test", }, *expected( rds.Boto3ClientMethod( - name="modify_db_cluster", + name="stop_db_cluster", waiter="cluster_available", - operation_description="modify DB cluster", + operation_description="stop DB cluster", resource="cluster", retry_codes=["InvalidDBClusterState"], ) ), ), ( - "list_tags_for_resource", + "restore_db_cluster_from_snapshot", { "new_db_cluster_identifier": "test", }, *expected( rds.Boto3ClientMethod( - name="list_tags_for_resource", + name="restore_db_cluster_from_snapshot", waiter="cluster_available", - operation_description="list tags for resource", + operation_description="restore DB cluster from snapshot", resource="cluster", - retry_codes=["InvalidDBClusterState"], + retry_codes=["InvalidDBClusterSnapshotState"], ) ), ), ( - "start_db_cluster", + "modify_db_cluster", { "new_db_cluster_identifier": "test", }, *expected( rds.Boto3ClientMethod( - name="start_db_cluster", + name="modify_db_cluster", waiter="cluster_available", - operation_description="start DB cluster", + operation_description="modify DB cluster", resource="cluster", retry_codes=["InvalidDBClusterState"], ) ), ), ( - "stop_db_cluster", + "list_tags_for_resource", { "new_db_cluster_identifier": "test", }, *expected( rds.Boto3ClientMethod( - name="stop_db_cluster", - waiter="cluster_stopped", - operation_description="stop DB cluster", + name="list_tags_for_resource", + waiter="cluster_available", + operation_description="list tags for resource", resource="cluster", retry_codes=["InvalidDBClusterState"], ) From 293334698fd8208a4ca5acc6f1860b289d558d54 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Tue, 1 Aug 2023 12:52:58 +0200 Subject: [PATCH 18/20] Fix typo Signed-off-by: Alina Buzachis --- plugins/modules/rds_cluster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 1aac8ae6d48..6f6b95d6747 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -867,7 +867,7 @@ def get_restore_s3_options(params_dict): def get_restore_snapshot_options(params_dict): options = [ "AvailabilityZones", - "BacktrackWindoc", + "BacktrackWindow", "DBClusterIdentifier", "DBSubnetGroupName", "DatabaseName", From dfc2c6df1497ffe858060ea284f141218a8866b9 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Tue, 1 Aug 2023 09:28:07 -0400 Subject: [PATCH 19/20] Update plugins/modules/rds_cluster.py Co-authored-by: Alina Buzachis --- plugins/modules/rds_cluster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index 6f6b95d6747..f8a9d390684 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -31,7 +31,7 @@ choices: ['present', 'absent', 'started', 'stopped'] default: 'present' type: str - version_added: 6.3.0 + creation_source: description: Which source to use if creating from a template (an existing cluster, S3 bucket, or snapshot). choices: ['snapshot', 's3', 'cluster'] From e1b34f83ac5b14b2d2dd9c73d505eb335962ee72 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Tue, 1 Aug 2023 09:31:42 -0400 Subject: [PATCH 20/20] Fix linting --- plugins/modules/rds_cluster.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/rds_cluster.py b/plugins/modules/rds_cluster.py index f8a9d390684..187bfbe28e6 100644 --- a/plugins/modules/rds_cluster.py +++ b/plugins/modules/rds_cluster.py @@ -31,7 +31,6 @@ choices: ['present', 'absent', 'started', 'stopped'] default: 'present' type: str - creation_source: description: Which source to use if creating from a template (an existing cluster, S3 bucket, or snapshot). choices: ['snapshot', 's3', 'cluster']