From c69551361b813d607f4ed459f26d5a4f8fa5ee8e Mon Sep 17 00:00:00 2001 From: rsuplina Date: Thu, 11 Jan 2024 18:07:10 +0000 Subject: [PATCH 1/2] Add cm_trial module Signed-off-by: rsuplina --- plugins/modules/cm_trial.py | 133 ++++++++++++++++++ .../plugins/modules/cm_trial/test_cm_trial.py | 45 ++++++ 2 files changed, 178 insertions(+) create mode 100644 plugins/modules/cm_trial.py create mode 100644 tests/unit/plugins/modules/cm_trial/test_cm_trial.py diff --git a/plugins/modules/cm_trial.py b/plugins/modules/cm_trial.py new file mode 100644 index 00000000..84ea9427 --- /dev/null +++ b/plugins/modules/cm_trial.py @@ -0,0 +1,133 @@ +# 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. +# 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.rest import ApiException +from cm_client import ClouderaManagerResourceApi + +ANSIBLE_METADATA = { + "metadata_version": "1.1", + "status": ["preview"], + "supported_by": "community", +} + +DOCUMENTATION = r""" +--- +module: cm_trial +short_description: Activate the trial license of Cloudera Manager +description: + - Checking if the trial license is already activated. + - Activating the trial license if it is not already activated. + - Return information about the trial license. +author: + - "Ronald Suplina (@rsuplina)" +requirements: + - cm_client +""" + +EXAMPLES = r""" +--- +- name: Activate the trial license of Cloudera Manager + cloudera.cluster.cm_trial: + host: example.cloudera.com + port: "7180" + username: "jane_smith" + password: "S&peR4Ec*re" +""" + +RETURN = r""" +--- +cloudera_manager: + description: Details about trial license + type: dict + contains: + owner: + description: Type of the license + type: str + returned: optional + uuid: + description: Unique ID of trial license + type: bool + returned: optional + expiration: + description: Expiration date of trial license + type: date + returned: optional + features: + description: List of features within the trial license + type: list + returned: optional + deactivation_date: + description: Date until trial is active + type: date + returned: optional + start_date: + description: trial activation date + type: date + returned: optional +""" + + +class ClouderaTrial(ClouderaManagerModule): + def __init__(self, module): + super(ClouderaTrial, self).__init__(module) + self.process() + + @ClouderaManagerModule.handle_process + def process(self): + + api_instance = ClouderaManagerResourceApi(self.api_client) + + try: + get_trial_state_request = api_instance.read_license().to_dict() + + if get_trial_state_request: + self.cm_trial_output = get_trial_state_request + self.changed = False + + except ApiException as e: + if e.status == 404: + api_instance.begin_trial() + get_trial_state_request = api_instance.read_license().to_dict() + self.cm_trial_output = get_trial_state_request + self.changed = True + +def main(): + module = ClouderaManagerModule.ansible_module( + + argument_spec= + dict(), + supports_check_mode=True + ) + + result = ClouderaTrial(module) + + changed = result.changed + + output = dict( + changed=changed, + cloudera_manager=result.cm_trial_output, + ) + + 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_trial/test_cm_trial.py b/tests/unit/plugins/modules/cm_trial/test_cm_trial.py new file mode 100644 index 00000000..5dd22d92 --- /dev/null +++ b/tests/unit/plugins/modules/cm_trial/test_cm_trial.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +# 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. +# 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 logging +import pytest + +from ansible_collections.cloudera.cluster.plugins.modules import cm_trial +from ansible_collections.cloudera.cluster.tests.unit import AnsibleExitJson, AnsibleFailJson + +LOG = logging.getLogger(__name__) + +def test_pytest_cm_trial(module_args): + module_args( + { + "username": os.getenv('CM_USERNAME'), + "password": os.getenv('CM_PASSWORD'), + "host": os.getenv('CM_HOST'), + "port": os.getenv('CM_PORT'), + "verify_tls": "no", + "debug": "no", + } + ) + + with pytest.raises(AnsibleExitJson) as e: + cm_trial.main() + + # LOG.info(str(e.value)) + LOG.info(str(e.value.cloudera_manager)) From 81554a4ceb2f1a1917286b134bc2e0109bfa49bf Mon Sep 17 00:00:00 2001 From: rsuplina Date: Wed, 17 Jan 2024 09:48:03 +0000 Subject: [PATCH 2/2] Add Requested changes Signed-off-by: rsuplina --- .../{cm_trial.py => cm_trial_license.py} | 36 +++++++++---------- ...t_cm_trial.py => test_cm_trial_license.py} | 10 +++--- 2 files changed, 23 insertions(+), 23 deletions(-) rename plugins/modules/{cm_trial.py => cm_trial_license.py} (80%) rename tests/unit/plugins/modules/cm_trial/{test_cm_trial.py => test_cm_trial_license.py} (87%) diff --git a/plugins/modules/cm_trial.py b/plugins/modules/cm_trial_license.py similarity index 80% rename from plugins/modules/cm_trial.py rename to plugins/modules/cm_trial_license.py index 84ea9427..e686e794 100644 --- a/plugins/modules/cm_trial.py +++ b/plugins/modules/cm_trial_license.py @@ -26,7 +26,7 @@ DOCUMENTATION = r""" --- -module: cm_trial +module: cm_trial_license short_description: Activate the trial license of Cloudera Manager description: - Checking if the trial license is already activated. @@ -41,7 +41,7 @@ EXAMPLES = r""" --- - name: Activate the trial license of Cloudera Manager - cloudera.cluster.cm_trial: + cloudera.cluster.cm_trial_license: host: example.cloudera.com port: "7180" username: "jane_smith" @@ -88,22 +88,22 @@ def __init__(self, module): @ClouderaManagerModule.handle_process def process(self): - - api_instance = ClouderaManagerResourceApi(self.api_client) - - try: - get_trial_state_request = api_instance.read_license().to_dict() - - if get_trial_state_request: - self.cm_trial_output = get_trial_state_request - self.changed = False - - except ApiException as e: - if e.status == 404: - api_instance.begin_trial() - get_trial_state_request = api_instance.read_license().to_dict() - self.cm_trial_output = get_trial_state_request - self.changed = True + if not self.module.check_mode: + api_instance = ClouderaManagerResourceApi(self.api_client) + + try: + get_trial_state_request = api_instance.read_license().to_dict() + + if get_trial_state_request: + self.cm_trial_output = get_trial_state_request + self.changed = False + + except ApiException as e: + if e.status == 404: + api_instance.begin_trial() + get_trial_state_request = api_instance.read_license().to_dict() + self.cm_trial_output = get_trial_state_request + self.changed = True def main(): module = ClouderaManagerModule.ansible_module( diff --git a/tests/unit/plugins/modules/cm_trial/test_cm_trial.py b/tests/unit/plugins/modules/cm_trial/test_cm_trial_license.py similarity index 87% rename from tests/unit/plugins/modules/cm_trial/test_cm_trial.py rename to tests/unit/plugins/modules/cm_trial/test_cm_trial_license.py index 5dd22d92..afb1a6ba 100644 --- a/tests/unit/plugins/modules/cm_trial/test_cm_trial.py +++ b/tests/unit/plugins/modules/cm_trial/test_cm_trial_license.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2024 Cloudera, Inc. All Rights Reserved. +# 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. @@ -21,25 +21,25 @@ import logging import pytest -from ansible_collections.cloudera.cluster.plugins.modules import cm_trial +from ansible_collections.cloudera.cluster.plugins.modules import cm_trial_license from ansible_collections.cloudera.cluster.tests.unit import AnsibleExitJson, AnsibleFailJson LOG = logging.getLogger(__name__) -def test_pytest_cm_trial(module_args): +def test_pytest_cm_trial_license(module_args): module_args( { "username": os.getenv('CM_USERNAME'), "password": os.getenv('CM_PASSWORD'), "host": os.getenv('CM_HOST'), - "port": os.getenv('CM_PORT'), + "port": "7180", "verify_tls": "no", "debug": "no", } ) with pytest.raises(AnsibleExitJson) as e: - cm_trial.main() + cm_trial_license.main() # LOG.info(str(e.value)) LOG.info(str(e.value.cloudera_manager))