|
| 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