| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: dynamodb_table_info |
| 10 |
version_added: 7.2.0 |
| 11 |
short_description: Returns information about a Dynamo DB table |
| 12 |
description: |
| 13 |
- Returns information about the Dynamo DB table, including the current status of the table, |
| 14 |
when it was created, the primary key schema, and any indexes on the table. |
| 15 |
author: |
| 16 |
- Aubin Bikouo (@abikouo) |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- The name of the table to describe. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
extends_documentation_fragment: |
| 24 |
- amazon.aws.common.modules |
| 25 |
- amazon.aws.region.modules |
| 26 |
- amazon.aws.boto3 |
| 27 |
""" |
| 28 |
|
| 29 |
EXAMPLES = r""" |
| 30 |
- name: Return information about the DynamoDB table named 'my-table' |
| 31 |
community.aws.dynamodb_table_info: |
| 32 |
name: my-table |
| 33 |
""" |
| 34 |
|
| 35 |
RETURN = r""" |
| 36 |
table: |
| 37 |
description: The returned table params from the describe API call. |
| 38 |
returned: success |
| 39 |
type: complex |
| 40 |
contains: |
| 41 |
table_name: |
| 42 |
description: The name of the table. |
| 43 |
returned: always |
| 44 |
type: str |
| 45 |
table_status: |
| 46 |
description: The current state of the table. |
| 47 |
returned: always |
| 48 |
type: str |
| 49 |
sample: 'ACTIVE' |
| 50 |
creation_date_time: |
| 51 |
description: The date and time when the table was created, in UNIX epoch time format. |
| 52 |
returned: always |
| 53 |
type: str |
| 54 |
table_size_bytes: |
| 55 |
description: The total size of the specified table, in bytes. |
| 56 |
returned: always |
| 57 |
type: int |
| 58 |
item_count: |
| 59 |
description: The number of items in the specified table. |
| 60 |
returned: always |
| 61 |
type: int |
| 62 |
table_arn: |
| 63 |
description: The Amazon Resource Name (ARN) that uniquely identifies the table. |
| 64 |
returned: always |
| 65 |
type: str |
| 66 |
table_id: |
| 67 |
description: Unique identifier for the table for which the backup was created. |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
attribute_definitions: |
| 71 |
description: A list of attributes for describing the key schema for the table and indexes. |
| 72 |
returned: always |
| 73 |
type: complex |
| 74 |
contains: |
| 75 |
attribute_name: |
| 76 |
description: A name for the attribute. |
| 77 |
type: str |
| 78 |
returned: always |
| 79 |
attribute_type: |
| 80 |
description: The data type for the attribute, S (String), N (Number) and B (Binary). |
| 81 |
type: str |
| 82 |
returned: always |
| 83 |
key_schema: |
| 84 |
description: A list of key schemas that specify the attributes that make up the primary key of a table, or the key attributes of an index. |
| 85 |
returned: always |
| 86 |
type: complex |
| 87 |
contains: |
| 88 |
attribute_name: |
| 89 |
description: The name of a key attribute. |
| 90 |
type: str |
| 91 |
returned: always |
| 92 |
key_type: |
| 93 |
description: The role that this key attribute will assume, 'HASH' for partition key, 'RANGE' for sort key |
| 94 |
type: str |
| 95 |
returned: always |
| 96 |
billing_mode: |
| 97 |
description: Controls how you are charged for read and write throughput and how you manage capacity. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
local_secondary_indexes: |
| 101 |
description: Represents one or more local secondary indexes on the table. |
| 102 |
returned: if any, on the table |
| 103 |
type: list |
| 104 |
elements: dict |
| 105 |
global_secondary_indexes: |
| 106 |
description: The global secondary indexes of table. |
| 107 |
returned: if any, on the table |
| 108 |
type: list |
| 109 |
elements: dict |
| 110 |
stream_specification: |
| 111 |
description: The current DynamoDB Streams configuration for the table. |
| 112 |
returned: if any, on the table |
| 113 |
type: complex |
| 114 |
contains: |
| 115 |
stream_enabled: |
| 116 |
description: Indicates whether DynamoDB Streams is enabled (true) or disabled (false) on the table. |
| 117 |
type: bool |
| 118 |
returned: always |
| 119 |
sample: true |
| 120 |
stream_view_type: |
| 121 |
description: When an item in the table is modified, stream_view_type determines what information is written to the stream for this table. |
| 122 |
type: str |
| 123 |
returned: always |
| 124 |
sample: KEYS_ONLY |
| 125 |
latest_stream_label: |
| 126 |
description: A timestamp, in ISO 8601 format, for this stream. |
| 127 |
type: str |
| 128 |
returned: if any on the table |
| 129 |
latest_stream_arn: |
| 130 |
description: The Amazon Resource Name (ARN) that uniquely identifies the latest stream for this table. |
| 131 |
returned: if any on the table |
| 132 |
type: str |
| 133 |
global_table_version: |
| 134 |
description: Represents the version of global tables in use, if the table is replicated across AWS Regions. |
| 135 |
type: str |
| 136 |
returned: if the table is replicated |
| 137 |
replicas: |
| 138 |
description: Represents replicas of the table. |
| 139 |
type: list |
| 140 |
elements: dict |
| 141 |
returned: if any on the table |
| 142 |
source_backup_arn: |
| 143 |
description: The Amazon Resource Name (ARN) of the backup from which the table was restored. |
| 144 |
type: str |
| 145 |
returned: if any, on the table |
| 146 |
source_table_arn: |
| 147 |
description: The ARN of the source table of the backup that is being restored. |
| 148 |
type: str |
| 149 |
returned: if any, on the table |
| 150 |
restore_date_time: |
| 151 |
description: Point in time or source backup time. |
| 152 |
type: str |
| 153 |
returned: if any, on table |
| 154 |
restore_in_progress: |
| 155 |
description: Indicates if a restore is in progress or not. |
| 156 |
type: bool |
| 157 |
returned: if any, on table |
| 158 |
sse_description: |
| 159 |
description: The description of the server-side encryption status on the specified table. |
| 160 |
type: dict |
| 161 |
returned: if any, on table |
| 162 |
sample: {} |
| 163 |
archival_summary: |
| 164 |
description: Contains information about the table archive. |
| 165 |
type: complex |
| 166 |
returned: if any, on table |
| 167 |
contains: |
| 168 |
archival_date_time: |
| 169 |
description: The date and time when table archival was initiated by DynamoDB, in UNIX epoch time format. |
| 170 |
type: str |
| 171 |
returned: always |
| 172 |
archival_reason: |
| 173 |
description: The reason DynamoDB archived the table. |
| 174 |
type: str |
| 175 |
returned: always |
| 176 |
sample: INACCESSIBLE_ENCRYPTION_CREDENTIALS |
| 177 |
archival_backup_arn: |
| 178 |
description: The Amazon Resource Name (ARN) of the backup the table was archived to, when applicable in the archival reason. |
| 179 |
type: str |
| 180 |
returned: always |
| 181 |
table_class: |
| 182 |
description: The table class of the specified table. |
| 183 |
type: str |
| 184 |
returned: if any on the table |
| 185 |
sample: STANDARD_INFREQUENT_ACCESS |
| 186 |
deletion_protection_enabled: |
| 187 |
description: Indicates whether deletion protection is enabled (true) or disabled (false) on the table. |
| 188 |
type: bool |
| 189 |
returned: always |
| 190 |
sample: true |
| 191 |
provisioned_throughput: |
| 192 |
description: The provisioned throughput settings for the table. |
| 193 |
type: dict |
| 194 |
returned: always |
| 195 |
sample: '{"number_of_decreases_today": 0, "read_capacity_units": 1, "write_capacity_units": 1}' |
| 196 |
tags: |
| 197 |
description: A dict of tags associated with the DynamoDB table. |
| 198 |
returned: always |
| 199 |
type: dict |
| 200 |
""" |
| 201 |
|
| 202 |
try: |
| 203 |
import botocore |
| 204 |
except ImportError: |
| 205 |
pass # Handled by AnsibleAWSModule |
| 206 |
|
| 207 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 208 |
|
| 209 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 210 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 211 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 212 |
|
| 213 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 214 |
|
| 215 |
|
| 216 |
# ResourceNotFoundException is expected here if the table doesn't exist |
| 217 |
@AWSRetry.jittered_backoff(catch_extra_error_codes=["LimitExceededException", "ResourceInUseException"]) |
| 218 |
def _describe_table(client, **params): |
| 219 |
return client.describe_table(**params) |
| 220 |
|
| 221 |
|
| 222 |
def describe_dynamodb_table(module): |
| 223 |
table_name = module.params.get("name") |
| 224 |
retry_decorator = AWSRetry.jittered_backoff( |
| 225 |
catch_extra_error_codes=["LimitExceededException", "ResourceInUseException", "ResourceNotFoundException"], |
| 226 |
) |
| 227 |
client = module.client("dynamodb", retry_decorator=retry_decorator) |
| 228 |
try: |
| 229 |
table = _describe_table(client, TableName=table_name) |
| 230 |
except is_boto3_error_code("ResourceNotFoundException"): |
| 231 |
module.exit_json(table={}) |
| 232 |
except ( |
| 233 |
botocore.exceptions.ClientError, |
| 234 |
botocore.exceptions.BotoCoreError, |
| 235 |
) as e: # pylint: disable=duplicate-except |
| 236 |
module.fail_json_aws(e, msg="Failed to describe table") |
| 237 |
|
| 238 |
table = table["Table"] |
| 239 |
try: |
| 240 |
tags = client.list_tags_of_resource(aws_retry=True, ResourceArn=table["TableArn"])["Tags"] |
| 241 |
except is_boto3_error_code("AccessDeniedException"): |
| 242 |
module.warn("Permission denied when listing tags") |
| 243 |
tags = [] |
| 244 |
except ( |
| 245 |
botocore.exceptions.ClientError, |
| 246 |
botocore.exceptions.BotoCoreError, |
| 247 |
) as e: # pylint: disable=duplicate-except |
| 248 |
module.fail_json_aws(e, msg="Failed to list table tags") |
| 249 |
|
| 250 |
table = camel_dict_to_snake_dict(table) |
| 251 |
table["tags"] = boto3_tag_list_to_ansible_dict(tags) |
| 252 |
|
| 253 |
if "table_class_summary" in table: |
| 254 |
table["table_class"] = table["table_class_summary"]["table_class"] |
| 255 |
del table["table_class_summary"] |
| 256 |
|
| 257 |
# billing_mode_summary doesn't always seem to be set but is always set for PAY_PER_REQUEST |
| 258 |
# and when updating the billing_mode |
| 259 |
if "billing_mode_summary" in table: |
| 260 |
table["billing_mode"] = table["billing_mode_summary"]["billing_mode"] |
| 261 |
del table["billing_mode_summary"] |
| 262 |
else: |
| 263 |
table["billing_mode"] = "PROVISIONED" |
| 264 |
|
| 265 |
# Restore summary |
| 266 |
if "restore_summary" in table: |
| 267 |
table["source_backup_arn"] = table["restore_summary"].get("source_backup_arn", "") |
| 268 |
table["source_table_arn"] = table["restore_summary"].get("source_table_arn", "") |
| 269 |
table["restore_date_time"] = table["restore_summary"].get("restore_date_time", "") |
| 270 |
table["restore_in_progress"] = table["restore_summary"].get("restore_in_progress") |
| 271 |
del table["restore_summary"] |
| 272 |
|
| 273 |
module.exit_json(table=table) |
| 274 |
|
| 275 |
|
| 276 |
def main(): |
| 277 |
argument_spec = dict( |
| 278 |
name=dict( |
| 279 |
required=True, |
| 280 |
), |
| 281 |
) |
| 282 |
|
| 283 |
module = AnsibleAWSModule( |
| 284 |
argument_spec=argument_spec, |
| 285 |
supports_check_mode=True, |
| 286 |
) |
| 287 |
|
| 288 |
describe_dynamodb_table(module) |
| 289 |
|
| 290 |
|
| 291 |
if __name__ == "__main__": |
| 292 |
main() |