mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Refactor rabbitmq_policy states to use test=true functionality correctly
Fixes #25658
This commit is contained in:
parent
1c6c394d0e
commit
1e8e86007c
2 changed files with 48 additions and 35 deletions
|
@ -75,33 +75,40 @@ def present(name,
|
|||
ret['comment'] = 'Policy {0} {1} is already present'.format(vhost, name)
|
||||
return ret
|
||||
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
if not policy:
|
||||
if not policy:
|
||||
ret['changes'].update({'old': {}, 'new': name})
|
||||
if __opts__['test']:
|
||||
ret['comment'] = 'Policy {0} {1} is set to be created'.format(vhost, name)
|
||||
elif updates:
|
||||
else:
|
||||
log.debug('Policy doesn\'t exist - Creating')
|
||||
result = __salt__['rabbitmq.set_policy'](vhost,
|
||||
name,
|
||||
pattern,
|
||||
definition,
|
||||
priority=priority,
|
||||
runas=runas)
|
||||
elif updates:
|
||||
ret['changes'].update({'old': policy, 'new': updates})
|
||||
if __opts__['test']:
|
||||
ret['comment'] = 'Policy {0} {1} is set to be updated'.format(vhost, name)
|
||||
else:
|
||||
changes = {'new': '', 'old': ''}
|
||||
if not policy:
|
||||
changes['new'] = policy
|
||||
log.debug(
|
||||
"Policy doesn't exist - Creating")
|
||||
result = __salt__['rabbitmq.set_policy'](
|
||||
vhost, name, pattern, definition, priority=priority, runas=runas)
|
||||
elif updates:
|
||||
changes['old'] = policy
|
||||
changes['new'] = updates
|
||||
else:
|
||||
log.debug('Policy exists but needs updating')
|
||||
result = __salt__['rabbitmq.set_policy'](
|
||||
vhost, name, pattern, definition, priority=priority, runas=runas)
|
||||
result = __salt__['rabbitmq.set_policy'](vhost,
|
||||
name,
|
||||
pattern,
|
||||
definition,
|
||||
priority=priority,
|
||||
runas=runas)
|
||||
|
||||
if 'Error' in result:
|
||||
ret['result'] = False
|
||||
ret['comment'] = result['Error']
|
||||
elif 'Set' in result:
|
||||
ret['comment'] = result['Set']
|
||||
ret['changes'] = changes
|
||||
if 'Error' in result:
|
||||
ret['result'] = False
|
||||
ret['comment'] = result['Error']
|
||||
elif ret['changes'] == {}:
|
||||
ret['comment'] = '\'{0}\' is already in the desired state.'.format(name)
|
||||
elif __opts__['test']:
|
||||
ret['result'] = None
|
||||
elif 'Set' in result:
|
||||
ret['comment'] = result['Set']
|
||||
|
||||
return ret
|
||||
|
||||
|
@ -125,19 +132,23 @@ def absent(name,
|
|||
vhost, name, runas=runas)
|
||||
|
||||
if not policy_exists:
|
||||
ret['comment'] = 'Policy {0} {1} is not present'.format(vhost, name)
|
||||
ret['comment'] = 'Policy \'{0} {1}\' is not present.'.format(vhost, name)
|
||||
return ret
|
||||
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] = 'Removing policy {0} {1}'.format(vhost, name)
|
||||
else:
|
||||
if not __opts__['test']:
|
||||
result = __salt__['rabbitmq.delete_policy'](vhost, name, runas=runas)
|
||||
if 'Error' in result:
|
||||
ret['result'] = False
|
||||
ret['comment'] = result['Error']
|
||||
return ret
|
||||
elif 'Deleted' in result:
|
||||
ret['comment'] = 'Deleted'
|
||||
ret['changes'] = {'new': '', 'old': name}
|
||||
|
||||
# If we've reached this far before returning, we have changes.
|
||||
ret['changes'] = {'new': '', 'old': name}
|
||||
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] = 'Policy \'{0} {1}\' will be removed.'.format(vhost, name)
|
||||
|
||||
return ret
|
||||
|
|
|
@ -56,8 +56,9 @@ class RabbitmqPolicyTestCase(TestCase):
|
|||
definition), ret)
|
||||
|
||||
with patch.dict(rabbitmq_policy.__opts__, {'test': True}):
|
||||
comt = ('Policy / HA is set to be created')
|
||||
ret.update({'comment': comt, 'result': None})
|
||||
comment = 'Policy / HA is set to be created'
|
||||
changes = {'new': 'HA', 'old': {}}
|
||||
ret.update({'comment': comment, 'result': None, 'changes': changes})
|
||||
self.assertDictEqual(rabbitmq_policy.present(name, pattern,
|
||||
definition), ret)
|
||||
|
||||
|
@ -77,13 +78,14 @@ class RabbitmqPolicyTestCase(TestCase):
|
|||
mock = MagicMock(side_effect=[False, True])
|
||||
with patch.dict(rabbitmq_policy.__salt__,
|
||||
{'rabbitmq.policy_exists': mock}):
|
||||
comt = ('Policy / HA is not present')
|
||||
ret.update({'comment': comt})
|
||||
comment = 'Policy \'/ HA\' is not present.'
|
||||
ret.update({'comment': comment})
|
||||
self.assertDictEqual(rabbitmq_policy.absent(name), ret)
|
||||
|
||||
with patch.dict(rabbitmq_policy.__opts__, {'test': True}):
|
||||
comt = ('Removing policy / HA')
|
||||
ret.update({'comment': comt, 'result': None})
|
||||
comment = 'Policy \'/ HA\' will be removed.'
|
||||
changes = {'new': '', 'old': 'HA'}
|
||||
ret.update({'comment': comment, 'result': None, 'changes': changes})
|
||||
self.assertDictEqual(rabbitmq_policy.absent(name), ret)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue