1"""
2Botocore waiters for elasticsearch that are not present in boto3+botocore (yet).
3
4:codeauthor: Herbert Buurman <herbert.buurman@ogd.nl>
5:depends: boto3
6"""
7
8import salt.utils.versions
9from salt.exceptions import SaltInvocationError
10
11try:
12    import botocore.waiter
13except ImportError:
14    pass
15
16
17WAITER_CONFIGS = {
18    "ESDomainAvailable": {
19        "delay": 60,
20        "operation": "DescribeElasticsearchDomainConfig",
21        "maxAttempts": 60,
22        "acceptors": [
23            {
24                "expected": "Active",
25                "matcher": "path",
26                "state": "success",
27                "argument": "DomainConfig.ElasticsearchClusterConfig.Status.State",
28            },
29            {
30                "expected": True,
31                "matcher": "pathAny",
32                "state": "failure",
33                "argument": "DomainConfig.*.Status.PendingDeletion",
34            },
35        ],
36    },
37    "ESUpgradeFinished": {
38        "delay": 60,
39        "operation": "DescribeElasticsearchDomain",
40        "maxAttempts": 60,
41        "acceptors": [
42            {
43                "expected": False,
44                "matcher": "path",
45                "state": "success",
46                "argument": "DomainStatus.UpgradeProcessing",
47            }
48        ],
49    },
50    "ESDomainDeleted": {
51        "delay": 30,
52        "operation": "DescribeElasticsearchDomain",
53        "maxAttempts": 60,
54        "acceptors": [
55            {
56                "expected": True,
57                "matcher": "path",
58                "state": "retry",
59                "argument": "DomainStatus.Deleted",
60            },
61            {
62                "expected": False,
63                "matcher": "path",
64                "state": "failure",
65                "argument": "DomainStatus.Processing",
66            },
67            {
68                "expected": "ResourceNotFoundException",
69                "matcher": "error",
70                "state": "success",
71            },
72        ],
73    },
74    "ESDomainCreated": {
75        "delay": 30,
76        "operation": "DescribeElasticsearchDomain",
77        "maxAttempts": 60,
78        "acceptors": [
79            {
80                "expected": True,
81                "matcher": "path",
82                "state": "success",
83                "argument": "DomainStatus.Created",
84            }
85        ],
86    },
87}
88
89
90def __virtual__():
91    """
92    Only load if botocore libraries exist.
93    """
94    return salt.utils.versions.check_boto_reqs(check_boto=False)
95
96
97@salt.utils.decorators.is_deprecated(globals(), "Sulfur")
98def get_waiter(client, waiter=None, waiter_config=None):
99    """
100    Gets a botocore waiter using either one of the preconfigured models by name
101    ``waiter``, or with a manually supplied ``waiter_config``.
102
103    :param botoclient client: The botocore client to use.
104    :param str waiter: The name of the waiter config to use.
105        Either ``waiter`` or ``waiter_config`` must be supplied.
106        If both ``waiter`` and ``waiter_config`` are supplied, ``waiter`` takes
107        presedence, unless no configuration for ``waiter`` exists.
108    :param dict waiter_config: The manual waiter config to use.
109        Either waiter or waiter_config must be supplied.
110
111    :returns botocore.waiter
112    """
113    if not any((waiter, waiter_config)):
114        raise SaltInvocationError(
115            "At least one of waiter or waiter_config must be specified."
116        )
117    waiter_model = botocore.waiter.WaiterModel(
118        {"version": 2, "waiters": {waiter: WAITER_CONFIGS.get(waiter, waiter_config)}}
119    )
120    return botocore.waiter.create_waiter_with_client(waiter, waiter_model, client)
121
122
123@salt.utils.decorators.is_deprecated(globals(), "Sulfur")
124def list_waiters():
125    """
126    Lists the builtin waiter configuration names.
127
128    :returns list
129    """
130    return WAITER_CONFIGS.keys()
131