ansible-collections / community.rabbitmq

Manage RabbitMQ with Ansible
http://galaxy.ansible.com/community/rabbitmq
Other
31 stars 50 forks source link

Simple code refactoring needed #94

Open Andersson007 opened 3 years ago

Andersson007 commented 3 years ago
SUMMARY

Relates https://github.com/ansible-collections/community.rabbitmq/pull/89#issuecomment-887398334

the modules use the same function _exec. We could move it (and other shared things if exist), to plugins/module_utils/something.py, document it there, and use everything instead of having the code duplicated.

  1. Move the code to a library under plugins/module_utils
  2. It could be a parental class but i personally prefer composition.
  3. Or it could be just a function, say, rabbitmqctl_exec (obj, args, force_exec_in_check_mode=False) where obj is an argument via which the objects will pass themselves.

the class could look like:

class RabbitMQ():
    """Doc explaining purpose, args, etc."""
    def __init__(self, obj):
    self. obj = obj

    def exec(self, args, force_exec_in_check_mode=False):
        """Doc explaining purpose, args, etc."""
        if not self.obj.module.check_mode or (self.obj.module.check_mode and force_exec_in_check_mode):
            cmd = [self.obj._rabbitmqctl, '-q', '-n', self.obj.node]
            rc, out, err = self.obj.module.run_command(cmd + args, check_rc=True)
            return out.splitlines()
        return list()

in the target classes you're changing here in __init__ we could initialize `self.rabbitmq = RabbitMQ(self)

and then _exec() method could look like:

    def _exec(self, args, force_exec_in_check_mode=False):
        return self.rabbitmq.exec(args, force_exec_in_check_mode)
ISSUE TYPE
COMPONENT NAME

Many modules

Andersson007 commented 3 years ago

@aitorpazos i created this issue not to forget. if you're still interested in this, please put it explicitly here to avoid working in parallel

aitorpazos commented 3 years ago

Sure, busy atm but will address it soon.