Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redhat_subscription: refactor of internal Rhsm class #6658

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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).
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