Skip to content

Commit

Permalink
redhat_subscription: refactor of internal Rhsm class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptoscano committed Jun 9, 2023
1 parent 61b8897 commit 2d81aec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
44 changes: 8 additions & 36 deletions plugins/modules/redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,32 +334,12 @@
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
Expand All @@ -380,22 +360,15 @@ def update_plugin_conf(self, plugin, enabled=True):
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)

Expand Down Expand Up @@ -1067,9 +1040,6 @@ def _read_syspurpose(self):

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.
Expand Down Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2d81aec

Please sign in to comment.