|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright 2024 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 | +DOCUMENTATION = r""" |
| 19 | +module: cm_service_role_config |
| 20 | +short_description: Manage a service role configuration in cluster |
| 21 | +description: |
| 22 | + - Manage a service role configuration (role-specific) in a cluster. |
| 23 | +author: |
| 24 | + - "Webster Mudge (@wmudge)" |
| 25 | +requirements: |
| 26 | + - cm-client |
| 27 | +options: |
| 28 | + name: |
| 29 | + description: |
| 30 | + - A Cloudera Manager Service role name to manage. |
| 31 | + - One of C(name) or C(type) is required. |
| 32 | + type: str |
| 33 | + aliases: |
| 34 | + - role_name |
| 35 | + - role |
| 36 | + type: |
| 37 | + description: |
| 38 | + - A Cloudera Manager Service role type to manage. |
| 39 | + - One of C(name) or C(type) is required. |
| 40 | + type: str |
| 41 | + aliases: |
| 42 | + - role_type |
| 43 | + parameters: |
| 44 | + description: |
| 45 | + - The role-specific configuration to set, i.e. role overrides. |
| 46 | + - To unset a parameter, use C(None) as the value. |
| 47 | + type: dict |
| 48 | + required: yes |
| 49 | + aliases: |
| 50 | + - params |
| 51 | + purge: |
| 52 | + description: |
| 53 | + - Flag for whether the declared parameters should append or overwrite any existing parameters. |
| 54 | + - To clear all parameters, set I(parameters={}), i.e. an empty dictionary, and I(purge=True). |
| 55 | + type: bool |
| 56 | + default: False |
| 57 | + view: |
| 58 | + description: |
| 59 | + - The view to return. |
| 60 | + type: str |
| 61 | + default: summary |
| 62 | + choices: |
| 63 | + - summary |
| 64 | + - full |
| 65 | +extends_documentation_fragment: |
| 66 | + - ansible.builtin.action_common_attributes |
| 67 | + - cloudera.cluster.cm_options |
| 68 | + - cloudera.cluster.cm_endpoint |
| 69 | + - cloudera.cluster.purge |
| 70 | + - cloudera.cluster.message |
| 71 | +attributes: |
| 72 | + check_mode: |
| 73 | + support: full |
| 74 | + diff_mode: |
| 75 | + support: full |
| 76 | + platform: |
| 77 | + platforms: all |
| 78 | +""" |
| 79 | + |
| 80 | +EXAMPLES = r""" |
| 81 | +- name: Update (append) Cloudera manager Service Host Monitor role parameters |
| 82 | + cloudera.cluster.cm_service_role_config: |
| 83 | + host: example.cloudera.com |
| 84 | + username: "jane_smith" |
| 85 | + password: "S&peR4Ec*re" |
| 86 | + type: HOSTMONITOR |
| 87 | + parameters: |
| 88 | + a_configuration: "schema://host:port" |
| 89 | + another_configuration: 234 |
| 90 | +
|
| 91 | +- name: Reset a Cloudera manager Service Host Monitor role parameter |
| 92 | + cloudera.cluster.cm_service_role_config: |
| 93 | + host: example.cloudera.com |
| 94 | + username: "jane_smith" |
| 95 | + password: "S&peR4Ec*re" |
| 96 | + name: "a-non-default-role-name" |
| 97 | + parameters: |
| 98 | + more_configuration: None |
| 99 | +
|
| 100 | +- name: Update (with purge) Cloudera manager Service Host Monitor role parameters |
| 101 | + cloudera.cluster.cluster_service_role_config: |
| 102 | + host: example.cloudera.com |
| 103 | + username: "jane_smith" |
| 104 | + password: "S&peR4Ec*re" |
| 105 | + type: HOSTMONITOR |
| 106 | + parameters: |
| 107 | + config_one: None |
| 108 | + config_two: ValueTwo |
| 109 | + config_three: 2345 |
| 110 | +
|
| 111 | +- name: Reset all Cloudera manager Service Host Monitor role parameters |
| 112 | + cloudera.cluster.cluster_service_role_config: |
| 113 | + host: example.cloudera.com |
| 114 | + username: "jane_smith" |
| 115 | + password: "S&peR4Ec*re" |
| 116 | + type: HOSTMONITOR |
| 117 | + parameters: {} |
| 118 | + purge: yes |
| 119 | +""" |
| 120 | + |
| 121 | +RETURN = r""" |
| 122 | +config: |
| 123 | + description: |
| 124 | + - List of Cloudera Manager Service role configurations. |
| 125 | + - Returns the C(summary) view of the resulting configuration. |
| 126 | + type: list |
| 127 | + elements: dict |
| 128 | + returned: always |
| 129 | + contains: |
| 130 | + name: |
| 131 | + description: |
| 132 | + - The canonical name that identifies this configuration parameter. |
| 133 | + type: str |
| 134 | + returned: when supported |
| 135 | + value: |
| 136 | + description: |
| 137 | + - The user-defined value. |
| 138 | + - When absent, the default value (if any) will be used. |
| 139 | + - Can also be absent, when enumerating allowed configs. |
| 140 | + type: str |
| 141 | + returned: when supported |
| 142 | + required: |
| 143 | + description: |
| 144 | + - Whether this configuration is required for the object. |
| 145 | + - If any required configuration is not set, operations on the object may not work. |
| 146 | + - Requires I(full) view. |
| 147 | + type: bool |
| 148 | + returned: when supported |
| 149 | + default: |
| 150 | + description: |
| 151 | + - The default value. |
| 152 | + - Requires I(full) view. |
| 153 | + type: str |
| 154 | + returned: when supported |
| 155 | + display_name: |
| 156 | + description: |
| 157 | + - A user-friendly name of the parameters, as would have been shown in the web UI. |
| 158 | + - Requires I(full) view. |
| 159 | + type: str |
| 160 | + returned: when supported |
| 161 | + description: |
| 162 | + description: |
| 163 | + - A textual description of the parameter. |
| 164 | + - Requires I(full) view. |
| 165 | + type: str |
| 166 | + returned: when supported |
| 167 | + related_name: |
| 168 | + description: |
| 169 | + - If applicable, contains the related configuration variable used by the source project. |
| 170 | + - Requires I(full) view. |
| 171 | + type: str |
| 172 | + returned: when supported |
| 173 | + sensitive: |
| 174 | + description: |
| 175 | + - Whether this configuration is sensitive, i.e. contains information such as passwords, which might affect how the value of this configuration might be shared by the caller. |
| 176 | + type: bool |
| 177 | + returned: when supported |
| 178 | + validate_state: |
| 179 | + description: |
| 180 | + - State of the configuration parameter after validation. |
| 181 | + - Requires I(full) view. |
| 182 | + type: str |
| 183 | + returned: when supported |
| 184 | + validation_message: |
| 185 | + description: |
| 186 | + - A message explaining the parameter's validation state. |
| 187 | + - Requires I(full) view. |
| 188 | + type: str |
| 189 | + returned: when supported |
| 190 | + validation_warnings_suppressed: |
| 191 | + description: |
| 192 | + - Whether validation warnings associated with this parameter are suppressed. |
| 193 | + - In general, suppressed validation warnings are hidden in the Cloudera Manager UI. |
| 194 | + - Configurations that do not produce warnings will not contain this field. |
| 195 | + - Requires I(full) view. |
| 196 | + type: bool |
| 197 | + returned: when supported |
| 198 | +""" |
| 199 | + |
| 200 | +import json |
| 201 | + |
| 202 | +from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import ( |
| 203 | + ClouderaManagerMutableModule, |
| 204 | + ConfigListUpdates, |
| 205 | +) |
| 206 | + |
| 207 | +from cm_client import MgmtRolesResourceApi |
| 208 | +from cm_client.rest import ApiException |
| 209 | + |
| 210 | + |
| 211 | +class ClouderaManagerServiceRoleConfig(ClouderaManagerMutableModule): |
| 212 | + def __init__(self, module): |
| 213 | + super(ClouderaManagerServiceRoleConfig, self).__init__(module) |
| 214 | + |
| 215 | + # Set the parameters |
| 216 | + self.name = self.get_param("name") |
| 217 | + self.type = self.get_param("type") |
| 218 | + self.params = self.get_param("parameters") |
| 219 | + self.purge = self.get_param("purge") |
| 220 | + self.view = self.get_param("view") |
| 221 | + |
| 222 | + # Initialize the return value |
| 223 | + self.changed = False |
| 224 | + self.diff = {} |
| 225 | + self.config = [] |
| 226 | + |
| 227 | + # Execute the logic |
| 228 | + self.process() |
| 229 | + |
| 230 | + @ClouderaManagerMutableModule.handle_process |
| 231 | + def process(self): |
| 232 | + refresh = True |
| 233 | + role_api = MgmtRolesResourceApi(self.api_client) |
| 234 | + |
| 235 | + try: |
| 236 | + if self.name is None: |
| 237 | + role = next( |
| 238 | + iter( |
| 239 | + [r for r in role_api.read_roles().items if r.type == self.type] |
| 240 | + ), |
| 241 | + None, |
| 242 | + ) |
| 243 | + if role is None: |
| 244 | + self.module.fail_json( |
| 245 | + msg=f"Unable to find Cloudera Manager Service role type '{self.type}" |
| 246 | + ) |
| 247 | + else: |
| 248 | + self.name = role.name |
| 249 | + |
| 250 | + # For some reason, the call to read_roles() doesn't retrieve the configuration |
| 251 | + existing = role_api.read_role_config(self.name) |
| 252 | + except ApiException as ex: |
| 253 | + if ex.status == 404: |
| 254 | + self.module.fail_json(msg=json.loads(ex.body)["message"]) |
| 255 | + else: |
| 256 | + raise ex |
| 257 | + |
| 258 | + updates = ConfigListUpdates(existing, self.params, self.purge) |
| 259 | + |
| 260 | + if updates.changed: |
| 261 | + self.changed = True |
| 262 | + |
| 263 | + if self.module._diff: |
| 264 | + self.diff = updates.diff |
| 265 | + |
| 266 | + if not self.module.check_mode: |
| 267 | + self.config = [ |
| 268 | + p.to_dict() |
| 269 | + for p in role_api.update_role_config( |
| 270 | + self.name, |
| 271 | + message=self.message, |
| 272 | + body=updates.config, |
| 273 | + ).items |
| 274 | + ] |
| 275 | + |
| 276 | + if self.view == "full": |
| 277 | + refresh = False |
| 278 | + |
| 279 | + if refresh: |
| 280 | + self.config = [ |
| 281 | + p.to_dict() |
| 282 | + for p in role_api.read_role_config(self.name, view=self.view).items |
| 283 | + ] |
| 284 | + |
| 285 | + |
| 286 | +def main(): |
| 287 | + module = ClouderaManagerMutableModule.ansible_module( |
| 288 | + argument_spec=dict( |
| 289 | + name=dict(aliases=["role_name", "role"]), |
| 290 | + type=dict(aliases=["role_type"]), |
| 291 | + parameters=dict(type="dict", required=True, aliases=["params"]), |
| 292 | + purge=dict(type="bool", default=False), |
| 293 | + view=dict( |
| 294 | + default="summary", |
| 295 | + choices=["summary", "full"], |
| 296 | + ), |
| 297 | + ), |
| 298 | + required_one_of=[ |
| 299 | + ["name", "type"], |
| 300 | + ], |
| 301 | + supports_check_mode=True, |
| 302 | + ) |
| 303 | + |
| 304 | + result = ClouderaManagerServiceRoleConfig(module) |
| 305 | + |
| 306 | + output = dict( |
| 307 | + changed=result.changed, |
| 308 | + config=result.config, |
| 309 | + ) |
| 310 | + |
| 311 | + if module._diff: |
| 312 | + output.update(diff=result.diff) |
| 313 | + |
| 314 | + if result.debug: |
| 315 | + log = result.log_capture.getvalue() |
| 316 | + output.update(debug=log, debug_lines=log.split("\n")) |
| 317 | + |
| 318 | + module.exit_json(**output) |
| 319 | + |
| 320 | + |
| 321 | +if __name__ == "__main__": |
| 322 | + main() |
0 commit comments