Skip to content

Commit b71e1e8

Browse files
authored
Add 'content' flag for including template content. (#13)
Bug fix to describe template, not cluster. Signed-off-by: Webster Mudge <wmudge@cloudera.com>
1 parent b404b9c commit b71e1e8

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

plugins/modules/datahub_template_info.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
required: False
4343
aliases:
4444
- template
45+
return_content:
46+
description: Flag dictating if cluster template content is returned
47+
type: bool
48+
required: False
49+
default: False
50+
aliases:
51+
- template_content
52+
- content
4553
extends_documentation_fragment:
4654
- cloudera.cloud.cdp_sdk_options
4755
- cloudera.cloud.cdp_auth_options
@@ -56,6 +64,11 @@
5664
# Gather detailed information about a named Datahub
5765
- cloudera.cloud.datahub_template_info:
5866
name: example-template
67+
68+
# Gather detailed information about a named Datahub, including the template contents in JSON
69+
- cloudera.cloud.datahub_template_info:
70+
name: example-template
71+
return_content: yes
5972
'''
6073

6174
RETURN = r'''
@@ -90,6 +103,13 @@
90103
description: The status of the cluster template.
91104
returned: always
92105
type: str
106+
sample:
107+
- DEFAULT
108+
- USER_MANAGED
109+
clusterTemplateContent:
110+
description: The cluster template contents, in JSON.
111+
returned: when specified
112+
type: str
93113
tags:
94114
description: Tags added to the cluster template
95115
type: dict
@@ -121,27 +141,53 @@ def __init__(self, module):
121141

122142
# Set variables
123143
self.name = self._get_param('name')
144+
self.content = self._get_param('return_content')
124145

125146
# Initialize return values
126147
self.templates = []
127148

149+
# Initialize internal values
150+
self.all_templates = []
151+
128152
# Execute logic process
129153
self.process()
130154

131155
@CdpModule._Decorators.process_debug
132156
def process(self):
133-
if self.name: # Note that both None and '' will trigger this
134-
template_single = self.cdpy.datahub.describe_cluster(self.name)
135-
if template_single is not None:
136-
self.templates.append(template_single)
157+
self.all_templates = self.cdpy.datahub.list_cluster_templates()
158+
159+
if self.name:
160+
short_desc = next((t for t in self.all_templates if t['crn'] == self.name
161+
or t['clusterTemplateName'] == self.name), None)
162+
if short_desc is not None:
163+
if self.content:
164+
self.templates.append(self._describe_template(short_desc))
165+
else:
166+
self.templates.append(short_desc)
167+
else:
168+
self.module.warn("Template not found, '%s'" % self.name)
137169
else:
138-
self.templates = self.cdpy.datahub.list_cluster_templates()
139-
170+
if self.content:
171+
for short_desc in self.all_templates:
172+
self.templates.append(self._describe_template(short_desc))
173+
else:
174+
self.templates = self.all_templates
175+
176+
def _describe_template(self, short_desc):
177+
full_desc = self.cdpy.datahub.describe_cluster_template(short_desc['crn'])
178+
if full_desc is not None:
179+
full_desc.update(productVersion=short_desc['productVersion'])
180+
return full_desc
181+
else:
182+
self.module.fail_json(msg="Failed to retrieve Cluster Template content, '%s'" %
183+
short_desc['clusterTemplateName'])
184+
140185

141186
def main():
142187
module = AnsibleModule(
143188
argument_spec=CdpModule.argument_spec(
144-
name=dict(required=False, type='str', aliases=['template'])
189+
name=dict(required=False, type='str', aliases=['template', 'crn']),
190+
return_content=dict(required=False, type='bool', default=False, aliases=['template_content', 'content'])
145191
),
146192
supports_check_mode=True
147193
)

0 commit comments

Comments
 (0)