From 396b94183d7fad1368d8678be1523a26b8c33022 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 10 Jun 2023 21:10:56 +0200 Subject: [PATCH] [PR #6658/42f7531f backport][stable-7] redhat_subscription: refactor of internal Rhsm class (#6667) redhat_subscription: refactor of internal Rhsm class (#6658) The two RegistrationBase & Rhsm classes were copied from the ones in the shared module_utils.redhat module; that said: - the versions here got improvements over the years - the RegistrationBase in module_utils.redhat is used only by the RHN modules, which are deprecated and slated for removal Hence, the classes here can be kept and simplified a bit: - fold the non-dummy content of RegistrationBase into Rhsm: there is no more need for the separate RegistrationBase base class - drop the init arguments "username", "password", and "token": the instance variables of them are not used anywhere, as the needed credentials (together with other variables) are passed to the register() method - create the Rhsm object later in main(), after the AnsibleModule creation and the uid check: this avoids the creation of Rhsm with a null module variable, changing it later There should be no behaviour change. (cherry picked from commit 42f7531f212c6401ca351de62bde0369916d8e58) Co-authored-by: Pino Toscano --- ...at_subscription-internal-rhsm-refactor.yml | 6 +++ plugins/modules/redhat_subscription.py | 44 ++++--------------- .../modules/test_redhat_subscription.py | 2 +- 3 files changed, 15 insertions(+), 37 deletions(-) create mode 100644 changelogs/fragments/6658-redhat_subscription-internal-rhsm-refactor.yml diff --git a/changelogs/fragments/6658-redhat_subscription-internal-rhsm-refactor.yml b/changelogs/fragments/6658-redhat_subscription-internal-rhsm-refactor.yml new file mode 100644 index 0000000000..ad0f4742a1 --- /dev/null +++ b/changelogs/fragments/6658-redhat_subscription-internal-rhsm-refactor.yml @@ -0,0 +1,6 @@ +minor_changes: + - | + redhat_subscription - the internal ``RegistrationBase`` class was folded + into the other internal ``Rhsm`` class, as the separation had no purpose + anymore + (https://github.com/ansible-collections/community.general/pull/6658). diff --git a/plugins/modules/redhat_subscription.py b/plugins/modules/redhat_subscription.py index 87c805fe92..685c07a968 100644 --- a/plugins/modules/redhat_subscription.py +++ b/plugins/modules/redhat_subscription.py @@ -334,32 +334,12 @@ from ansible.module_utils import distro SUBMAN_CMD = None -class RegistrationBase(object): +class Rhsm(object): REDHAT_REPO = "/etc/yum.repos.d/redhat.repo" - def __init__(self, module, username=None, password=None, token=None): + def __init__(self, module): self.module = module - self.username = username - self.password = password - self.token = token - - def configure(self): - raise NotImplementedError("Must be implemented by a sub-class") - - def enable(self): - # Remove any existing redhat.repo - if isfile(self.REDHAT_REPO): - unlink(self.REDHAT_REPO) - - def register(self): - raise NotImplementedError("Must be implemented by a sub-class") - - def unregister(self): - raise NotImplementedError("Must be implemented by a sub-class") - - def unsubscribe(self): - raise NotImplementedError("Must be implemented by a sub-class") def update_plugin_conf(self, plugin, enabled=True): plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin @@ -380,22 +360,15 @@ class RegistrationBase(object): fd.close() self.module.atomic_move(tmpfile, plugin_conf) - def subscribe(self, **kwargs): - raise NotImplementedError("Must be implemented by a sub-class") - - -class Rhsm(RegistrationBase): - def __init__(self, module, username=None, password=None, token=None): - RegistrationBase.__init__(self, module, username, password, token) - self.module = module - def enable(self): ''' Enable the system to receive updates from subscription-manager. This involves updating affected yum plugins and removing any conflicting yum repositories. ''' - RegistrationBase.enable(self) + # Remove any existing redhat.repo + if isfile(self.REDHAT_REPO): + unlink(self.REDHAT_REPO) self.update_plugin_conf('rhnplugin', False) self.update_plugin_conf('subscription-manager', True) @@ -1067,9 +1040,6 @@ class SysPurpose(object): def main(): - # Load RHSM configuration from file - rhsm = Rhsm(None) - # Note: the default values for parameters are: # 'type': 'str', 'default': None, 'required': False # So there is no need to repeat these values for each parameter. @@ -1144,7 +1114,9 @@ def main(): msg="Interacting with subscription-manager requires root permissions ('become: true')" ) - rhsm.module = module + # Load RHSM configuration from file + rhsm = Rhsm(module) + state = module.params['state'] username = module.params['username'] password = module.params['password'] diff --git a/tests/unit/plugins/modules/test_redhat_subscription.py b/tests/unit/plugins/modules/test_redhat_subscription.py index 4bf2729163..e80b04f734 100644 --- a/tests/unit/plugins/modules/test_redhat_subscription.py +++ b/tests/unit/plugins/modules/test_redhat_subscription.py @@ -22,7 +22,7 @@ def patch_redhat_subscription(mocker): """ Function used for mocking some parts of redhat_subscription module """ - mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.RegistrationBase.REDHAT_REPO') + mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.Rhsm.REDHAT_REPO') mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.isfile', return_value=False) mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.unlink', return_value=True) mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.AnsibleModule.get_bin_path',