From 5be4adc434643d024186f964d437208e29677546 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Wed, 28 Jul 2021 17:49:37 +1200 Subject: [PATCH] ejabberd_user - refactoring and simplification (#3093) * ejabberd_user - refactoring and simplification * added changelog fragment * Update changelogs/fragments/3093-ejabberd_user-refactor.yaml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- .../3093-ejabberd_user-refactor.yaml | 2 + .../web_infrastructure/ejabberd_user.py | 59 +++++-------------- 2 files changed, 16 insertions(+), 45 deletions(-) create mode 100644 changelogs/fragments/3093-ejabberd_user-refactor.yaml diff --git a/changelogs/fragments/3093-ejabberd_user-refactor.yaml b/changelogs/fragments/3093-ejabberd_user-refactor.yaml new file mode 100644 index 0000000000..875ef6da71 --- /dev/null +++ b/changelogs/fragments/3093-ejabberd_user-refactor.yaml @@ -0,0 +1,2 @@ +bugfixes: + - ejabberd_user - replaced in-code check with ``required_if``, using ``get_bin_path()`` for the command, passing args to ``run_command()`` as list instead of string (https://github.com/ansible-collections/community.general/pull/3093). diff --git a/plugins/modules/web_infrastructure/ejabberd_user.py b/plugins/modules/web_infrastructure/ejabberd_user.py index be63c92080..e6cdd72b5e 100644 --- a/plugins/modules/web_infrastructure/ejabberd_user.py +++ b/plugins/modules/web_infrastructure/ejabberd_user.py @@ -72,11 +72,6 @@ import syslog from ansible.module_utils.basic import AnsibleModule -class EjabberdUserException(Exception): - """ Base exception for EjabberdUser class object """ - pass - - class EjabberdUser(object): """ This object represents a user resource for an ejabberd server. The object manages user creation and deletion using ejabberdctl. The following @@ -99,12 +94,7 @@ class EjabberdUser(object): changed. It will return True if the user does not match the supplied credentials and False if it does not """ - try: - options = [self.user, self.host, self.pwd] - (rc, out, err) = self.run_command('check_password', options) - except EjabberdUserException: - (rc, out, err) = (1, None, "required attribute(s) missing") - return rc + return self.run_command('check_password', [self.user, self.host, self.pwd]) @property def exists(self): @@ -112,12 +102,7 @@ class EjabberdUser(object): host specified. If the user exists True is returned, otherwise False is returned """ - try: - options = [self.user, self.host] - (rc, out, err) = self.run_command('check_account', options) - except EjabberdUserException: - (rc, out, err) = (1, None, "required attribute(s) missing") - return not bool(int(rc)) + return self.run_command('check_account', [self.user, self.host]) def log(self, entry): """ This method will log information to the local syslog facility """ @@ -129,44 +114,25 @@ class EjabberdUser(object): """ This method will run the any command specified and return the returns using the Ansible common module """ - if not all(options): - raise EjabberdUserException - - cmd = 'ejabberdctl %s ' % cmd - cmd += " ".join(options) - self.log('command: %s' % cmd) - return self.module.run_command(cmd.split()) + cmd = [self.module.get_bin_path('ejabberdctl'), cmd] + options + self.log('command: %s' % " ".join(cmd)) + return self.module.run_command(cmd) def update(self): """ The update method will update the credentials for the user provided """ - try: - options = [self.user, self.host, self.pwd] - (rc, out, err) = self.run_command('change_password', options) - except EjabberdUserException: - (rc, out, err) = (1, None, "required attribute(s) missing") - return (rc, out, err) + return self.run_command('change_password', [self.user, self.host, self.pwd]) def create(self): """ The create method will create a new user on the host with the password provided """ - try: - options = [self.user, self.host, self.pwd] - (rc, out, err) = self.run_command('register', options) - except EjabberdUserException: - (rc, out, err) = (1, None, "required attribute(s) missing") - return (rc, out, err) + return self.run_command('register', [self.user, self.host, self.pwd]) def delete(self): """ The delete method will delete the user from the host """ - try: - options = [self.user, self.host] - (rc, out, err) = self.run_command('unregister', options) - except EjabberdUserException: - (rc, out, err) = (1, None, "required attribute(s) missing") - return (rc, out, err) + return self.run_command('unregister', [self.user, self.host]) def main(): @@ -174,11 +140,14 @@ def main(): argument_spec=dict( host=dict(required=True, type='str'), username=dict(required=True, type='str'), - password=dict(default=None, type='str', no_log=True), + password=dict(type='str', no_log=True), state=dict(default='present', choices=['present', 'absent']), - logging=dict(default=False, type='bool') + logging=dict(default=False, type='bool') # deprecate in favour of c.g.syslogger? ), - supports_check_mode=True + required_if=[ + ('state', 'present', ['password']), + ], + supports_check_mode=True, ) obj = EjabberdUser(module)