| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2014-2017 Ansible Project |
| 5 |
# Copyright (c) 2017, 2018 Will Thames |
| 6 |
# Copyright (c) 2017, 2018 Michael De La Rue |
| 7 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: rds_snapshot_info |
| 12 |
version_added: 5.0.0 |
| 13 |
short_description: obtain information about one or more RDS snapshots |
| 14 |
description: |
| 15 |
- Obtain information about one or more RDS snapshots. These can be for unclustered snapshots or snapshots of clustered DBs (Aurora). |
| 16 |
- Aurora snapshot information may be obtained if no identifier parameters are passed or if one of the cluster parameters are passed. |
| 17 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 18 |
options: |
| 19 |
db_snapshot_identifier: |
| 20 |
description: |
| 21 |
- Name of an RDS (unclustered) snapshot. |
| 22 |
- Mutually exclusive with O(db_instance_identifier), O(db_cluster_identifier), O(db_cluster_snapshot_identifier). |
| 23 |
required: false |
| 24 |
aliases: |
| 25 |
- snapshot_name |
| 26 |
type: str |
| 27 |
db_instance_identifier: |
| 28 |
description: |
| 29 |
- RDS instance name for which to find snapshots. |
| 30 |
- Mutually exclusive with O(db_snapshot_identifier), O(db_cluster_identifier), O(db_cluster_snapshot_identifier). |
| 31 |
required: false |
| 32 |
type: str |
| 33 |
db_cluster_identifier: |
| 34 |
description: |
| 35 |
- RDS cluster name for which to find snapshots. |
| 36 |
- Mutually exclusive with O(db_snapshot_identifier), O(db_instance_identifier), O(db_cluster_snapshot_identifier). |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
db_cluster_snapshot_identifier: |
| 40 |
description: |
| 41 |
- Name of an RDS cluster snapshot. |
| 42 |
- Mutually exclusive with O(db_instance_identifier), O(db_snapshot_identifier), O(db_cluster_identifier). |
| 43 |
required: false |
| 44 |
type: str |
| 45 |
snapshot_type: |
| 46 |
description: |
| 47 |
- Type of snapshot to find. |
| 48 |
- By default both automated and manual snapshots will be returned. |
| 49 |
required: false |
| 50 |
choices: ['automated', 'manual', 'shared', 'public'] |
| 51 |
type: str |
| 52 |
author: |
| 53 |
- "Will Thames (@willthames)" |
| 54 |
extends_documentation_fragment: |
| 55 |
- amazon.aws.common.modules |
| 56 |
- amazon.aws.region.modules |
| 57 |
- amazon.aws.boto3 |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
- name: Get information about an snapshot |
| 62 |
amazon.aws.rds_snapshot_info: |
| 63 |
db_snapshot_identifier: snapshot_name |
| 64 |
register: new_database_info |
| 65 |
|
| 66 |
- name: Get all RDS snapshots for an RDS instance |
| 67 |
amazon.aws.rds_snapshot_info: |
| 68 |
db_instance_identifier: helloworld-rds-master |
| 69 |
""" |
| 70 |
|
| 71 |
RETURN = r""" |
| 72 |
snapshots: |
| 73 |
description: List of non-clustered snapshots. |
| 74 |
returned: When cluster parameters are not passed. |
| 75 |
type: complex |
| 76 |
contains: |
| 77 |
allocated_storage: |
| 78 |
description: The allocated storage size in gibibytes (GiB). |
| 79 |
returned: always |
| 80 |
type: int |
| 81 |
sample: 10 |
| 82 |
availability_zone: |
| 83 |
description: The name of the Availability Zone the DB instance was located in at the time of the DB snapshot. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: "us-west-2b" |
| 87 |
db_instance_identifier: |
| 88 |
description: The DB instance identifier of the DB instance this DB snapshot was created from. |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
sample: "hello-world-rds" |
| 92 |
db_snapshot_arn: |
| 93 |
description: The Amazon Resource Name (ARN) for the DB snapshot. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: "arn:aws:rds:us-west-2:123456789012:snapshot:rds:hello-world-rds-us1-2018-05-16-04-03" |
| 97 |
db_snapshot_identifier: |
| 98 |
description: The identifier for the DB snapshot. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: "rds:hello-world-rds-us1-2018-05-16-04-03" |
| 102 |
dbi_resource_id: |
| 103 |
description: The identifier for the source DB instance, which can't be changed and which is unique to an Amazon Web Services Region. |
| 104 |
returned: always |
| 105 |
type: str |
| 106 |
sample: "db-ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 107 |
dedicated_log_volume: |
| 108 |
description: Whether the DB instance has a dedicated log volume (DLV) enabled. |
| 109 |
returned: always |
| 110 |
type: bool |
| 111 |
sample: false |
| 112 |
encrypted: |
| 113 |
description: Whether the DB snapshot is encrypted. |
| 114 |
returned: always |
| 115 |
type: bool |
| 116 |
sample: true |
| 117 |
engine: |
| 118 |
description: The name of the database engine. |
| 119 |
returned: always |
| 120 |
type: str |
| 121 |
sample: "postgres" |
| 122 |
engine_version: |
| 123 |
description: The version of the database engine. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: "9.5.10" |
| 127 |
iam_database_authentication_enabled: |
| 128 |
description: Whether mapping of Amazon Web Services Identity and Access Management (IAM) accounts to database accounts is enabled. |
| 129 |
returned: always |
| 130 |
type: bool |
| 131 |
sample: false |
| 132 |
instance_create_time: |
| 133 |
description: The time when the DB instance, from which the snapshot was taken, was created. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: "2017-10-10T04:00:07.434000+00:00" |
| 137 |
iops: |
| 138 |
description: The Provisioned IOPS (I/O operations per second) value of the DB instance at the time of the snapshot. |
| 139 |
returned: always |
| 140 |
type: int |
| 141 |
sample: 3000 |
| 142 |
kms_key_id: |
| 143 |
description: The Amazon Web Services KMS key identifier for the encrypted DB snapshot. |
| 144 |
returned: when encrypted is true |
| 145 |
type: str |
| 146 |
sample: "arn:aws:kms:us-west-2:123456789012:key/abcd1234-1234-aaaa-0000-1234567890ab" |
| 147 |
license_model: |
| 148 |
description: License model information for the restored DB instance. |
| 149 |
returned: always |
| 150 |
type: str |
| 151 |
sample: "postgresql-license" |
| 152 |
master_username: |
| 153 |
description: The master username for the DB snapshot. |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: "dbadmin" |
| 157 |
multi_tenant: |
| 158 |
description: Whether the snapshot is of a DB instance using the multi-tenant configuration (TRUE) or the single-tenant configuration (FALSE). |
| 159 |
returned: always |
| 160 |
type: bool |
| 161 |
sample: false |
| 162 |
option_group_name: |
| 163 |
description: The option group name for the DB snapshot. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: "default:postgres-9-5" |
| 167 |
original_snapshot_create_time: |
| 168 |
description: The time of the CreateDBSnapshot operation. Doesn't change when the snapshot is copied. |
| 169 |
returned: always |
| 170 |
type: str |
| 171 |
sample: "2017-10-10T04:00:07.434000+00:00" |
| 172 |
percent_progress: |
| 173 |
description: The percentage of the estimated data that has been transferred. |
| 174 |
returned: always |
| 175 |
type: int |
| 176 |
sample: 100 |
| 177 |
port: |
| 178 |
description: The port that the database engine was listening on at the time of the snapshot. |
| 179 |
returned: always |
| 180 |
type: int |
| 181 |
sample: 0 |
| 182 |
snapshot_create_time: |
| 183 |
description: When the snapshot was taken. |
| 184 |
returned: always |
| 185 |
type: str |
| 186 |
sample: "2018-05-16T04:03:33.871000+00:00" |
| 187 |
snapshot_target: |
| 188 |
description: "Where manual snapshots are stored: Amazon Web Services Outposts or the Amazon Web Services Region." |
| 189 |
returned: always |
| 190 |
type: str |
| 191 |
sample: "region" |
| 192 |
snapshot_type: |
| 193 |
description: The type of the DB snapshot. |
| 194 |
returned: always |
| 195 |
type: str |
| 196 |
sample: "automated" |
| 197 |
status: |
| 198 |
description: The status of this DB snapshot. |
| 199 |
returned: always |
| 200 |
type: str |
| 201 |
sample: "available" |
| 202 |
storage_throughput: |
| 203 |
description: The storage throughput for the DB snapshot. |
| 204 |
returned: always |
| 205 |
type: int |
| 206 |
sample: 500 |
| 207 |
storage_type: |
| 208 |
description: The storage type associated with DB snapshot. |
| 209 |
returned: always |
| 210 |
type: str |
| 211 |
sample: "gp2" |
| 212 |
tags: |
| 213 |
description: Tags of the snapshot. |
| 214 |
returned: when snapshot is not shared. |
| 215 |
type: complex |
| 216 |
contains: {} |
| 217 |
vpc_id: |
| 218 |
description: The VPC ID associated with the DB snapshot. |
| 219 |
returned: always |
| 220 |
type: str |
| 221 |
sample: "vpc-abcd1234" |
| 222 |
cluster_snapshots: |
| 223 |
description: List of cluster snapshots. |
| 224 |
returned: When cluster parameters are passed. |
| 225 |
type: complex |
| 226 |
contains: |
| 227 |
allocated_storage: |
| 228 |
description: The allocated storage size in gibibytes (GiB). |
| 229 |
returned: always |
| 230 |
type: int |
| 231 |
sample: 1 |
| 232 |
availability_zones: |
| 233 |
description: The list of Availability Zones (AZs) where instances in the DB cluster snapshot can be restored. |
| 234 |
returned: always |
| 235 |
type: list |
| 236 |
sample: |
| 237 |
- "ca-central-1a" |
| 238 |
- "ca-central-1b" |
| 239 |
cluster_create_time: |
| 240 |
description: The time when the DB cluster was created. |
| 241 |
returned: always |
| 242 |
type: str |
| 243 |
sample: "2018-05-17T00:13:40.223000+00:00" |
| 244 |
db_cluster_identifier: |
| 245 |
description: The DB cluster identifier of the DB cluster that this DB cluster snapshot was created from. |
| 246 |
returned: always |
| 247 |
type: str |
| 248 |
sample: "test-aurora-cluster" |
| 249 |
db_cluster_resource_id: |
| 250 |
description: The resource ID of the DB cluster that this DB cluster snapshot was created from. |
| 251 |
returned: always |
| 252 |
type: str |
| 253 |
sample: "cluster-ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 254 |
db_cluster_snapshot_arn: |
| 255 |
description: The Amazon Resource Name (ARN) for the DB cluster snapshot.6 |
| 256 |
returned: always |
| 257 |
type: str |
| 258 |
sample: "arn:aws:rds:ca-central-1:123456789012:cluster-snapshot:test-aurora-snapshot" |
| 259 |
db_cluster_snapshot_identifier: |
| 260 |
description: The identifier for the DB cluster snapshot. |
| 261 |
returned: always |
| 262 |
type: str |
| 263 |
sample: "test-aurora-snapshot" |
| 264 |
engine: |
| 265 |
description: The name of the database engine for this DB cluster snapshot. |
| 266 |
returned: always |
| 267 |
type: str |
| 268 |
sample: "aurora" |
| 269 |
engine_mode: |
| 270 |
description: The engine mode of the database engine for this DB cluster snapshot. |
| 271 |
returned: always |
| 272 |
type: str |
| 273 |
sample: "provisioned" |
| 274 |
engine_version: |
| 275 |
description: The version of the database engine for this DB cluster snapshot. |
| 276 |
returned: always |
| 277 |
type: str |
| 278 |
sample: "5.6.10a" |
| 279 |
iam_database_authentication_enabled: |
| 280 |
description: Whether mapping of Amazon Web Services Identity and Access Management (IAM) accounts to database accounts is enabled. |
| 281 |
returned: always |
| 282 |
type: bool |
| 283 |
sample: false |
| 284 |
kms_key_id: |
| 285 |
description: the Amazon Web Services KMS key identifier for the encrypted DB cluster snapshot. |
| 286 |
returned: when storage_encrypted is true |
| 287 |
type: str |
| 288 |
sample: "arn:aws:kms:us-west-2:123456789012:key/abcd1234-1234-aaaa-0000-1234567890ab" |
| 289 |
license_model: |
| 290 |
description: The license model information for this DB cluster snapshot. |
| 291 |
returned: always |
| 292 |
type: str |
| 293 |
sample: "aurora" |
| 294 |
master_username: |
| 295 |
description: The master username for this DB cluster snapshot. |
| 296 |
returned: always |
| 297 |
type: str |
| 298 |
sample: "shertel" |
| 299 |
percent_progress: |
| 300 |
description: The percentage of the estimated data that has been transferred. |
| 301 |
returned: always |
| 302 |
type: int |
| 303 |
sample: 0 |
| 304 |
port: |
| 305 |
description: The port that the DB cluster was listening on at the time of the snapshot. |
| 306 |
returned: always |
| 307 |
type: int |
| 308 |
sample: 0 |
| 309 |
snapshot_create_time: |
| 310 |
description: The time when the snapshot was taken. |
| 311 |
returned: always |
| 312 |
type: str |
| 313 |
sample: "2018-05-17T00:23:23.731000+00:00" |
| 314 |
snapshot_type: |
| 315 |
description: The type of the DB cluster snapshot. |
| 316 |
returned: always |
| 317 |
type: str |
| 318 |
sample: "manual" |
| 319 |
status: |
| 320 |
description: The status of this DB cluster snapshot. |
| 321 |
returned: always |
| 322 |
type: str |
| 323 |
sample: "creating" |
| 324 |
storage_encrypted: |
| 325 |
description: Indicates whether the DB cluster snapshot is encrypted. |
| 326 |
returned: always |
| 327 |
type: bool |
| 328 |
sample: true |
| 329 |
tags: |
| 330 |
description: Tags of the snapshot. |
| 331 |
returned: when snapshot is not shared. |
| 332 |
type: complex |
| 333 |
contains: {} |
| 334 |
vpc_id: |
| 335 |
description: The VPC ID associated with the DB cluster snapshot. |
| 336 |
returned: always |
| 337 |
type: str |
| 338 |
sample: "vpc-abcd1234" |
| 339 |
""" |
| 340 |
|
| 341 |
from typing import Any |
| 342 |
from typing import Callable |
| 343 |
from typing import Dict |
| 344 |
from typing import List |
| 345 |
|
| 346 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 347 |
|
| 348 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 349 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import AnsibleRDSError |
| 350 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import describe_db_cluster_snapshots |
| 351 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import describe_db_snapshots |
| 352 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 353 |
|
| 354 |
|
| 355 |
def common_snapshot_info( |
| 356 |
client, module: AnsibleAWSModule, describe_snapshots_method: Callable, params: Dict[str, Any] |
| 357 |
) -> List[Dict[str, Any]]: |
| 358 |
try: |
| 359 |
results = describe_snapshots_method(client, **params) |
| 360 |
except AnsibleRDSError as e: |
| 361 |
module.fail_json_aws(e, "Failed to get snapshot information") |
| 362 |
|
| 363 |
for snapshot in results: |
| 364 |
if snapshot["SnapshotType"] != "shared": |
| 365 |
snapshot["Tags"] = boto3_tag_list_to_ansible_dict(snapshot.pop("Tags", [])) |
| 366 |
|
| 367 |
return [camel_dict_to_snake_dict(snapshot, ignore_list=["Tags"]) for snapshot in results] |
| 368 |
|
| 369 |
|
| 370 |
def cluster_snapshot_info(client, module: AnsibleAWSModule) -> List[Dict[str, Any]]: |
| 371 |
snapshot_id = module.params.get("db_cluster_snapshot_identifier") |
| 372 |
snapshot_type = module.params.get("snapshot_type") |
| 373 |
instance_name = module.params.get("db_cluster_identifier") |
| 374 |
|
| 375 |
params = dict() |
| 376 |
if snapshot_id: |
| 377 |
params["DBClusterSnapshotIdentifier"] = snapshot_id |
| 378 |
if instance_name: |
| 379 |
params["DBClusterIdentifier"] = instance_name |
| 380 |
if snapshot_type: |
| 381 |
params["SnapshotType"] = snapshot_type |
| 382 |
if snapshot_type == "public": |
| 383 |
params["IncludePublic"] = True |
| 384 |
elif snapshot_type == "shared": |
| 385 |
params["IncludeShared"] = True |
| 386 |
|
| 387 |
return common_snapshot_info(client, module, describe_db_cluster_snapshots, params) |
| 388 |
|
| 389 |
|
| 390 |
def instance_snapshot_info(client, module: AnsibleAWSModule) -> List[Dict[str, Any]]: |
| 391 |
snapshot_id = module.params.get("db_snapshot_identifier") |
| 392 |
snapshot_type = module.params.get("snapshot_type") |
| 393 |
instance_name = module.params.get("db_instance_identifier") |
| 394 |
|
| 395 |
params = dict() |
| 396 |
if snapshot_id: |
| 397 |
params["DBSnapshotIdentifier"] = snapshot_id |
| 398 |
if instance_name: |
| 399 |
params["DBInstanceIdentifier"] = instance_name |
| 400 |
if snapshot_type: |
| 401 |
params["SnapshotType"] = snapshot_type |
| 402 |
if snapshot_type == "public": |
| 403 |
params["IncludePublic"] = True |
| 404 |
elif snapshot_type == "shared": |
| 405 |
params["IncludeShared"] = True |
| 406 |
|
| 407 |
return common_snapshot_info(client, module, describe_db_snapshots, params) |
| 408 |
|
| 409 |
|
| 410 |
def main(): |
| 411 |
argument_spec = dict( |
| 412 |
db_snapshot_identifier=dict(aliases=["snapshot_name"]), |
| 413 |
db_instance_identifier=dict(), |
| 414 |
db_cluster_identifier=dict(), |
| 415 |
db_cluster_snapshot_identifier=dict(), |
| 416 |
snapshot_type=dict(choices=["automated", "manual", "shared", "public"]), |
| 417 |
) |
| 418 |
|
| 419 |
module = AnsibleAWSModule( |
| 420 |
argument_spec=argument_spec, |
| 421 |
supports_check_mode=True, |
| 422 |
mutually_exclusive=[ |
| 423 |
[ |
| 424 |
"db_snapshot_identifier", |
| 425 |
"db_instance_identifier", |
| 426 |
"db_cluster_identifier", |
| 427 |
"db_cluster_snapshot_identifier", |
| 428 |
] |
| 429 |
], |
| 430 |
) |
| 431 |
|
| 432 |
client = module.client("rds") |
| 433 |
results = dict() |
| 434 |
if not module.params["db_cluster_identifier"] and not module.params["db_cluster_snapshot_identifier"]: |
| 435 |
results["snapshots"] = instance_snapshot_info(client, module) |
| 436 |
if not module.params["db_snapshot_identifier"] and not module.params["db_instance_identifier"]: |
| 437 |
results["cluster_snapshots"] = cluster_snapshot_info(client, module) |
| 438 |
|
| 439 |
module.exit_json(changed=False, **results) |
| 440 |
|
| 441 |
|
| 442 |
if __name__ == "__main__": |
| 443 |
main() |