|
| 1 | +# Copyright 2024 Cloudera, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from ansible.module_utils.basic import to_native |
| 16 | +from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import ( |
| 17 | + ClouderaManagerModule, |
| 18 | +) |
| 19 | +from cm_client.rest import ApiException |
| 20 | +from cm_client import ClustersResourceApi |
| 21 | + |
| 22 | +ANSIBLE_METADATA = { |
| 23 | + "metadata_version": "1.1", |
| 24 | + "status": ["preview"], |
| 25 | + "supported_by": "community", |
| 26 | +} |
| 27 | + |
| 28 | +DOCUMENTATION = r""" |
| 29 | +--- |
| 30 | +module: cm_cluster_info |
| 31 | +short_description: Retrieve details about one or more clusters |
| 32 | +description: |
| 33 | + - Retrieves details about one or more clusters managed by Cloudera Manager |
| 34 | +author: |
| 35 | + - "Ronald Suplina (@rsuplina)" |
| 36 | +options: |
| 37 | + name: |
| 38 | + description: |
| 39 | + - Name of Cloudera Manager cluster. |
| 40 | + - This parameter specifies the name of the cluster from which data will be gathered. |
| 41 | + type: str |
| 42 | + required: False |
| 43 | +requirements: |
| 44 | + - cm_client |
| 45 | +""" |
| 46 | + |
| 47 | +EXAMPLES = r""" |
| 48 | +--- |
| 49 | +- name: Get information about the cluster |
| 50 | + cloudera.cluster.cm_cluster_info: |
| 51 | + host: example.cloudera.com |
| 52 | + username: "jane_smith" |
| 53 | + name: "OneNodeCluster" |
| 54 | + password: "S&peR4Ec*re" |
| 55 | + port: "7180" |
| 56 | + |
| 57 | +- name: Get information about all clusters |
| 58 | + cloudera.cluster.cm_cluster_info: |
| 59 | + host: example.cloudera.com |
| 60 | + username: "jane_smith" |
| 61 | + password: "S&peR4Ec*re" |
| 62 | + port: "7180" |
| 63 | +
|
| 64 | +""" |
| 65 | + |
| 66 | +RETURN = r""" |
| 67 | +--- |
| 68 | +clusters: |
| 69 | + description: Details about Cloudera Manager Cluster |
| 70 | + type: list |
| 71 | + elements: dict |
| 72 | + contains: |
| 73 | + cluster_type: |
| 74 | + description: The type of Cloudera Manager cluster. |
| 75 | + type: str |
| 76 | + returned: always |
| 77 | + cluster_url: |
| 78 | + description: Url of Cloudera Manager cluster. |
| 79 | + type: str |
| 80 | + returned: always |
| 81 | + display_name: |
| 82 | + description: The name of the cluster displayed on the site. |
| 83 | + type: str |
| 84 | + returned: always |
| 85 | + entity_status: |
| 86 | + description: Health status of the cluster. |
| 87 | + type: str |
| 88 | + returned: always |
| 89 | + full_version: |
| 90 | + description: Version of the cluster installed. |
| 91 | + type: str |
| 92 | + returned: always |
| 93 | + hosts_url: |
| 94 | + description: Url of all the hosts on which cluster is installed. |
| 95 | + type: str |
| 96 | + returned: always |
| 97 | + maintenance_mode: |
| 98 | + description: Maintance mode of Cloudera Manager Cluster. |
| 99 | + type: bool |
| 100 | + returned: always |
| 101 | + maintenance_owners: |
| 102 | + description: List of Maintance owners for Cloudera Manager Cluster. |
| 103 | + type: list |
| 104 | + returned: always |
| 105 | + name: |
| 106 | + description: The name of the cluster. |
| 107 | + type: str |
| 108 | + returned: always |
| 109 | + tags: |
| 110 | + description: List of tags for Cloudera Manager Cluster. |
| 111 | + type: list |
| 112 | + returned: always |
| 113 | + uuid: |
| 114 | + description: Unique ID of the cluster |
| 115 | + type: bool |
| 116 | + returned: always |
| 117 | +""" |
| 118 | + |
| 119 | + |
| 120 | +class ClusterInfo(ClouderaManagerModule): |
| 121 | + def __init__(self, module): |
| 122 | + super(ClusterInfo, self).__init__(module) |
| 123 | + self.name = self.get_param("name") |
| 124 | + self.output = [] |
| 125 | + self.process() |
| 126 | + |
| 127 | + @ClouderaManagerModule.handle_process |
| 128 | + def process(self): |
| 129 | + try: |
| 130 | + cluster_api_instance = ClustersResourceApi(self.api_client) |
| 131 | + if self.name: |
| 132 | + self.output = [cluster_api_instance.read_cluster(cluster_name=self.name).to_dict()] |
| 133 | + else: |
| 134 | + self.output = cluster_api_instance.read_clusters().to_dict()['items'] |
| 135 | + |
| 136 | + except ApiException as e: |
| 137 | + if e.status == 404: |
| 138 | + pass |
| 139 | + else: |
| 140 | + raise e |
| 141 | + except KeyError as ke: |
| 142 | + self.module.fail_json(msg='Invalid result object from Cloudera Manager API', error=to_native(ke)) |
| 143 | + |
| 144 | + |
| 145 | +def main(): |
| 146 | + module = ClouderaManagerModule.ansible_module( |
| 147 | + |
| 148 | + argument_spec=dict( |
| 149 | + name=dict(aliases=["cluster_name","cluster"]), |
| 150 | + ), |
| 151 | + supports_check_mode=True |
| 152 | + ) |
| 153 | + |
| 154 | + result = ClusterInfo(module) |
| 155 | + |
| 156 | + output = dict( |
| 157 | + changed=False, |
| 158 | + clusters=result.output, |
| 159 | + ) |
| 160 | + |
| 161 | + if result.debug: |
| 162 | + log = result.log_capture.getvalue() |
| 163 | + output.update(debug=log, debug_lines=log.split("\n")) |
| 164 | + |
| 165 | + module.exit_json(**output) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + main() |
0 commit comments