Debian Codegraph @

ansible_collections/community/aws/plugins/module_utils/dynamodb.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright: Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 try:
7 import botocore
8 except ImportError:
9 pass # Handled by AnsibleAWSModule
10
11 from ansible_collections.community.aws.plugins.module_utils.base import BaseWaiterFactory
12
13
14 class DynamodbWaiterFactory(BaseWaiterFactory):
15 def __init__(self, module):
16 # the AWSRetry wrapper doesn't support the wait functions (there's no
17 # public call we can cleanly wrap)
18 client = module.client("dynamodb")
19 super().__init__(module, client)
20
21 @property
22 def _waiter_model_data(self):
23 data = super()._waiter_model_data
24 ddb_data = dict(
25 table_exists=dict(
26 operation="DescribeTable",
27 delay=20,
28 maxAttempts=25,
29 acceptors=[
30 dict(expected="ACTIVE", matcher="path", state="success", argument="Table.TableStatus"),
31 dict(expected="ResourceNotFoundException", matcher="error", state="retry"),
32 ],
33 ),
34 table_not_exists=dict(
35 operation="DescribeTable",
36 delay=20,
37 maxAttempts=25,
38 acceptors=[
39 dict(expected="ResourceNotFoundException", matcher="error", state="success"),
40 ],
41 ),
42 global_indexes_active=dict(
43 operation="DescribeTable",
44 delay=20,
45 maxAttempts=25,
46 acceptors=[
47 dict(expected="ResourceNotFoundException", matcher="error", state="failure"),
48 # If there are no secondary indexes, simply return
49 dict(
50 expected=False,
51 matcher="path",
52 state="success",
53 argument="contains(keys(Table), `GlobalSecondaryIndexes`)",
54 ),
55 dict(
56 expected="ACTIVE",
57 matcher="pathAll",
58 state="success",
59 argument="Table.GlobalSecondaryIndexes[].IndexStatus",
60 ),
61 dict(
62 expected="CREATING",
63 matcher="pathAny",
64 state="retry",
65 argument="Table.GlobalSecondaryIndexes[].IndexStatus",
66 ),
67 dict(
68 expected="UPDATING",
69 matcher="pathAny",
70 state="retry",
71 argument="Table.GlobalSecondaryIndexes[].IndexStatus",
72 ),
73 dict(
74 expected="DELETING",
75 matcher="pathAny",
76 state="retry",
77 argument="Table.GlobalSecondaryIndexes[].IndexStatus",
78 ),
79 dict(
80 expected=True,
81 matcher="path",
82 state="success",
83 argument="length(Table.GlobalSecondaryIndexes) == `0`",
84 ),
85 ],
86 ),
87 )
88 data.update(ddb_data)
89 return data
90
91
92 def _do_wait(module, waiter_name, action_description, wait_timeout, table_name):
93 delay = min(wait_timeout, 5)
94 max_attempts = wait_timeout // delay
95
96 try:
97 waiter = DynamodbWaiterFactory(module).get_waiter(waiter_name)
98 waiter.wait(
99 WaiterConfig={"Delay": delay, "MaxAttempts": max_attempts},
100 TableName=table_name,
101 )
102 except botocore.exceptions.WaiterError as e:
103 module.fail_json_aws(e, msg=f"Timeout while waiting for {action_description}")
104 except (
105 botocore.exceptions.ClientError,
106 botocore.exceptions.BotoCoreError,
107 ) as e: # pylint: disable=duplicate-except
108 module.fail_json_aws(e, msg=f"Failed while waiting for {action_description}")
109
110
111 def wait_table_exists(module, wait_timeout, table_name):
112 _do_wait(module, "table_exists", "table creation", wait_timeout, table_name)
113
114
115 def wait_table_not_exists(module, wait_timeout, table_name):
116 _do_wait(module, "table_not_exists", "table deletion", wait_timeout, table_name)
117
118
119 def wait_indexes_active(module, wait_timeout, table_name):
120 _do_wait(module, "global_indexes_active", "secondary index updates", wait_timeout, table_name)