|
16 | 16 | ClouderaManagerModule, |
17 | 17 | ) |
18 | 18 |
|
| 19 | +from ansible_collections.cloudera.cluster.plugins.module_utils.parcel_utils import ( |
| 20 | + parse_parcel_result, |
| 21 | +) |
| 22 | + |
19 | 23 | from cm_client import ClustersResourceApi, ParcelResourceApi, ParcelsResourceApi |
20 | 24 | from cm_client.rest import ApiException |
21 | 25 |
|
|
26 | 30 | } |
27 | 31 |
|
28 | 32 | DOCUMENTATION = r""" |
29 | | ---- |
30 | 33 | module: parcel_info |
31 | 34 | short_description: Gather details about the parcels on the cluster |
32 | 35 | description: |
33 | 36 | - Gathers details about a single parcel or about all parcels on the cluster |
34 | 37 | author: |
35 | 38 | - "Ronald Suplina (@rsuplina)" |
36 | 39 | requirements: |
37 | | - - cm_client |
| 40 | + - cm-client |
38 | 41 | options: |
39 | | - cluster_name: |
| 42 | + cluster: |
40 | 43 | description: |
41 | 44 | - The name of the cluster |
42 | 45 | type: str |
43 | 46 | required: yes |
44 | | - product: |
| 47 | + aliases: |
| 48 | + - cluster_name |
| 49 | + name: |
45 | 50 | description: |
46 | | - - The name of the product, e.g. CDH, Impala |
| 51 | + - The name of the product, e.g. CDH, Impala. |
| 52 | + - Required if I(parcel_version) is declared. |
47 | 53 | type: str |
48 | 54 | required: no |
| 55 | + aliases: |
| 56 | + - product |
| 57 | + - parcel |
49 | 58 | parcel_version: |
50 | 59 | description: |
51 | 60 | - The version of the product, e.g. 1.1.0, 2.3.0. |
| 61 | + - Required if I(name) is declared. |
52 | 62 | type: str |
53 | 63 | required: no |
54 | 64 | """ |
55 | 65 |
|
56 | 66 | EXAMPLES = r""" |
57 | | ---- |
58 | 67 | - name: Gather details about specific parcel |
59 | 68 | cloudera.cluster.parcel_info: |
60 | 69 | host: example.cloudera.com |
|
73 | 82 | """ |
74 | 83 |
|
75 | 84 | RETURN = r""" |
76 | | ---- |
77 | | -cloudera_manager: |
78 | | - description: Returns details about specific parcel or all parcels on the cluster |
| 85 | +parcels: |
| 86 | + description: Returns details about a specific parcel or all parcels on the cluster |
79 | 87 | type: list |
80 | 88 | elements: dict |
81 | 89 | contains: |
|
94 | 102 | state: |
95 | 103 | description: |
96 | 104 | - The state of the parcel. |
97 | | - - Shows the progress of state transitions and reports any errors. |
| 105 | + - This shows the progress of state transitions and if there were any errors. |
98 | 106 | type: dict |
99 | 107 | returned: when supported |
100 | 108 | cluster_name: |
101 | 109 | description: The name of the enclosing cluster. |
102 | 110 | type: dict |
103 | 111 | returned: always |
104 | | - displayName: |
| 112 | + display_name: |
105 | 113 | description: Display name of the parcel. |
106 | 114 | type: str |
107 | 115 | returned: when supported |
|
115 | 123 | class ClouderaParcelInfo(ClouderaManagerModule): |
116 | 124 | def __init__(self, module): |
117 | 125 | super(ClouderaParcelInfo, self).__init__(module) |
118 | | - self.cluster_name = self.get_param("cluster_name") |
119 | | - self.product = self.get_param("product") |
| 126 | + |
| 127 | + self.cluster = self.get_param("cluster") |
| 128 | + self.parcel = self.get_param("name") |
120 | 129 | self.parcel_version = self.get_param("parcel_version") |
| 130 | + |
| 131 | + self.output = {} |
| 132 | + self.changed = False |
| 133 | + |
121 | 134 | self.process() |
122 | 135 |
|
123 | 136 | @ClouderaManagerModule.handle_process |
124 | 137 | def process(self): |
125 | | - parcel_api_instance = ParcelResourceApi(self.api_client) |
126 | | - parcels_api_instance = ParcelsResourceApi(self.api_client) |
127 | | - cluster_api_instance = ClustersResourceApi(self.api_client) |
128 | 138 |
|
129 | | - self.parcel_output = {} |
130 | | - self.changed = False |
| 139 | + parcel_api = ParcelResourceApi(self.api_client) |
| 140 | + parcels_api = ParcelsResourceApi(self.api_client) |
| 141 | + cluster_api = ClustersResourceApi(self.api_client) |
131 | 142 |
|
132 | 143 | try: |
133 | | - cluster_api_instance.read_cluster(cluster_name=self.cluster_name).to_dict() |
| 144 | + cluster_api.read_cluster(cluster_name=self.cluster) |
134 | 145 | except ApiException as ex: |
135 | 146 | if ex.status == 404: |
136 | | - self.module.fail_json(msg=f" Cluster {self.cluster_name} {ex.reason}") |
137 | | - |
138 | | - if self.product and self.parcel_version: |
139 | | - self.parcel_info = parcel_api_instance.read_parcel( |
140 | | - cluster_name=self.cluster_name, |
141 | | - product=self.product, |
142 | | - version=self.parcel_version, |
143 | | - ).to_dict() |
144 | | - self.parcel_output = {"items": [self.parcel_info]} |
| 147 | + self.module.fail_json(msg=f"Cluster '{self.cluster}' not found") |
| 148 | + |
| 149 | + if self.parcel and self.parcel_version: |
| 150 | + try: |
| 151 | + parcel_info = parcel_api.read_parcel( |
| 152 | + cluster_name=self.cluster, |
| 153 | + product=self.parcel, |
| 154 | + version=self.parcel_version, |
| 155 | + ) |
| 156 | + self.output = [parse_parcel_result(parcel_info)] |
| 157 | + except ApiException as ex: |
| 158 | + if ex.status == 404: |
| 159 | + pass |
145 | 160 | else: |
146 | | - self.parcel_output = parcels_api_instance.read_parcels( |
147 | | - cluster_name=self.cluster_name |
148 | | - ).to_dict() |
| 161 | + self.output = [ |
| 162 | + parse_parcel_result(p) |
| 163 | + for p in parcels_api.read_parcels(cluster_name=self.cluster).items |
| 164 | + ] |
149 | 165 |
|
150 | 166 |
|
151 | 167 | def main(): |
152 | 168 | module = ClouderaManagerModule.ansible_module( |
153 | 169 | argument_spec=dict( |
154 | | - cluster_name=dict(required=True, type="str"), |
155 | | - product=dict(required=False, type="str"), |
156 | | - parcel_version=dict(required=False, type="str"), |
| 170 | + cluster=dict(required=True, aliases=["cluster_name"]), |
| 171 | + name=dict(aliases=["product", "parcel"]), |
| 172 | + parcel_version=dict(), |
157 | 173 | ), |
158 | 174 | supports_check_mode=True, |
159 | 175 | required_together=[ |
160 | | - ("product", "parcel_version"), |
| 176 | + ("name", "parcel_version"), |
161 | 177 | ], |
162 | 178 | ) |
163 | 179 |
|
164 | 180 | result = ClouderaParcelInfo(module) |
165 | 181 |
|
166 | | - changed = result.changed |
167 | | - |
168 | 182 | output = dict( |
169 | | - changed=changed, |
170 | | - cloudera_manager=result.parcel_output, |
| 183 | + changed=result.changed, |
| 184 | + parcels=result.output, |
171 | 185 | ) |
172 | 186 |
|
173 | 187 | if result.debug: |
|
0 commit comments