From 82768784dcd1ca456d9b383b4e2ce6f8b1b72e60 Mon Sep 17 00:00:00 2001 From: rsuplina Date: Fri, 5 Jan 2024 11:50:18 +0000 Subject: [PATCH 1/2] Add CM Service Info Module Signed-off-by: rsuplina --- plugins/modules/cm_service_info.py | 180 ++++++++++++++++++ .../cm_service_info/test_cm_service_info.py | 46 +++++ 2 files changed, 226 insertions(+) create mode 100644 plugins/modules/cm_service_info.py create mode 100644 tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py diff --git a/plugins/modules/cm_service_info.py b/plugins/modules/cm_service_info.py new file mode 100644 index 00000000..ccc0c322 --- /dev/null +++ b/plugins/modules/cm_service_info.py @@ -0,0 +1,180 @@ +# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import ( + ClouderaManagerModule, +) + +from cm_client import MgmtServiceResourceApi + +ANSIBLE_METADATA = { + "metadata_version": "1.1", + "status": ["preview"], + "supported_by": "community", +} + +DOCUMENTATION = r""" +--- +module: cm_service_info +short_description: Retrieve information about the Cloudera Management Services +description: + - Gather information about the Cloudera Manager service. +author: + - "Ronald Suplina (@rsuplina)" +requirements: + - cm_client +""" + +EXAMPLES = r""" +--- +- name: Gather details using an host + cloudera.cluster.cm_version: + host: "example.cloudera.host" + username: "will_jordan" + password: "S&peR4Ec*re" + register: cm_output +""" + +RETURN = r""" +--- +cloudera_manager: + description: Details about Cloudera Manager Service + type: dict + contains: + name: + description: The Cloudera Manager service name. + type: str + returned: optional + type: + description: The Cloudera Manager service type. + type: str + returned: optional + cluster_ref: + description: Reference to a cluster. + type: str + returned: optional + service_state: + description: State of the Cloudera Manager Service. + type: str + returned: optional + health_summary: + description: Health of the Cloudera Manager Service. + type: str + returned: optional + config_stale: + description: Configuration state of Cloudera Manager Service. + type: str + returned: optional + config_staleness_status: + description: Status of configuration staleness for Cloudera Manager Service. + type: str + returned: optional + client_config_staleness_status: + description: Status of Client configuration for Cloudera Manager Service. + type: str + returned: optional + health_checks: + description: Lists all available health checks for Cloudera Manager Service. + type: dict + returned: optional + service_url: + description: Service url for Cloudera Manager Service. + type: str + returned: optional + role_instances_url: + description: Role instance url for Cloudera Manager Service. + type: str + returned: optional + maintenance_mode: + description: Maintance mode of Cloudera Manager Service. + type: bool + returned: optional + maintenance_owners: + description: List of Maintance owners for Cloudera Manager Service. + type: list + returned: optional + config: + description: Configuration details for Cloudera Manager Service. + type: dict + returned: optional + roles: + description: Role list of Cloudera Manager Service. + type: dict + returned: optional + display_name: + description: Display name of Cloudera Manager Service. + type: dict + returned: optional + role_config_groups: + description: List of role configuration groups for Cloudera Manager Service. + type: list + returned: optional + replication_schedules: + description: List of replication schedules for Cloudera Manager Service. + type: list + returned: optional + snapshot_policies: + description: Snapshot policy for Cloudera Manager Service. + type: str + returned: optional + entity_status: + description: Health status of entities for Cloudera Manager Service. + type: str + returned: optional + tags: + description: List of tags for Cloudera Manager Service. + type: list + returned: optional + service_version: + description: Version of Cloudera Manager Service. + type: str + returned: optional +""" + + +class ClouderaServiceInfo(ClouderaManagerModule): + def __init__(self, module): + super(ClouderaServiceInfo, self).__init__(module) + + # Initialize the return values + self.cm_service_info = dict() + + # Execute the logic + self.process() + + @ClouderaManagerModule.handle_process + def process(self): + api_instance = MgmtServiceResourceApi(self.api_client) + self.cm_service_info = api_instance.read_service().to_dict() + + +def main(): + module = ClouderaManagerModule.ansible_module(supports_check_mode=True) + + result = ClouderaServiceInfo(module) + + output = dict( + changed=False, + cloudera_manager=result.cm_service_info, + ) + + if result.debug: + log = result.log_capture.getvalue() + output.update(debug=log, debug_lines=log.split("\n")) + + module.exit_json(**output) + + +if __name__ == "__main__": + main() diff --git a/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py b/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py new file mode 100644 index 00000000..2c05b009 --- /dev/null +++ b/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import pytest +import unittest + + +from ansible_collections.cloudera.cluster.plugins.modules import cm_service_info +from ansible_collections.cloudera.cluster.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, setup_module_args + + +@unittest.skipUnless(os.getenv('CM_USERNAME'), "Cloudera Manager access parameters not set") +class TestCMServiceInfo(ModuleTestCase): + + def test_service_info(self): + setup_module_args({ + "username": os.getenv('CM_USERNAME'), + "password": os.getenv('CM_PASSWORD'), + "host": os.getenv('CM_HOST'), + "port": "7180", + "verify_tls": "no", + "debug": "yes" + }) + + with pytest.raises(AnsibleExitJson) as e: + cm_service_info.main() + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 85568930ffa4a2c16f78f8459ca269f9d6fdfd09 Mon Sep 17 00:00:00 2001 From: rsuplina Date: Wed, 17 Jan 2024 08:22:43 +0000 Subject: [PATCH 2/2] Add Copyright year Signed-off-by: rsuplina --- plugins/modules/cm_service_info.py | 2 +- .../plugins/modules/cm_service_info/test_cm_service_info.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/cm_service_info.py b/plugins/modules/cm_service_info.py index ccc0c322..4c72d84f 100644 --- a/plugins/modules/cm_service_info.py +++ b/plugins/modules/cm_service_info.py @@ -1,4 +1,4 @@ -# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# Copyright 2024 Cloudera, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py b/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py index 2c05b009..d7d7af0b 100644 --- a/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py +++ b/tests/unit/plugins/modules/cm_service_info/test_cm_service_info.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# Copyright 2024 Cloudera, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.