1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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.
This commit is contained in:
Pino Toscano 2023-06-10 15:03:34 +02:00 committed by GitHub
parent 78c42def04
commit 42f7531f21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 37 deletions

View file

@ -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).

View file

@ -334,32 +334,12 @@ from ansible.module_utils import distro
SUBMAN_CMD = None SUBMAN_CMD = None
class RegistrationBase(object): class Rhsm(object):
REDHAT_REPO = "/etc/yum.repos.d/redhat.repo" 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.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): def update_plugin_conf(self, plugin, enabled=True):
plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin
@ -380,22 +360,15 @@ class RegistrationBase(object):
fd.close() fd.close()
self.module.atomic_move(tmpfile, plugin_conf) 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): def enable(self):
''' '''
Enable the system to receive updates from subscription-manager. Enable the system to receive updates from subscription-manager.
This involves updating affected yum plugins and removing any This involves updating affected yum plugins and removing any
conflicting yum repositories. 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('rhnplugin', False)
self.update_plugin_conf('subscription-manager', True) self.update_plugin_conf('subscription-manager', True)
@ -1067,9 +1040,6 @@ class SysPurpose(object):
def main(): def main():
# Load RHSM configuration from file
rhsm = Rhsm(None)
# Note: the default values for parameters are: # Note: the default values for parameters are:
# 'type': 'str', 'default': None, 'required': False # 'type': 'str', 'default': None, 'required': False
# So there is no need to repeat these values for each parameter. # 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')" 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'] state = module.params['state']
username = module.params['username'] username = module.params['username']
password = module.params['password'] password = module.params['password']

View file

@ -22,7 +22,7 @@ def patch_redhat_subscription(mocker):
""" """
Function used for mocking some parts of redhat_subscription module 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.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.unlink', return_value=True)
mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.AnsibleModule.get_bin_path', mocker.patch('ansible_collections.community.general.plugins.modules.redhat_subscription.AnsibleModule.get_bin_path',