community.rabbitmq icon indicating copy to clipboard operation
community.rabbitmq copied to clipboard

Simple code refactoring needed

Open Andersson007 opened this issue 2 years ago • 2 comments

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
  • Feature Idea
COMPONENT NAME

Many modules

Andersson007 avatar Aug 13 '21 11:08 Andersson007