| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2022 Ansible Project |
| 5 |
# Copyright (c) 2022 Alina Buzachis (@alinabuzachis) |
| 6 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: rds_cluster_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Obtain information about one or more RDS clusters |
| 12 |
description: |
| 13 |
- Obtain information about one or more RDS clusters. |
| 14 |
- This module was originally added to C(community.aws) in release 3.2.0. |
| 15 |
options: |
| 16 |
db_cluster_identifier: |
| 17 |
description: |
| 18 |
- The user-supplied DB cluster identifier. |
| 19 |
- If this parameter is specified, information from only the specific DB cluster is returned. |
| 20 |
aliases: |
| 21 |
- cluster_id |
| 22 |
- id |
| 23 |
- cluster_name |
| 24 |
type: str |
| 25 |
filters: |
| 26 |
description: |
| 27 |
- A filter that specifies one or more DB clusters to describe. |
| 28 |
See U(https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_DescribeDBClusters.html). |
| 29 |
type: dict |
| 30 |
author: |
| 31 |
- Alina Buzachis (@alinabuzachis) |
| 32 |
extends_documentation_fragment: |
| 33 |
- amazon.aws.common.modules |
| 34 |
- amazon.aws.region.modules |
| 35 |
- amazon.aws.boto3 |
| 36 |
""" |
| 37 |
|
| 38 |
EXAMPLES = r""" |
| 39 |
- name: Get info of all existing DB clusters |
| 40 |
amazon.aws.rds_cluster_info: |
| 41 |
register: _result_cluster_info |
| 42 |
|
| 43 |
- name: Get info on a specific DB cluster |
| 44 |
amazon.aws.rds_cluster_info: |
| 45 |
cluster_id: "{{ cluster_id }}" |
| 46 |
register: _result_cluster_info |
| 47 |
|
| 48 |
- name: Get info all DB clusters with specific engine |
| 49 |
amazon.aws.rds_cluster_info: |
| 50 |
engine: "aurora" |
| 51 |
register: _result_cluster_info |
| 52 |
""" |
| 53 |
|
| 54 |
RETURN = r""" |
| 55 |
clusters: |
| 56 |
description: List of RDS clusters. |
| 57 |
returned: always |
| 58 |
type: list |
| 59 |
contains: |
| 60 |
activity_stream_status: |
| 61 |
description: The status of the database activity stream. |
| 62 |
type: str |
| 63 |
sample: "stopped" |
| 64 |
allocated_storage: |
| 65 |
description: |
| 66 |
- The allocated storage size in gigabytes. Since aurora storage size is not fixed this is |
| 67 |
always 1 for aurora database engines. |
| 68 |
type: int |
| 69 |
sample: 1 |
| 70 |
associated_roles: |
| 71 |
description: |
| 72 |
- A list of dictionaries of the AWS Identity and Access Management (IAM) roles that are associated |
| 73 |
with the DB cluster. Each dictionary contains the role_arn and the status of the role. |
| 74 |
type: list |
| 75 |
sample: [] |
| 76 |
auto_minor_version_upgrade: |
| 77 |
description: Specifies whether minor engine upgrades are applied automatically to the DB cluster during the maintenance window. |
| 78 |
returned: always |
| 79 |
type: bool |
| 80 |
sample: true |
| 81 |
availability_zones: |
| 82 |
description: The list of availability zones that instances in the DB cluster can be created in. |
| 83 |
type: list |
| 84 |
sample: |
| 85 |
- us-east-1c |
| 86 |
- us-east-1a |
| 87 |
- us-east-1e |
| 88 |
backup_retention_period: |
| 89 |
description: The number of days for which automatic DB snapshots are retained. |
| 90 |
type: int |
| 91 |
sample: 1 |
| 92 |
cluster_create_time: |
| 93 |
description: The time in UTC when the DB cluster was created. |
| 94 |
type: str |
| 95 |
sample: "2018-06-29T14:08:58.491000+00:00" |
| 96 |
copy_tags_to_snapshot: |
| 97 |
description: |
| 98 |
- Specifies whether tags are copied from the DB cluster to snapshots of the DB cluster. |
| 99 |
type: bool |
| 100 |
sample: false |
| 101 |
cross_account_clone: |
| 102 |
description: |
| 103 |
- Specifies whether the DB cluster is a clone of a DB cluster owned by a different Amazon Web Services account. |
| 104 |
type: bool |
| 105 |
sample: false |
| 106 |
db_cluster_arn: |
| 107 |
description: The Amazon Resource Name (ARN) for the DB cluster. |
| 108 |
type: str |
| 109 |
sample: "arn:aws:rds:us-east-1:123456789012:cluster:rds-cluster-demo" |
| 110 |
db_cluster_identifier: |
| 111 |
description: The lowercase user-supplied DB cluster identifier. |
| 112 |
type: str |
| 113 |
sample: "rds-cluster-demo" |
| 114 |
db_cluster_members: |
| 115 |
description: |
| 116 |
- A list of dictionaries containing information about the instances in the cluster. |
| 117 |
Each dictionary contains the I(db_instance_identifier), I(is_cluster_writer) (bool), |
| 118 |
I(db_cluster_parameter_group_status), and I(promotion_tier) (int). |
| 119 |
type: list |
| 120 |
sample: [] |
| 121 |
db_cluster_parameter_group: |
| 122 |
description: The parameter group associated with the DB cluster. |
| 123 |
type: str |
| 124 |
sample: "default.aurora5.6" |
| 125 |
db_cluster_resource_id: |
| 126 |
description: The AWS Region-unique, immutable identifier for the DB cluster. |
| 127 |
type: str |
| 128 |
sample: "cluster-D2MEQDN3BQNXDF74K6DQJTHASU" |
| 129 |
db_subnet_group: |
| 130 |
description: The name of the subnet group associated with the DB Cluster. |
| 131 |
type: str |
| 132 |
sample: "default" |
| 133 |
deletion_protection: |
| 134 |
description: |
| 135 |
- Indicates if the DB cluster has deletion protection enabled. |
| 136 |
The database can't be deleted when deletion protection is enabled. |
| 137 |
type: bool |
| 138 |
sample: false |
| 139 |
domain_memberships: |
| 140 |
description: |
| 141 |
- The Active Directory Domain membership records associated with the DB cluster. |
| 142 |
type: list |
| 143 |
sample: [] |
| 144 |
earliest_restorable_time: |
| 145 |
description: The earliest time to which a database can be restored with point-in-time restore. |
| 146 |
type: str |
| 147 |
sample: "2018-06-29T14:09:34.797000+00:00" |
| 148 |
endpoint: |
| 149 |
description: The connection endpoint for the primary instance of the DB cluster. |
| 150 |
type: str |
| 151 |
sample: "rds-cluster-demo.cluster-cvlrtwiennww.us-east-1.rds.amazonaws.com" |
| 152 |
engine: |
| 153 |
description: The database engine of the DB cluster. |
| 154 |
type: str |
| 155 |
sample: "aurora" |
| 156 |
engine_mode: |
| 157 |
description: The DB engine mode of the DB cluster. |
| 158 |
type: str |
| 159 |
sample: "provisioned" |
| 160 |
engine_version: |
| 161 |
description: The database engine version. |
| 162 |
type: str |
| 163 |
sample: "5.6.10a" |
| 164 |
hosted_zone_id: |
| 165 |
description: The ID that Amazon Route 53 assigns when you create a hosted zone. |
| 166 |
type: str |
| 167 |
sample: "Z2R2ITUGPM61AM" |
| 168 |
http_endpoint_enabled: |
| 169 |
description: |
| 170 |
- A value that indicates whether the HTTP endpoint for an Aurora Serverless DB cluster is enabled. |
| 171 |
type: bool |
| 172 |
sample: false |
| 173 |
iam_database_authentication_enabled: |
| 174 |
description: Whether IAM accounts may be mapped to database accounts. |
| 175 |
type: bool |
| 176 |
sample: false |
| 177 |
latest_restorable_time: |
| 178 |
description: The latest time to which a database can be restored with point-in-time restore. |
| 179 |
type: str |
| 180 |
sample: "2018-06-29T14:09:34.797000+00:00" |
| 181 |
master_username: |
| 182 |
description: The master username for the DB cluster. |
| 183 |
type: str |
| 184 |
sample: "username" |
| 185 |
multi_az: |
| 186 |
description: Whether the DB cluster has instances in multiple availability zones. |
| 187 |
type: bool |
| 188 |
sample: false |
| 189 |
port: |
| 190 |
description: The port that the database engine is listening on. |
| 191 |
type: int |
| 192 |
sample: 3306 |
| 193 |
preferred_backup_window: |
| 194 |
description: The UTC weekly time range during which system maintenance can occur. |
| 195 |
type: str |
| 196 |
sample: "10:18-10:48" |
| 197 |
preferred_maintenance_window: |
| 198 |
description: The UTC weekly time range during which system maintenance can occur. |
| 199 |
type: str |
| 200 |
sample: "tue:03:23-tue:03:53" |
| 201 |
read_replica_identifiers: |
| 202 |
description: A list of read replica ID strings associated with the DB cluster. |
| 203 |
type: list |
| 204 |
sample: [] |
| 205 |
reader_endpoint: |
| 206 |
description: The reader endpoint for the DB cluster. |
| 207 |
type: str |
| 208 |
sample: "rds-cluster-demo.cluster-ro-cvlrtwiennww.us-east-1.rds.amazonaws.com" |
| 209 |
status: |
| 210 |
description: The status of the DB cluster. |
| 211 |
type: str |
| 212 |
sample: "available" |
| 213 |
storage_encrypted: |
| 214 |
description: Whether the DB cluster is storage encrypted. |
| 215 |
type: bool |
| 216 |
sample: false |
| 217 |
tag_list: |
| 218 |
description: A list of tags consisting of key-value pairs. |
| 219 |
type: list |
| 220 |
elements: dict |
| 221 |
sample: [ |
| 222 |
{ |
| 223 |
"key": "Created_By", |
| 224 |
"value": "Ansible_rds_cluster_integration_test" |
| 225 |
} |
| 226 |
] |
| 227 |
tags: |
| 228 |
description: A dictionary of key value pairs. |
| 229 |
type: dict |
| 230 |
sample: { |
| 231 |
"Name": "rds-cluster-demo" |
| 232 |
} |
| 233 |
vpc_security_groups: |
| 234 |
description: A list of the DB cluster's security groups and their status. |
| 235 |
type: complex |
| 236 |
contains: |
| 237 |
status: |
| 238 |
description: Status of the security group. |
| 239 |
type: str |
| 240 |
sample: "active" |
| 241 |
vpc_security_group_id: |
| 242 |
description: Security group of the cluster. |
| 243 |
type: str |
| 244 |
sample: "sg-12345678" |
| 245 |
""" |
| 246 |
|
| 247 |
|
| 248 |
try: |
| 249 |
import botocore |
| 250 |
except ImportError: |
| 251 |
pass # handled by AnsibleAWSModule |
| 252 |
|
| 253 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 254 |
|
| 255 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 256 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 257 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import get_tags |
| 258 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 259 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 260 |
|
| 261 |
|
| 262 |
@AWSRetry.jittered_backoff(retries=10) |
| 263 |
def _describe_db_clusters(client, **params): |
| 264 |
try: |
| 265 |
paginator = client.get_paginator("describe_db_clusters") |
| 266 |
return paginator.paginate(**params).build_full_result()["DBClusters"] |
| 267 |
except is_boto3_error_code("DBClusterNotFoundFault"): |
| 268 |
return [] |
| 269 |
|
| 270 |
|
| 271 |
def cluster_info(client, module): |
| 272 |
cluster_id = module.params.get("db_cluster_identifier") |
| 273 |
filters = module.params.get("filters") |
| 274 |
|
| 275 |
params = dict() |
| 276 |
if cluster_id: |
| 277 |
params["DBClusterIdentifier"] = cluster_id |
| 278 |
if filters: |
| 279 |
params["Filters"] = ansible_dict_to_boto3_filter_list(filters) |
| 280 |
|
| 281 |
try: |
| 282 |
result = _describe_db_clusters(client, **params) |
| 283 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 284 |
module.fail_json_aws(e, "Couldn't get RDS cluster information.") |
| 285 |
|
| 286 |
for cluster in result: |
| 287 |
cluster["Tags"] = get_tags(client, module, cluster["DBClusterArn"]) |
| 288 |
|
| 289 |
return dict(changed=False, clusters=[camel_dict_to_snake_dict(cluster, ignore_list=["Tags"]) for cluster in result]) |
| 290 |
|
| 291 |
|
| 292 |
def main(): |
| 293 |
argument_spec = dict( |
| 294 |
db_cluster_identifier=dict(aliases=["cluster_id", "id", "cluster_name"]), |
| 295 |
filters=dict(type="dict"), |
| 296 |
) |
| 297 |
|
| 298 |
module = AnsibleAWSModule( |
| 299 |
argument_spec=argument_spec, |
| 300 |
supports_check_mode=True, |
| 301 |
) |
| 302 |
|
| 303 |
try: |
| 304 |
client = module.client("rds", retry_decorator=AWSRetry.jittered_backoff(retries=10)) |
| 305 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 306 |
module.fail_json_aws(e, msg="Failed to connect to AWS.") |
| 307 |
|
| 308 |
module.exit_json(**cluster_info(client, module)) |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == "__main__": |
| 312 |
main() |