mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
processes and returns better output for rabbitmq module
- also: fix test mode of rabbitmq_user
This commit is contained in:
parent
39a8f30f06
commit
cd0212e8ed
2 changed files with 120 additions and 61 deletions
|
@ -55,6 +55,24 @@ def _get_rabbitmq_plugin():
|
|||
return rabbitmq
|
||||
|
||||
|
||||
def _output_to_dict(cmdoutput, values_mapper=None):
|
||||
'''Convert rabbitmqctl output to a dict of data
|
||||
cmdoutput: string output of rabbitmqctl commands
|
||||
values_mapper: function object to process the values part of each line
|
||||
'''
|
||||
ret = {}
|
||||
if values_mapper is None:
|
||||
values_mapper = lambda string: string.split('\t')
|
||||
|
||||
# remove first and last line: Listing ... - ...done
|
||||
data_rows = cmdoutput.splitlines()[1:-1]
|
||||
for row in data_rows:
|
||||
key, values = row.split('\t', 1)
|
||||
ret[key] = values_mapper(values)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def list_users(runas=None):
|
||||
'''
|
||||
Return a list of users based off of rabbitmqctl user_list.
|
||||
|
@ -65,19 +83,14 @@ def list_users(runas=None):
|
|||
|
||||
salt '*' rabbitmq.list_users
|
||||
'''
|
||||
ret = {}
|
||||
if runas is None:
|
||||
runas = salt.utils.get_user()
|
||||
res = __salt__['cmd.run']('rabbitmqctl list_users',
|
||||
runas=runas)
|
||||
for line in res.splitlines():
|
||||
if '...' not in line or line == '\n':
|
||||
parts = line.split('\t')
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
user, properties = parts[0], parts[1]
|
||||
ret[user] = properties
|
||||
return ret
|
||||
|
||||
# func to get tags from string such as "[admin, monitoring]"
|
||||
func = lambda string: set(string[1:-1].split(','))
|
||||
return _output_to_dict(res, func)
|
||||
|
||||
|
||||
def list_vhosts(runas=None):
|
||||
|
@ -94,9 +107,9 @@ def list_vhosts(runas=None):
|
|||
runas = salt.utils.get_user()
|
||||
res = __salt__['cmd.run']('rabbitmqctl list_vhosts',
|
||||
runas=runas)
|
||||
lines = res.splitlines()
|
||||
vhost_list = [line for line in lines if '...' not in line]
|
||||
return vhost_list
|
||||
|
||||
# remove first and last line: Listing ... - ...done
|
||||
return res.splitlines()[1:-1]
|
||||
|
||||
|
||||
def user_exists(name, runas=None):
|
||||
|
@ -311,7 +324,8 @@ def list_permissions(vhost, runas=None):
|
|||
'rabbitmqctl list_permissions -p {0}'.format(vhost),
|
||||
python_shell=False,
|
||||
runas=runas)
|
||||
return [r.split('\t') for r in res.splitlines()]
|
||||
|
||||
return _output_to_dict(res)
|
||||
|
||||
|
||||
def list_user_permissions(name, runas=None):
|
||||
|
@ -330,7 +344,8 @@ def list_user_permissions(name, runas=None):
|
|||
'rabbitmqctl list_user_permissions {0}'.format(name),
|
||||
python_shell=False,
|
||||
runas=runas)
|
||||
return [r.split('\t') for r in res.splitlines()]
|
||||
|
||||
return _output_to_dict(res)
|
||||
|
||||
|
||||
def set_user_tags(name, tags, runas=None):
|
||||
|
@ -344,6 +359,10 @@ def set_user_tags(name, tags, runas=None):
|
|||
'''
|
||||
if runas is None:
|
||||
runas = salt.utils.get_user()
|
||||
|
||||
if tags and isinstance(tags, (list, tuple)):
|
||||
tags = ' '.join(tags)
|
||||
|
||||
res = __salt__['cmd.run'](
|
||||
'rabbitmqctl set_user_tags {0} {1}'.format(name, tags),
|
||||
python_shell=False,
|
||||
|
|
|
@ -36,6 +36,39 @@ def __virtual__():
|
|||
return salt.utils.which('rabbitmqctl') is not None
|
||||
|
||||
|
||||
def _check_perms_changes(name, newperms):
|
||||
'''
|
||||
Whether Rabbitmq user's permissions need to be changed
|
||||
'''
|
||||
if not newperms:
|
||||
return False
|
||||
|
||||
existing_perms = __salt__['rabbitmq.list_user_permissions'](name)
|
||||
|
||||
perm_need_change = False
|
||||
for vhost_perms in newperms:
|
||||
for vhost, perms in vhost_perms.iteritems():
|
||||
if vhost in existing_perms:
|
||||
if perms != existing_perms[vhost]:
|
||||
perm_need_change = True
|
||||
else:
|
||||
perm_need_change = True
|
||||
|
||||
return perm_need_change
|
||||
|
||||
|
||||
def _check_tags_changes(name, newtags):
|
||||
'''
|
||||
Whether Rabbitmq user's tags need to be changed
|
||||
'''
|
||||
if newtags:
|
||||
if isinstance(newtags, str):
|
||||
newtags = newtags.split()
|
||||
return __salt__['rabbitmq.list_users']()[name] - set(newtags)
|
||||
else:
|
||||
return []
|
||||
|
||||
|
||||
def present(name,
|
||||
password=None,
|
||||
force=False,
|
||||
|
@ -64,39 +97,67 @@ def present(name,
|
|||
|
||||
user_exists = __salt__['rabbitmq.user_exists'](name, runas=runas)
|
||||
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
|
||||
if user_exists:
|
||||
if force:
|
||||
ret['comment'] = 'User {0} is set to be updated'
|
||||
else:
|
||||
ret['comment'] = 'User {0} already presents'
|
||||
else:
|
||||
ret['comment'] = 'User {0} is set to be created'
|
||||
|
||||
ret['comment'] = ret['comment'].format(name)
|
||||
return ret
|
||||
|
||||
if user_exists and not force:
|
||||
if user_exists and not any((force, perms, tags)):
|
||||
log.debug('User exists, and force is not set - Abandoning')
|
||||
ret['comment'] = 'User {0} already presents'.format(name)
|
||||
return ret
|
||||
else:
|
||||
changes = {'old': '', 'new': ''}
|
||||
|
||||
# Get it into the correct format
|
||||
if tags and isinstance(tags, (list, tuple)):
|
||||
tags = ' '.join(tags)
|
||||
if not user_exists:
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] = 'User {0} is set to be created'.format(name)
|
||||
return ret
|
||||
|
||||
def _set_tags_and_perms(tags, perms):
|
||||
if tags:
|
||||
result.update(__salt__['rabbitmq.set_user_tags'](
|
||||
name, tags, runas=runas)
|
||||
)
|
||||
changes['new'] += 'Set tags: {0}\n'.format(tags)
|
||||
for element in perms:
|
||||
for vhost, perm in element.items():
|
||||
log.debug("User doesn't exist - Creating")
|
||||
result = __salt__['rabbitmq.add_user'](
|
||||
name, password, runas=runas)
|
||||
else:
|
||||
log.debug('RabbitMQ user exists')
|
||||
if force:
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
|
||||
if password is not None:
|
||||
if __opts__['test']:
|
||||
ret['comment'] = ('User {0}\'s password is '
|
||||
'set to be updated'.format(name))
|
||||
return ret
|
||||
|
||||
result = __salt__['rabbitmq.change_password'](
|
||||
name, password, runas=runas)
|
||||
changes['new'] = 'Set password.\n'
|
||||
else:
|
||||
log.debug('Password is not set - Clearing password')
|
||||
if __opts__['test']:
|
||||
ret['comment'] = ('User {0}\'s password is '
|
||||
'set to be removed'.format(name))
|
||||
return ret
|
||||
|
||||
result = __salt__['rabbitmq.clear_password'](
|
||||
name, runas=runas)
|
||||
changes['old'] += 'Removed password.\n'
|
||||
|
||||
if _check_tags_changes(name, tags):
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] += ('Tags for user {0} '
|
||||
'is set to be changed'.format(name))
|
||||
return ret
|
||||
result.update(__salt__['rabbitmq.set_user_tags'](
|
||||
name, tags, runas=runas)
|
||||
)
|
||||
changes['new'] += 'Set tags: {0}\n'.format(tags)
|
||||
|
||||
if _check_perms_changes(name, perms):
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] += ('Permissions for user {0} '
|
||||
'is set to be changed'.format(name))
|
||||
return ret
|
||||
for vhost_perm in perms:
|
||||
for vhost, perm in vhost_perm.iteritems():
|
||||
result.update(__salt__['rabbitmq.set_permissions'](
|
||||
vhost, name, perm[0], perm[1], perm[2], runas)
|
||||
)
|
||||
|
@ -104,27 +165,6 @@ def present(name,
|
|||
'Set permissions {0} for vhost {1}'
|
||||
).format(perm, vhost)
|
||||
|
||||
if not user_exists:
|
||||
log.debug(
|
||||
"User doesn't exist - Creating")
|
||||
result = __salt__['rabbitmq.add_user'](
|
||||
name, password, runas=runas)
|
||||
|
||||
_set_tags_and_perms(tags, perms)
|
||||
else:
|
||||
log.debug('RabbitMQ user exists and force is set - Overriding')
|
||||
if password is not None:
|
||||
result = __salt__['rabbitmq.change_password'](
|
||||
name, password, runas=runas)
|
||||
changes['new'] = 'Set password.\n'
|
||||
else:
|
||||
log.debug('Password is not set - Clearing password')
|
||||
result = __salt__['rabbitmq.clear_password'](
|
||||
name, runas=runas)
|
||||
changes['old'] += 'Removed password.\n'
|
||||
|
||||
_set_tags_and_perms(tags, perms)
|
||||
|
||||
if 'Error' in result:
|
||||
ret['result'] = False
|
||||
ret['comment'] = result['Error']
|
||||
|
|
Loading…
Add table
Reference in a new issue