|
42 | 42 | required: False |
43 | 43 | aliases: |
44 | 44 | - 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 |
45 | 53 | extends_documentation_fragment: |
46 | 54 | - cloudera.cloud.cdp_sdk_options |
47 | 55 | - cloudera.cloud.cdp_auth_options |
|
56 | 64 | # Gather detailed information about a named Datahub |
57 | 65 | - cloudera.cloud.datahub_template_info: |
58 | 66 | 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 |
59 | 72 | ''' |
60 | 73 |
|
61 | 74 | RETURN = r''' |
|
90 | 103 | description: The status of the cluster template. |
91 | 104 | returned: always |
92 | 105 | 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 |
93 | 113 | tags: |
94 | 114 | description: Tags added to the cluster template |
95 | 115 | type: dict |
@@ -121,27 +141,53 @@ def __init__(self, module): |
121 | 141 |
|
122 | 142 | # Set variables |
123 | 143 | self.name = self._get_param('name') |
| 144 | + self.content = self._get_param('return_content') |
124 | 145 |
|
125 | 146 | # Initialize return values |
126 | 147 | self.templates = [] |
127 | 148 |
|
| 149 | + # Initialize internal values |
| 150 | + self.all_templates = [] |
| 151 | + |
128 | 152 | # Execute logic process |
129 | 153 | self.process() |
130 | 154 |
|
131 | 155 | @CdpModule._Decorators.process_debug |
132 | 156 | 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) |
137 | 169 | 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 | + |
140 | 185 |
|
141 | 186 | def main(): |
142 | 187 | module = AnsibleModule( |
143 | 188 | 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']) |
145 | 191 | ), |
146 | 192 | supports_check_mode=True |
147 | 193 | ) |
|
0 commit comments