Skip to content

Add Performance Insights and Database Insights to rds_cluster.py #2543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/2543-add_PI_rds_cluster.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Add functionality to enable Performance Insights and Database Insights at cluster level (https://github.com/ansible-collections/amazon.aws/pull/2543).
60 changes: 58 additions & 2 deletions plugins/modules/rds_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
description:
- The character set to associate with the DB cluster.
type: str
database_insights_mode:
description:
- Indicates which mode of Database Insights to enable for the target DB cluster.
choices:
- standard
- advanced
type: str
version_added: 9.5.0
database_name:
description:
- The name for your database. If a name is not provided Amazon RDS will not create a database.
Expand Down Expand Up @@ -177,6 +185,11 @@
- Enable mapping of AWS Identity and Access Management (IAM) accounts to database accounts.
If this option is omitted when creating the cluster, Amazon RDS sets this to C(false).
type: bool
enable_performance_insights:
description:
- Whether to enable Performance Insights for the DB cluster.
type: bool
version_added: 9.5.0
allocated_storage:
description:
- The amount of storage in gibibytes (GiB) to allocate to each DB instance in the Multi-AZ DB cluster.
Expand Down Expand Up @@ -285,6 +298,16 @@
description:
- The option group to associate with the DB cluster.
type: str
performance_insights_kms_key_id:
description:
- The AWS KMS key identifier (ARN, name, or alias) for encryption of Performance Insights data.
type: str
version_added: 9.5.0
performance_insights_retention_period:
description:
- The amount of time, in days, to retain Performance Insights data. Valid values are V(7) or V(731).
type: int
version_added: 9.5.0
port:
description:
- The port number on which the instances in the DB cluster accept connections. If not specified, Amazon RDS
Expand Down Expand Up @@ -784,7 +807,6 @@
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list


@AWSRetry.jittered_backoff(retries=10)
def _describe_db_clusters(**params):
try:
Expand Down Expand Up @@ -822,13 +844,17 @@ def get_create_options(params_dict):
"DBClusterIdentifier",
"DBClusterParameterGroupName",
"DBSubnetGroupName",
"DatabaseInsightsMode",
"DatabaseName",
"EnableCloudwatchLogsExports",
"EnableIAMDatabaseAuthentication",
"EnablePerformanceInsights",
"KmsKeyId",
"Engine",
"EngineMode",
"EngineVersion",
"PerformanceInsightsKMSKeyId",
"PerformanceInsightsRetentionPeriod",
"PreferredMaintenanceWindow",
"MasterUserPassword",
"MasterUsername",
Expand Down Expand Up @@ -865,15 +891,19 @@ def get_modify_options(params_dict, force_update_password):
"BacktrackWindow",
"BackupRetentionPeriod",
"PreferredBackupWindow",
"DatabaseInsightsMode",
"DBClusterIdentifier",
"DBClusterParameterGroupName",
"EnableIAMDatabaseAuthentication",
"EnablePerformanceInsights",
"EngineVersion",
"PreferredMaintenanceWindow",
"MasterUserPassword",
"NewDBClusterIdentifier",
"OptionGroupName",
"Port",
"PerformanceInsightsKMSKeyId",
"PerformanceInsightsRetentionPeriod",
"VpcSecurityGroupIds",
"EnableIAMDatabaseAuthentication",
"CloudwatchLogsExportConfiguration",
Expand Down Expand Up @@ -934,7 +964,7 @@ def get_restore_s3_options(params_dict):
"EnableHttpEndpoint",
"CopyTagsToSnapshot",
"Domain",
"DomainIAMRoleName",
"DomainIAMRoleName"
]

return dict((k, v) for k, v in params_dict.items() if k in options and v is not None)
Expand All @@ -949,10 +979,13 @@ def get_restore_snapshot_options(params_dict):
"DatabaseName",
"EnableCloudwatchLogsExports",
"EnableIAMDatabaseAuthentication",
"EnablePerformanceInsights",
"Engine",
"EngineVersion",
"KmsKeyId",
"OptionGroupName",
"PerformanceInsightsKMSKeyId",
"PerformanceInsightsRetentionPeriod",
"Port",
"SnapshotIdentifier",
"Tags",
Expand All @@ -973,8 +1006,11 @@ def get_restore_cluster_options(params_dict):
"DBSubnetGroupName",
"EnableCloudwatchLogsExports",
"EnableIAMDatabaseAuthentication",
"EnablePerformanceInsights",
"KmsKeyId",
"OptionGroupName",
"PerformanceInsightsKMSKeyId",
"PerformanceInsightsRetentionPeriod",
"Port",
"RestoreToTime",
"RestoreType",
Expand Down Expand Up @@ -1092,6 +1128,22 @@ def changing_cluster_options(modify_params, current_cluster):
g["DBClusterOptionGroupName"] for g in current_cluster["DBClusterOptionGroupMemberships"]
]:
changing_params["OptionGroupName"] = option_group

enable_performance_insights = modify_params.pop("EnablePerformanceInsights", None)
if enable_performance_insights != current_cluster["EnablePerformanceInsights"]:
changing_params["EnablePerformanceInsights"] = enable_performance_insights

performance_insights_kms_key_id = modify_params.pop("PerformanceInsightsKMSKeyId", None)
if performance_insights_kms_key_id != current_cluster["PerformanceInsightsKMSKeyId"]:
changing_params["PerformanceInsightsKMSKeyId"] = performance_insights_kms_key_id

performance_insights_retention_period = modify_params.pop("PerformanceInsightsRetentionPeriod", None)
if performance_insights_retention_period != current_cluster["PerformanceInsightsRetentionPeriod"]:
changing_params["PerformanceInsightsRetentionPeriod"] = performance_insights_retention_period

database_insights_mode = modify_params.pop("DatabaseInsightsMode", None)
if database_insights_mode != current_cluster["DatabaseInsightsMode"]:
changing_params["DatabaseInsightsMode"] = database_insights_mode

vpc_sgs = modify_params.pop("VpcSecurityGroupIds", None)
if vpc_sgs:
Expand Down Expand Up @@ -1259,10 +1311,12 @@ def main():
backup_retention_period=dict(type="int", default=1),
character_set_name=dict(),
database_name=dict(aliases=["db_name"]),
database_insights_mode=dict(choices=["standard","advanced"]),
db_cluster_identifier=dict(required=True, aliases=["cluster_id", "id", "cluster_name"]),
db_cluster_parameter_group_name=dict(),
db_subnet_group_name=dict(),
enable_cloudwatch_logs_exports=dict(type="list", elements="str"),
enable_performance_insights=dict(type="bool"),
deletion_protection=dict(type="bool"),
global_cluster_identifier=dict(),
enable_http_endpoint=dict(type="bool"),
Expand All @@ -1286,6 +1340,8 @@ def main():
new_db_cluster_identifier=dict(aliases=["new_cluster_id", "new_id", "new_cluster_name"]),
option_group_name=dict(),
port=dict(type="int"),
performance_insights_kms_key_id=dict(),
performance_insights_retention_period=dict(type="int"),
preferred_backup_window=dict(aliases=["backup_window"]),
preferred_maintenance_window=dict(aliases=["maintenance_window"]),
remove_from_global_db=dict(type="bool"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
time=20m
cloud/aws
rds_cluster
rds_cluster_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# Create cluster
cluster_id: ansible-test-cluster-{{ tiny_prefix }}
username: testrdsusername
password: test-rds_password
engine: aurora-mysql
db_port: 3306
tags_create:
Name: ansible-test-cluster-{{ tiny_prefix }}
Created_By: Ansible_rds_cluster_integration_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
- module_defaults:
group/aws:
region: "{{ aws_region }}"
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
session_token: "{{ security_token | default(omit) }}"
block:
- name: Ensure the resource doesn't exist
amazon.aws.rds_cluster:
id: "{{ cluster_id }}"
state: absent
engine: "{{ engine }}"
username: "{{ username }}"
password: "{{ password }}"
skip_final_snapshot: true
register: _result_delete_db_cluster

- ansible.builtin.assert:
that:
- not _result_delete_db_cluster.changed
ignore_errors: true

- name: Get info of all existing clusters
amazon.aws.rds_cluster_info:
register: _result_cluster_info

- ansible.builtin.assert:
that:
- _result_cluster_info is successful

- name: Create Cluster with Performance Insights Enabled and Database Insights Enabled (CHECK MODE)
amazon.aws.rds_cluster:
engine: "{{ engine }}"
username: "{{ username }}"
password: "{{ password }}"
cluster_id: "{{ cluster_id }}"
enable_performance_insights: true
performance_insights_retention_period: 7
database_insights_mode: standard
tags: "{{ tags_create }}"
register: _result_create_db_cluster
check_mode: true

- ansible.builtin.assert:
that:
- _result_create_db_cluster.changed

- name: Create Cluster with Performance Insights Enabled and Database Insights Enabled (CHECK MODE)
amazon.aws.rds_cluster:
engine: "{{ engine }}"
username: "{{ username }}"
password: "{{ password }}"
cluster_id: "{{ cluster_id }}"
enable_performance_insights: true
performance_insights_retention_period: 7
database_insights_mode: standard
tags: "{{ tags_create }}"
register: _result_create_db_cluster

- ansible.builtin.assert:
that:
- _result_create_db_cluster.changed
- "'allocated_storage' in _result_create_db_cluster"
- _result_create_db_cluster.allocated_storage == 1
- "'cluster_create_time' in _result_create_db_cluster"
- _result_create_db_cluster.copy_tags_to_snapshot == false
- "'db_cluster_arn' in _result_create_db_cluster"
- "'db_cluster_identifier' in _result_create_db_cluster"
- _result_create_db_cluster.db_cluster_identifier == cluster_id
- "'db_cluster_parameter_group' in _result_create_db_cluster"
- "'db_cluster_resource_id' in _result_create_db_cluster"
- "'endpoint' in _result_create_db_cluster"
- "'engine' in _result_create_db_cluster"
- _result_create_db_cluster.engine == engine
- "'engine_mode' in _result_create_db_cluster"
- _result_create_db_cluster.engine_mode == "provisioned"
- "'engine_version' in _result_create_db_cluster"
- "'master_username' in _result_create_db_cluster"
- _result_create_db_cluster.master_username == username
- "'port' in _result_create_db_cluster"
- _result_create_db_cluster.port == db_port
- "'status' in _result_create_db_cluster"
- _result_create_db_cluster.status == 'available'
- _result_create_db_cluster.storage_encrypted == false
- "'tags' in _result_create_db_cluster"
- _result_create_db_cluster.tags | length == 2
- _result_create_db_cluster.tags["Created_By"] == tags_create["Created_By"]
- _result_create_db_cluster.tags["Name"] == tags_create["Name"]
- "'vpc_security_groups' in _result_create_db_cluster"
- _"'enable_performance_insights' in _result_create_db_cluster"
- _result_create_db_cluster.enable_performance_insights == true
- _"'performance_insights_retention_period' in _result_create_db_cluster"
- _result_create_db_cluster.performance_insights_retention_period == 7
- _"'database_insights_mode' in _result_create_db_cluster"
- _result_create_db_cluster.database_insights_mode == standard
- name: Get info of the existing cluster
amazon.aws.rds_cluster_info:
cluster_id: "{{ cluster_id }}"
register: result_cluster_info

- ansible.builtin.assert:
that:
- result_cluster_info is successful
Loading