Skip to content

Commit 3aee1cf

Browse files
authored
Add new definition info module for datahubs and update datahub_cluste… (#12)
* Add new definition info module for datahubs and update datahub_cluster.py to accommodate creating clusters by definition only * Add content flag for workloadTemplate retrieval. Signed-off-by: Chris Perro <cmperro@gmail.com>
1 parent b71e1e8 commit 3aee1cf

File tree

3 files changed

+220
-10
lines changed

3 files changed

+220
-10
lines changed

docsrc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cloudera.cloud Ansible Collection
1313
datalake_runtime_info <datalake_runtime_info>
1414
datahub_cluster <datahub_cluster>
1515
datahub_cluster_info <datahub_cluster_info>
16+
datahub_definition_info <datahub_definition_info>
1617
datahub_template_info <datahub_template_info>
1718
df <df>
1819
df_info <df_info>

plugins/modules/datahub_cluster.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
author:
3232
- "Webster Mudge (@wmudge)"
3333
- "Daniel Chaffelson (@chaffelson)"
34+
- "Chris Perro (@cmperro)"
3435
requirements:
3536
- cdpy
3637
options:
@@ -189,7 +190,7 @@
189190
EXAMPLES = r'''
190191
# Note: These examples do not set authentication details.
191192
192-
# Create a datahub (and do not wait for status change)
193+
# Create a datahub specifying instance group details (and do not wait for status change)
193194
- cloudera.cloud.datahub_cluster:
194195
name: datahub-name
195196
env: name-or-crn
@@ -214,6 +215,15 @@
214215
project: Arbitrary content
215216
wait: no
216217
218+
# Create a datahub specifying only a definition name
219+
- cloudera.cloud.datahub_cluster:
220+
name: datahub-name
221+
env: name-or-crn
222+
definition: definition-name
223+
tags:
224+
project: Arbitrary content
225+
wait: no
226+
217227
# Delete the datahub (and wait for status change)
218228
cloudera.cloud.datahub:
219229
name: example-datahub
@@ -484,13 +494,16 @@ def create_cluster(self):
484494
def _configure_payload(self):
485495
payload = dict(
486496
clusterName=self.name,
487-
environmentName=self.environment,
488-
clusterDefinitionName=self.definition,
489-
image={"id": self.image_id, "catalogName": self.image_catalog},
490-
clusterTemplateName=self.template,
491-
instanceGroups=self.groups
497+
environmentName=self.environment
492498
)
493499

500+
if self.definition is not None:
501+
payload["clusterDefinitionName"]=self.definition
502+
else:
503+
payload["image"]={"id": self.image_id, "catalogName": self.image_catalog}
504+
payload["clusterTemplateName"]=self.template
505+
payload["instanceGroups"]=self.groups
506+
494507
if self.host_env['cloudPlatform'] == 'GCP':
495508
payload['subnetName'] = self.subnet
496509
else:
@@ -549,10 +562,11 @@ def main():
549562
delay=dict(required=False, type='int', aliases=['polling_delay'], default=15),
550563
timeout=dict(required=False, type='int', aliases=['polling_timeout'], default=3600)
551564
),
552-
supports_check_mode=True,
553-
required_together=[
554-
['subnet', 'image', 'catalog', 'template', 'groups', 'environment'],
555-
]
565+
supports_check_mode=True
566+
#Punting on additional checks here. There are a variety of supporting datahub invocations that can make this more complex
567+
#required_together=[
568+
# ['subnet', 'image', 'catalog', 'template', 'groups', 'environment'],
569+
#]
556570
)
557571

558572
result = DatahubCluster(module)
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright 2021 Cloudera, Inc. All Rights Reserved.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
from ansible.module_utils.basic import AnsibleModule
19+
from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_common import CdpModule
20+
21+
ANSIBLE_METADATA = {'metadata_version': '1.1',
22+
'status': ['preview'],
23+
'supported_by': 'community'}
24+
25+
DOCUMENTATION = r'''
26+
---
27+
module: datahub_definition_info
28+
short_description: Gather information about CDP Datahub Cluster Definitions
29+
description:
30+
- Gather information about CDP Datahub Cluster Definitions
31+
author:
32+
- "Chris Perro (@cmperro)"
33+
- "Webster Mudge (@wmudge)"
34+
- "Dan Chaffelson (@chaffelson)"
35+
- "Chris Perro (@cmperro)"
36+
requirements:
37+
- cdpy
38+
options:
39+
name:
40+
description:
41+
- If a name or CRN is provided, that Definition will be described.
42+
- If no name or CRN is provided, all Definitions will be listed.
43+
type: str
44+
required: False
45+
aliases:
46+
- definition
47+
- crn
48+
content:
49+
description: Flag dictating if the workload template content of the cluster definition is returned
50+
type: bool
51+
required: False
52+
default: False
53+
aliases:
54+
- definition_content
55+
extends_documentation_fragment:
56+
- cloudera.cloud.cdp_sdk_options
57+
- cloudera.cloud.cdp_auth_options
58+
'''
59+
60+
EXAMPLES = r'''
61+
# Note: These examples do not set authentication details.
62+
63+
# List basic information about all Datahubs
64+
- cloudera.cloud.datahub_definition_info:
65+
66+
# Gather detailed information about a named Datahub
67+
- cloudera.cloud.datahub_definition_info:
68+
name: example-definition
69+
'''
70+
71+
RETURN = r'''
72+
---
73+
definitions:
74+
description: The information about the named Definition or Definitions
75+
type: list
76+
returned: on success
77+
elements: complex
78+
contains:
79+
clusterDefinitionName:
80+
description: The name of the cluster definition.
81+
returned: always
82+
type: str
83+
crn:
84+
description: The CRN of the cluster definition.
85+
returned: always
86+
type: str
87+
type:
88+
description: The type of cluster definition.
89+
returned: always
90+
type: str
91+
sample:
92+
- DATAENGINEERING
93+
- DATAMART
94+
- DISCOVERY_DATA_AND_EXPLORATION
95+
- FLOW_MANAGEMENT
96+
- OPERATIONALDATABASE
97+
- STREAMING
98+
nodeCount:
99+
description: The node count of the cluster definition.
100+
returned: always
101+
type: str
102+
cloudPlatform:
103+
description: The cloud provider of the cluster definition.
104+
returned: always
105+
type: str
106+
productVersion:
107+
description: The product version of the cluster definition.
108+
returned: always
109+
type: str
110+
description:
111+
description: The description of the cluster definition.
112+
returned: always
113+
type: str
114+
workloadTemplate:
115+
description: The workload template of the cluster definition, in JSON.
116+
returned: when specified
117+
type: str
118+
sdk_out:
119+
description: Returns the captured CDP SDK log.
120+
returned: when supported
121+
type: str
122+
sdk_out_lines:
123+
description: Returns a list of each line of the captured CDP SDK log.
124+
returned: when supported
125+
type: list
126+
elements: str
127+
'''
128+
129+
130+
class DatahubDefinitionInfo(CdpModule):
131+
def __init__(self, module):
132+
super(DatahubDefinitionInfo, self).__init__(module)
133+
134+
# Set variables
135+
self.name = self._get_param('name')
136+
self.content = self._get_param('content')
137+
138+
# Initialize internal values
139+
self.all_definitions = []
140+
141+
# Initialize return values
142+
self.definitions = []
143+
144+
# Execute logic process
145+
self.process()
146+
147+
@CdpModule._Decorators.process_debug
148+
def process(self):
149+
self.all_definitions = self.cdpy.datahub.list_cluster_definitions()
150+
if self.name:
151+
short_desc = next((d for d in self.all_definitions if d['crn'] == self.name or
152+
d['clusterDefinitionName']== self.name), None)
153+
if short_desc is not None:
154+
if self.content:
155+
self.definitions.append(self._describe_definition(short_desc))
156+
else:
157+
self.definitions.append(short_desc)
158+
else:
159+
self.module.warn("Definition not found, '%s'" % self.name)
160+
else:
161+
if self.content:
162+
for short_desc in self.all_definitions:
163+
self.definitions.append(self._describe_definition(short_desc))
164+
else:
165+
self.definitions = self.all_definitions
166+
167+
def _describe_definition(self, short_desc):
168+
full_desc = self.cdpy.datahub.describe_cluster_definition(short_desc['crn'])
169+
if full_desc is not None:
170+
full_desc.update(productVersion=short_desc['productVersion'], nodeCount=short_desc['nodeCount'])
171+
return full_desc
172+
else:
173+
self.module.fail_json(msg="Failed to retrieve Cluster Definition content, '%s'" %
174+
short_desc['clusterDefinitionName'])
175+
176+
def main():
177+
module = AnsibleModule(
178+
argument_spec=CdpModule.argument_spec(
179+
name=dict(required=False, type='str', aliases=['definition', 'crn']),
180+
content=dict(required=False, type='bool', default=False, aliases=['definition_content'])
181+
),
182+
supports_check_mode=True
183+
)
184+
185+
result = DatahubDefinitionInfo(module)
186+
output = dict(changed=False, definitions=result.definitions)
187+
188+
if result.debug:
189+
output.update(sdk_out=result.log_out, sdk_out_lines=result.log_lines)
190+
191+
module.exit_json(**output)
192+
193+
194+
if __name__ == '__main__':
195+
main()

0 commit comments

Comments
 (0)