summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/aws/plugins/module_utils/dynamodb.py
blob: d48029c1b89f2f3834ea773ceefa2b90973490f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-

# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

try:
    import botocore
except ImportError:
    pass  # Handled by AnsibleAWSModule

from ansible_collections.community.aws.plugins.module_utils.base import BaseWaiterFactory


class DynamodbWaiterFactory(BaseWaiterFactory):
    def __init__(self, module):
        # the AWSRetry wrapper doesn't support the wait functions (there's no
        # public call we can cleanly wrap)
        client = module.client("dynamodb")
        super().__init__(module, client)

    @property
    def _waiter_model_data(self):
        data = super()._waiter_model_data
        ddb_data = dict(
            table_exists=dict(
                operation="DescribeTable",
                delay=20,
                maxAttempts=25,
                acceptors=[
                    dict(expected="ACTIVE", matcher="path", state="success", argument="Table.TableStatus"),
                    dict(expected="ResourceNotFoundException", matcher="error", state="retry"),
                ],
            ),
            table_not_exists=dict(
                operation="DescribeTable",
                delay=20,
                maxAttempts=25,
                acceptors=[
                    dict(expected="ResourceNotFoundException", matcher="error", state="success"),
                ],
            ),
            global_indexes_active=dict(
                operation="DescribeTable",
                delay=20,
                maxAttempts=25,
                acceptors=[
                    dict(expected="ResourceNotFoundException", matcher="error", state="failure"),
                    # If there are no secondary indexes, simply return
                    dict(
                        expected=False,
                        matcher="path",
                        state="success",
                        argument="contains(keys(Table), `GlobalSecondaryIndexes`)",
                    ),
                    dict(
                        expected="ACTIVE",
                        matcher="pathAll",
                        state="success",
                        argument="Table.GlobalSecondaryIndexes[].IndexStatus",
                    ),
                    dict(
                        expected="CREATING",
                        matcher="pathAny",
                        state="retry",
                        argument="Table.GlobalSecondaryIndexes[].IndexStatus",
                    ),
                    dict(
                        expected="UPDATING",
                        matcher="pathAny",
                        state="retry",
                        argument="Table.GlobalSecondaryIndexes[].IndexStatus",
                    ),
                    dict(
                        expected="DELETING",
                        matcher="pathAny",
                        state="retry",
                        argument="Table.GlobalSecondaryIndexes[].IndexStatus",
                    ),
                    dict(
                        expected=True,
                        matcher="path",
                        state="success",
                        argument="length(Table.GlobalSecondaryIndexes) == `0`",
                    ),
                ],
            ),
        )
        data.update(ddb_data)
        return data


def _do_wait(module, waiter_name, action_description, wait_timeout, table_name):
    delay = min(wait_timeout, 5)
    max_attempts = wait_timeout // delay

    try:
        waiter = DynamodbWaiterFactory(module).get_waiter(waiter_name)
        waiter.wait(
            WaiterConfig={"Delay": delay, "MaxAttempts": max_attempts},
            TableName=table_name,
        )
    except botocore.exceptions.WaiterError as e:
        module.fail_json_aws(e, msg=f"Timeout while waiting for {action_description}")
    except (
        botocore.exceptions.ClientError,
        botocore.exceptions.BotoCoreError,
    ) as e:  # pylint: disable=duplicate-except
        module.fail_json_aws(e, msg=f"Failed while waiting for {action_description}")


def wait_table_exists(module, wait_timeout, table_name):
    _do_wait(module, "table_exists", "table creation", wait_timeout, table_name)


def wait_table_not_exists(module, wait_timeout, table_name):
    _do_wait(module, "table_not_exists", "table deletion", wait_timeout, table_name)


def wait_indexes_active(module, wait_timeout, table_name):
    _do_wait(module, "global_indexes_active", "secondary index updates", wait_timeout, table_name)