forked from cloudera-labs/cloudera.cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdw_cluster.py
369 lines (342 loc) · 12.8 KB
/
dw_cluster.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2021 Cloudera, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_common import CdpModule
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = r'''
---
module: dw_cluster
short_description: Create or Delete CDP Data Warehouse Clusters
description:
- Create or Delete CDP Data Warehouse Clusters
author:
- "Dan Chaffelson (@chaffelson)"
- "Saravanan Raju (@raju-saravanan)"
- "Webster Mudge (@wmudge)"
requirements:
- cdpy
options:
cluster_id:
description:
- The identifier of the Data Warehouse Cluster.
- Required if I(state=absent) and I(env) is not specified.
type: str
aliases:
- id
- name
env:
description:
- The name of the target environment.
- Required if I(state=present).
- Required if I(state=absent) and I(cluster_id) is not specified.
type: str
aliases:
- environment
- env_crn
overlay:
description:
- Flag to use private IP addresses for Pods within the cluster.
- Otherwise, use IP addresses within the VPC.
type: bool
default: False
private_load_balancer:
description: Flag to set up a load balancer for private subnets.
type: bool
default: False
aws_public_subnets:
description:
- List of zero or more Public AWS Subnet IDs used for deployment.
- Required if I(state=present) and the I(env) is deployed to AWS.
type: list
elements: str
aws_private_subnets:
description:
- List of zero or more Private AWS Subnet IDs used for deployment.
- Required if I(state=present) and the I(env) is deployed to AWS.
type: list
elements: str
az_subnet:
description:
- The Azure Subnet Name.
- Required if I(state=present) and the I(env) is deployed to Azure.
type: str
az_enable_az:
description:
- Flag to enable Availability Zone mode.
- Required if I(state=present) and the I(env) is deployed to Azure.
type: bool
state:
description: The state of the Data Warehouse Cluster
type: str
default: present
choices:
- present
- absent
wait:
description:
- Flag to enable internal polling to wait for the Data Warehouse Cluster to achieve the declared state.
- If set to FALSE, the module will return immediately.
type: bool
default: True
force:
description:
- Flag to enable force deletion of the Data Warehouse Cluster.
- This will not destroy the underlying cloud provider assets.
type: bool
default: False
delay:
description:
- The internal polling interval (in seconds) while the module waits for the Data Warehouse Cluster to achieve the declared
state.
type: int
default: 15
aliases:
- polling_delay
timeout:
description:
- The internal polling timeout (in seconds) while the module waits for the Data Warehouse Cluster to achieve the declared
state.
type: int
default: 3600
aliases:
- polling_timeout
extends_documentation_fragment:
- cloudera.cloud.cdp_sdk_options
- cloudera.cloud.cdp_auth_options
'''
EXAMPLES = r'''
# Note: These examples do not set authentication details.
# Request Azure Cluster creation
- cloudera.cloud.dw_cluster:
env_crn: crn:cdp:environments...
az_subnet: my-subnet-name
az_enable_az: yes
# Request AWS Cluster Creation
- cloudera.cloud.dw_cluster:
env_crn: crn:cdp:environments...
aws_public_subnets: [subnet-id-1, subnet-id-2]
aws_private_subnets: [subnet-id-3, subnet-id-4]
# Delete a Data Warehouse Cluster
- cloudera.cloud.dw_cluster:
state: absent
cluster_id: my-id
# Delete the Data Warehouse Cluster within the Environment
- cloudera.cloud.dw_cluster:
state: absent
env: crn:cdp:environments...
'''
RETURN = r'''
---
cluster:
description: Details for the Data Warehouse cluster
type: dict
contains:
id:
description: The cluster identifier.
returned: always
type: str
environmentCrn:
description: The CRN of the cluster's Environment
returned: always
type: str
crn:
description: The cluster's CRN.
returned: always
type: str
creationDate:
description: The creation timestamp of the cluster in UTC.
returned: always
type: str
status:
description: The status of the cluster
returned: always
type: str
creator:
description: The cluster creator details.
returned: always
type: dict
contains:
crn:
description: The Actor CRN.
type: str
returned: always
email:
description: Email address (users).
type: str
returned: when supported
workloadUsername:
description: Username (users).
type: str
returned: when supported
machineUsername:
description: Username (machine users).
type: str
returned: when supported
cloudPlatform:
description: The cloud platform of the environment that was used to create this cluster.
returned: always
type: str
sdk_out:
description: Returns the captured CDP SDK log.
returned: when supported
type: str
sdk_out_lines:
description: Returns a list of each line of the captured CDP SDK log.
returned: when supported
type: list
elements: str
'''
class DwCluster(CdpModule):
def __init__(self, module):
super(DwCluster, self).__init__(module)
# Set variables
self.name = self._get_param('name')
self.env = self._get_param('env')
self.overlay = self._get_param('overlay')
self.private_load_balancer = self._get_param('private_load_balancer')
self.az_subnet = self._get_param('az_subnet')
self.az_enable_az = self._get_param('az_enable_az')
self.aws_public_subnets = self._get_param('aws_public_subnets')
self.aws_private_subnets = self._get_param('aws_private_subnets')
self.force = self._get_param('force')
self.state = self._get_param('state')
self.wait = self._get_param('wait')
self.delay = self._get_param('delay')
self.timeout = self._get_param('timeout')
# Initialize return values
self.cluster = {}
self.changed = False
# Initialize internal values
self.target = None
# Execute logic process
self.process()
@CdpModule._Decorators.process_debug
def process(self):
env_crn = self.cdpy.environments.resolve_environment_crn(self.env)
# Check if Cluster exists
if self.name is not None:
self.target = self.cdpy.dw.describe_cluster(cluster_id=self.name)
elif env_crn is not None:
listing = self.cdpy.dw.list_clusters(env_crn) # Always returns a list
if len(listing) == 1:
self.name = listing[0]['id']
self.target = self.cdpy.dw.describe_cluster(cluster_id=self.name)
elif len(listing) == 0:
self.target = None
else:
self.module.fail_json(msg="Received multiple (i.e. ambiguous) Clusters in Environment %s" % self.env)
else:
self.target = None
if self.target is not None:
# Begin Cluster Exists
if self.state == 'absent':
# Begin Delete
if self.module.check_mode:
self.cluster = self.target
else:
self.changed = True
if self.target['status'] not in self.cdpy.sdk.REMOVABLE_STATES:
self.module.warn("Cluster is not in a valid state for Delete operations: %s" % self.target['status'])
else:
_ = self.cdpy.dw.delete_cluster(cluster_id=self.name, force=self.force)
if self.wait:
self.cdpy.sdk.wait_for_state(
describe_func=self.cdpy.dw.describe_cluster,
params=dict(cluster_id=self.name),
field=None, delay=self.delay, timeout=self.timeout
)
else:
self.cdpy.sdk.sleep(self.delay) # Wait for consistency sync
self.cluster = self.cdpy.dw.describe_cluster(cluster_id=self.name)
# End Delete
elif self.state == 'present':
# Begin Config Check
self.module.warn("Cluster is already present and reconciliation is not yet implemented")
if self.wait:
self.target = self.cdpy.sdk.wait_for_state(
describe_func=self.cdpy.dw.describe_cluster,
params=dict(cluster_id=self.name),
state='Running', delay=self.delay, timeout=self.timeout
)
self.cluster = self.target
# End Config Check
else:
self.module.fail_json(msg="State %s is not valid for this module" % self.state)
# End Cluster Exists
else:
# Begin Cluster Not Found
if self.state == 'absent':
self.module.warn("Cluster %s already absent in Environment %s" % (self.name, self.env))
elif self.state == 'present':
if not self.module.check_mode:
# Begin Cluster Creation
self.changed = True
if env_crn is None:
self.module.fail_json(msg="Could not retrieve CRN for CDP Environment %s" % self.env)
else:
self.name = self.cdpy.dw.create_cluster(
env_crn=env_crn, overlay=self.overlay, private_load_balancer=self.private_load_balancer,
aws_public_subnets=self.aws_public_subnets, aws_private_subnets=self.aws_private_subnets,
az_subnet=self.az_subnet, az_enable_az=self.az_enable_az
)
if self.wait:
self.cluster = self.cdpy.sdk.wait_for_state(
describe_func=self.cdpy.dw.describe_cluster,
params=dict(cluster_id=self.name),
state='Running', delay=self.delay, timeout=self.timeout
)
else:
self.cluster = self.cdpy.dw.describe_cluster(cluster_id=self.name)
# End Cluster Creation
else:
self.module.fail_json(msg="Invalid state: %s" % self.state)
# End Cluster Not Found
def main():
module = AnsibleModule(
argument_spec=CdpModule.argument_spec(
cluster_id=dict(type='str', aliases=['id', 'name']),
env=dict(type='str', aliases=['environment', 'env_crn']),
overlay=dict(type='bool', default=False),
private_load_balancer=dict(type='bool', default=False),
az_subnet=dict(type='str'),
az_enable_az=dict(type='bool'),
aws_public_subnets=dict(type='list'),
aws_private_subnets=dict(type='list'),
state=dict(type='str', choices=['present', 'absent'], default='present'),
force=dict(type='bool', default=False),
wait=dict(type='bool', default=True),
delay=dict(type='int', aliases=['polling_delay'], default=15),
timeout=dict(type='int', aliases=['polling_timeout'], default=3600)
),
required_together=[
['az_subnet', 'az_enable_az'],
['aws_public_subnets', 'aws_private_subnets']
],
required_if=[
['state', 'absent', ['cluster_id', 'env'], True],
['state', 'present', ['env']]
],
supports_check_mode=True
)
result = DwCluster(module)
output = dict(changed=result.changed, cluster=result.cluster)
if result.debug:
output.update(sdk_out=result.log_out, sdk_out_lines=result.log_lines)
module.exit_json(**output)
if __name__ == '__main__':
main()