diff --git a/changelogs/fragments/registration_module.yml b/changelogs/fragments/registration_module.yml new file mode 100644 index 00000000..8ea16e38 --- /dev/null +++ b/changelogs/fragments/registration_module.yml @@ -0,0 +1,4 @@ +--- +major_changes: + - Added registration and registration_info module. + (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/98) diff --git a/plugins/module_utils/rest_client.py b/plugins/module_utils/rest_client.py index 54fa2f66..20b2188a 100644 --- a/plugins/module_utils/rest_client.py +++ b/plugins/module_utils/rest_client.py @@ -131,11 +131,13 @@ class CachedRestClient(RestClient): # Use ONLY in case, that all task operations are read only. Should hould for all _info # modules. - def __init__(self, client): + def __init__(self, client: Client): super().__init__(client) self.cache = dict() - def list_records(self, endpoint, query=None, timeout=None): + def list_records( + self, endpoint: str, query: dict[Any, Any] = None, timeout: float = None + ) -> list[Any]: if endpoint in self.cache: records = self.cache[endpoint] else: diff --git a/plugins/modules/registration_info.py b/plugins/modules/registration_info.py new file mode 100644 index 00000000..45520437 --- /dev/null +++ b/plugins/modules/registration_info.py @@ -0,0 +1,86 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright: (c) 2022, XLAB Steampunk +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +DOCUMENTATION = r""" +module: registration_info + +author: + - Domen Dobnikar (@domen_dobnikar) +short_description: Retrieve information about cluster registration. +description: + - Retrieve information about cluster registration. +version_added: 1.1.0 +extends_documentation_fragment: + - scale_computing.hypercore.cluster_instance +seealso: [] +""" + +EXAMPLES = r""" +- name: Get registration info + scale_computing.hypercore.registration_info: + register: output + +- name: output the registration info + debug: + var: output +""" + +RETURN = r""" +record: + description: + - Cluster registration info + returned: success + type: dict + sample: + company_name: sample_company + contact: John Smith + email: john_smith@sgmail.com + phone: '777777777' +""" + +from ansible.module_utils.basic import AnsibleModule + +from ..module_utils import arguments, errors +from ..module_utils.client import Client +from ..module_utils.registration import Registration +from ..module_utils.typed_classes import TypedRegistrationToAnsible +from ..module_utils.rest_client import CachedRestClient + +from typing import Union + + +def run( + module: AnsibleModule, rest_client: CachedRestClient +) -> Union[TypedRegistrationToAnsible, None]: + registration_list = rest_client.list_records("/rest/v1/Registration") + if registration_list: + return Registration.from_hypercore(registration_list[0]).to_ansible() + return None + + +def main() -> None: + module = AnsibleModule( + supports_check_mode=True, + argument_spec=dict( + arguments.get_spec("cluster_instance"), + ), + ) + + try: + client = Client.get_client(module.params["cluster_instance"]) + rest_client = CachedRestClient(client) + record = run(module, rest_client) + module.exit_json(changed=False, record=record) + except errors.ScaleComputingError as e: + module.fail_json(msg=str(e)) + + +if __name__ == "__main__": + main() diff --git a/plugins/modules/user.py b/plugins/modules/user.py index 37c9744c..4a8d2181 100644 --- a/plugins/modules/user.py +++ b/plugins/modules/user.py @@ -264,7 +264,7 @@ def main() -> None: try: client = Client.get_client(module.params["cluster_instance"]) - rest_client = CachedRestClient(client) # type: ignore + rest_client = CachedRestClient(client) changed, record, diff = run(module, rest_client) module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: diff --git a/tests/integration/targets/registration/tasks/main.yml b/tests/integration/targets/registration/tasks/main.yml index 03d2c4a9..3a153ce2 100644 --- a/tests/integration/targets/registration/tasks/main.yml +++ b/tests/integration/targets/registration/tasks/main.yml @@ -5,8 +5,25 @@ SC_PASSWORD: "{{ sc_password }}" SC_TIMEOUT: "{{ sc_timeout }}" + vars: + default_company_name: "" + default_contact: "" + default_email: "" + default_phone: "" + # ----------------------------------Setup------------------------------------------------------------------------ block: + - name: GET Registration info (original info) + scale_computing.hypercore.api: + endpoint: rest/v1/Registration + action: get + register: registration_original + - ansible.builtin.set_fact: + default_company_name: "{{ registration_original.record.0.companyName }}" + default_contact: "{{ registration_original.record.0.contact }}" + default_email: "{{ registration_original.record.0.email }}" + default_phone: "{{ registration_original.record.0.phone }}" + - name: Delete current registration scale_computing.hypercore.registration: state: absent @@ -45,6 +62,19 @@ - registration_info.record.0.email == "jonh.doe@gmail.com" - registration_info.record.0.phone == "056777888" + - name: GET Registration info (check info module) + scale_computing.hypercore.registration_info: + register: result + - ansible.builtin.assert: + that: + - result is succeeded + - result is not changed + - result.record + - result.record.company_name == "My company" + - result.record.contact == "John Doe" + - result.record.email == "jonh.doe@gmail.com" + - result.record.phone == "056777888" + - name: Update registration scale_computing.hypercore.registration: company_name: New company @@ -77,6 +107,19 @@ - registration_info.record.0.email == "janez.novak@gmail.com" - registration_info.record.0.phone == "031777888" + - name: GET Registration info (check info module) after update + scale_computing.hypercore.registration_info: + register: result + - ansible.builtin.assert: + that: + - result is succeeded + - result is not changed + - result.record + - result.record.company_name == "New company" + - result.record.contact == "Janez Novak" + - result.record.email == "janez.novak@gmail.com" + - result.record.phone == "031777888" + - name: Delete current registration scale_computing.hypercore.registration: state: absent @@ -97,35 +140,42 @@ - registration_info is not changed - registration_info.record | length == 0 + - name: GET Registration info (check info module) after delete + scale_computing.hypercore.registration_info: + register: result + - ansible.builtin.assert: + that: + - result is succeeded + - result is not changed + - result.record == None + # Don't delete here, we don't want to have a pop-up window at the end of this test. - name: Re-create registration scale_computing.hypercore.registration: - company_name: My company - contact: John Doe - email: jonh.doe@gmail.com - phone: 056777888 + company_name: "{{ default_company_name }}" + contact: "{{ default_contact }}" + email: "{{ default_email }}" + phone: "{{ default_phone }}" state: present register: registration_create - ansible.builtin.assert: that: - registration_create is succeeded - registration_create is changed - - registration_create.record.company_name == "My company" - - registration_create.record.contact == "John Doe" - - registration_create.record.email == "jonh.doe@gmail.com" - - registration_create.record.phone == "056777888" + - registration_create.record.company_name == "{{ default_company_name }}" + - registration_create.record.contact == "{{ default_contact }}" + - registration_create.record.email == "{{ default_email }}" + - registration_create.record.phone == "{{ default_phone }}" - - name: GET Registration info FINAL - scale_computing.hypercore.api: - endpoint: rest/v1/Registration - action: get - register: registration_info + - name: GET Registration info (check info module) FINAL + scale_computing.hypercore.registration_info: + register: result - ansible.builtin.assert: that: - - registration_info is succeeded - - registration_info is not changed - - registration_info.record | length == 1 - - registration_info.record.0.companyName == "My company" - - registration_info.record.0.contact == "John Doe" - - registration_info.record.0.email == "jonh.doe@gmail.com" - - registration_info.record.0.phone == "056777888" + - result is succeeded + - result is not changed + - result.record + - result.record.company_name == "{{ default_company_name }}" + - result.record.contact == "{{ default_contact }}" + - result.record.email == "{{ default_email }}" + - result.record.phone == "{{ default_phone }}"