1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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 <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2021-07-28 17:49:37 +12:00 committed by GitHub
parent 87baa5860a
commit 5be4adc434
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 45 deletions

View file

@ -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).

View file

@ -72,11 +72,6 @@ import syslog
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
class EjabberdUserException(Exception):
""" Base exception for EjabberdUser class object """
pass
class EjabberdUser(object): class EjabberdUser(object):
""" This object represents a user resource for an ejabberd server. The """ This object represents a user resource for an ejabberd server. The
object manages user creation and deletion using ejabberdctl. The following 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 changed. It will return True if the user does not match the supplied
credentials and False if it does not credentials and False if it does not
""" """
try: return self.run_command('check_password', [self.user, self.host, self.pwd])
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
@property @property
def exists(self): def exists(self):
@ -112,12 +102,7 @@ class EjabberdUser(object):
host specified. If the user exists True is returned, otherwise False host specified. If the user exists True is returned, otherwise False
is returned is returned
""" """
try: return self.run_command('check_account', [self.user, self.host])
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))
def log(self, entry): def log(self, entry):
""" This method will log information to the local syslog facility """ """ 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 """ This method will run the any command specified and return the
returns using the Ansible common module returns using the Ansible common module
""" """
if not all(options): cmd = [self.module.get_bin_path('ejabberdctl'), cmd] + options
raise EjabberdUserException self.log('command: %s' % " ".join(cmd))
return self.module.run_command(cmd)
cmd = 'ejabberdctl %s ' % cmd
cmd += " ".join(options)
self.log('command: %s' % cmd)
return self.module.run_command(cmd.split())
def update(self): def update(self):
""" The update method will update the credentials for the user provided """ The update method will update the credentials for the user provided
""" """
try: return self.run_command('change_password', [self.user, self.host, self.pwd])
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)
def create(self): def create(self):
""" The create method will create a new user on the host with the """ The create method will create a new user on the host with the
password provided password provided
""" """
try: return self.run_command('register', [self.user, self.host, self.pwd])
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)
def delete(self): def delete(self):
""" The delete method will delete the user from the host """ The delete method will delete the user from the host
""" """
try: return self.run_command('unregister', [self.user, self.host])
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)
def main(): def main():
@ -174,11 +140,14 @@ def main():
argument_spec=dict( argument_spec=dict(
host=dict(required=True, type='str'), host=dict(required=True, type='str'),
username=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']), 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) obj = EjabberdUser(module)