forked from cloudera-labs/cloudera.cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdf.py
More file actions
379 lines (352 loc) · 13.2 KB
/
Copy pathdf.py
File metadata and controls
379 lines (352 loc) · 13.2 KB
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
370
371
372
373
374
375
376
377
378
379
#!/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: df
short_description: Enable or Disable CDP DataFlow Services
description:
- Enable or Disable CDP DataFlow Services
author:
- "Dan Chaffelson (@chaffelson)"
requirements:
- cdpy
options:
crn:
description: The name or crn of the CDP Environment to host the Dataflow Service
type: str
required: True
aliases:
- name
- env_crn
state:
description:
- The declarative state of the Dataflow Service
type: str
required: False
default: present
choices:
- present
- enabled
- absent
- disabled
nodes_min:
description: The minimum number of kubernetes nodes needed for the environment.
Note that the lowest minimum is 3 nodes.
type: int
default: 3
required: False
aliases:
- min_k8s_node_count
nodes_max:
description: The maximum number of kubernetes nodes that environment may scale up under high-demand situations.
type: int
default: 3
required: False
aliases:
- max_k8s_node_count
public_loadbalancer:
description: Indicates whether or not to use a public load balancer when deploying dependencies stack.
type: bool
required: False
aliases:
- use_public_load_balancer
ip_ranges:
description: The IP ranges authorized to connect to the Kubernetes API server
type: list
required: False
aliases:
- authorized_ip_ranges
persist:
description: Whether or not to retain the database records of related entities during removal.
type: bool
required: False
default: False
wait:
description:
- Flag to enable internal polling to wait for the Dataflow Service to achieve the declared state.
- If set to FALSE, the module will return immediately.
type: bool
required: False
default: True
delay:
description:
- The internal polling interval (in seconds) while the module waits for the Dataflow Service to achieve the
declared state.
type: int
required: False
default: 15
aliases:
- polling_delay
timeout:
description:
- The internal polling timeout (in seconds) while the module waits for the Dataflow Service to achieve the
declared state.
type: int
required: False
default: 3600
aliases:
- polling_timeout
notes:
- This feature this module is for is in Technical Preview
extends_documentation_fragment:
- cloudera.cloud.cdp_sdk_options
- cloudera.cloud.cdp_auth_options
'''
EXAMPLES = r'''
# Note: These examples do not set authentication details.
# Create a Dataflow Service
- cloudera.cloud.df:
name: my-service
nodes_min: 3
nodes_max: 10
public_loadbalancer: True
ip_ranges: ['192.168.0.1/24']
state: present
wait: yes
# Remove a Dataflow Service with Async wait
- cloudera.cloud.df:
name: my-service
persist: False
state: absent
wait: yes
async: 3600
poll: 0
register: __my_teardown_request
'''
RETURN = r'''
---
environments:
description: The information about the named DataFlow Service or DataFlow Services
type: list
returned: always
elements: complex
contains:
crn:
description: The DataFlow Service's parent environment CRN.
returned: always
type: str
name:
description: The DataFlow Service's parent environment name.
returned: always
type: str
cloudPlatform:
description: The cloud platform of the environment.
returned: always
type: str
region:
description: The region of the environment.
returned: always
type: str
deploymentCount:
description: The deployment count.
returned: always
type: str
minK8sNodeCount:
description: The minimum number of Kubernetes nodes that need to be provisioned in the environment.
returned: always
type: int
maxK8sNodeCount:
description: The maximum number of kubernetes nodes that environment may scale up under high-demand situations.
returned: always
type: str
status:
description: The status of a DataFlow enabled environment.
returned: always
type: dict
contains:
state:
description: The state of the environment.
returned: always
type: str
message:
description: A status message for the environment.
returned: always
type: str
k8sNodeCount:
description: The number of kubernetes nodes currently in use by DataFlow for this environment.
returned: always
type: int
instanceType:
description: The instance type of the kubernetes nodes currently in use by DataFlow for this environment.
returned: always
type: str
dfLocalUrl:
description: The URL of the environment local DataFlow application.
returned: always
type: str
authorizedIpRanges:
description: The authorized IP Ranges.
returned: always
type: list
activeWarningAlertCount:
description: Current count of active alerts classified as a warning.
returned: always
type: int
activeErrorAlertCount:
description: Current count of active alerts classified as an error.
returned: always
type: int
clusterId:
description: Cluster id of the environment.
returned: if enabled
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 DFService(CdpModule):
def __init__(self, module):
super(DFService, self).__init__(module)
# Set variables
self.name = self._get_param('name')
self.nodes_min = self._get_param('nodes_min')
self.nodes_max = self._get_param('nodes_max')
self.public_loadbalancer = self._get_param('public_loadbalancer')
self.ip_ranges = self._get_param('ip_ranges')
self.persist = self._get_param('persist')
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.service = {}
# Initialize internal values
self.target = None
self.env_crn = None
# Execute logic process
self.process()
@CdpModule._Decorators.process_debug
def process(self):
self.env_crn = self.cdpy.environments.resolve_environment_crn(self.name)
if self.env_crn is not None:
self.target = self.cdpy.df.describe_environment(env_crn=self.name)
if self.target is not None:
# DF Database Entry exists
if self.state in ['absent']:
if self.module.check_mode:
self.service = self.target
else:
self._disable_df()
elif self.state in ['present']:
self.module.warn(
"Dataflow Service already enabled and configuration validation and reconciliation is not supported;" +
"to change a Dataflow Service, explicitly disable and recreate the Service or use the UI")
if self.wait:
self.service = self._wait_for_enabled()
else:
self.module.fail_json(
msg="State %s is not valid for this module" % self.state)
else:
# Environment does not have DF database entry, and probably doesn't exist
if self.state in ['absent']:
self.module.log(
"Dataflow Service %s already disabled in CDP Environment %s" % (self.name, self.env_crn))
elif self.state in ['present']:
if self.env_crn is None:
self.module.fail_json(msg="Could not retrieve CRN for CDP Environment %s" % self.env)
else:
# create DF Service
if not self.module.check_mode:
self.service = self.cdpy.df.enable_environment(
env_crn=self.env_crn,
authorized_ips=self.ip_ranges,
min_nodes=self.nodes_min,
max_nodes=self.nodes_max,
enable_public_ip=self.public_loadbalancer
)
if self.wait:
self.service = self._wait_for_enabled()
else:
self.module.fail_json(
msg="State %s is not valid for this module" % self.state)
def _wait_for_enabled(self):
return self.cdpy.sdk.wait_for_state(
describe_func=self.cdpy.df.describe_environment, params=dict(env_crn=self.env_crn),
field=['status', 'state'], state=self.cdpy.sdk.STARTED_STATES,
delay=self.delay, timeout=self.timeout
)
def _disable_df(self):
# Attempt clean Disable, which also ensures we have tried at least once before we do a forced removal
if self.target['status']['state'] in self.cdpy.sdk.REMOVABLE_STATES:
self.service = self.cdpy.df.disable_environment(
env_crn=self.env_crn,
persist=self.persist
)
if self.wait:
# Wait for Clean Disable, if possible
self.service = self.cdpy.sdk.wait_for_state(
describe_func=self.cdpy.df.describe_environment, params=dict(env_crn=self.env_crn),
field=['status', 'state'],
state=self.cdpy.sdk.STOPPED_STATES + self.cdpy.sdk.REMOVABLE_STATES + [None],
delay=self.delay, timeout=self.timeout, ignore_failures=True
)
else:
self.service = self.cdpy.df.describe_environment(env_crn=self.name)
# Check disable result against need for further forced delete action, in case it didn't work first time around
if self.service is not None:
if self.service['status']['state'] in self.cdpy.sdk.REMOVABLE_STATES:
if self.force:
self.service = self.cdpy.df.delete_environment(
env_crn=self.env_crn
)
else:
self.module.fail_json(msg="DF Service Disable failed and Force delete not requested")
if self.wait:
self.service = self.cdpy.sdk.wait_for_state(
describe_func=self.cdpy.df.describe_environment, params=dict(env_crn=self.env_crn),
field=None, # This time we require removal or declare failure
delay=self.delay, timeout=self.timeout
)
else:
self.service = self.cdpy.df.describe_environment(env_crn=self.name)
def main():
module = AnsibleModule(
argument_spec=CdpModule.argument_spec(
name=dict(required=True, type='str', aliases=['crn', 'env_crn']),
nodes_min=dict(required=False, type='int', default=3, aliases=['min_k8s_node_count']),
nodes_max=dict(required=False, type='int', default=3, aliases=['max_k8s_node_count']),
public_loadbalancer=dict(required=False, type='bool', default=False, aliases=['use_public_load_balancer']),
ip_ranges=dict(required=False, type='list', elements='str', default=list(),
aliases=['authorized_ip_ranges']),
persist=dict(required=False, type='bool', default=False),
state=dict(required=False, type='str', choices=['present', 'absent'],
default='present'),
force=dict(required=False, type='bool', default=False, aliases=['force_delete']),
wait=dict(required=False, type='bool', default=True),
delay=dict(required=False, type='int', aliases=['polling_delay'], default=15),
timeout=dict(required=False, type='int', aliases=['polling_timeout'], default=3600)
),
supports_check_mode=True,
)
result = DFService(module)
output = dict(changed=False, service=result.service)
if result.debug:
output.update(sdk_out=result.log_out, sdk_out_lines=result.log_lines)
module.exit_json(**output)
if __name__ == '__main__':
main()