The setting of params via dict or string by tags

Updated per @mpdehaan suggestions.
* Rather than passing in JSON, supply the policy options as a hash
  or string.
* Bumped version_added to 1.5.
reviewable/pr18780/r1
John Dewey 11 years ago
parent 52b125a9da
commit cfa588c1a3

@ -25,7 +25,7 @@ module: rabbitmq_policy
short_description: Manage the state of policies in RabbitMQ. short_description: Manage the state of policies in RabbitMQ.
description: description:
- Manage the state of a virtual host in RabbitMQ. - Manage the state of a virtual host in RabbitMQ.
version_added: "1.0" version_added: "1.5"
author: John Dewey author: John Dewey
options: options:
name: name:
@ -43,9 +43,9 @@ options:
- A regex of queues to apply the policy to. - A regex of queues to apply the policy to.
required: true required: true
default: null default: null
params: tags:
description: description:
- A json dict describing the policy. - A dict or string describing the policy.
required: true required: true
default: null default: null
priority: priority:
@ -66,8 +66,14 @@ options:
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Ensure that the / vhost contains the HA policy. - name: ensure the default vhost contains the HA policy via a dict
- rabbitmq_policy: name=/ params='{"ha-mode":"all"}' policy=HA pattern='.*' rabbitmq_policy: name=HA pattern='.*'
args:
tags:
"ha-mode": all
- name: ensure the default vhost contains the HA policy
rabbitmq_policy: name=HA pattern='.*' tags="ha-mode=all"
''' '''
class RabbitMqPolicy(object): class RabbitMqPolicy(object):
def __init__(self, module, name): def __init__(self, module, name):
@ -75,7 +81,7 @@ class RabbitMqPolicy(object):
self._name = name self._name = name
self._vhost = module.params['vhost'] self._vhost = module.params['vhost']
self._pattern = module.params['pattern'] self._pattern = module.params['pattern']
self._params = module.params['params'] self._tags = module.params['tags']
self._priority = module.params['priority'] self._priority = module.params['priority']
self._node = module.params['node'] self._node = module.params['node']
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True) self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
@ -98,11 +104,22 @@ class RabbitMqPolicy(object):
return True return True
return False return False
def _dump_json(self, d):
import json
return json.dumps(d)
def _make_params(self):
if type(self._tags) is dict:
return self._dump_json(self._tags)
else:
d = dict([x.split('=') for x in self._tags.split()])
return self._dump_json(d)
def set(self): def set(self):
args = ['set_policy'] args = ['set_policy']
args.append(self._name) args.append(self._name)
args.append(self._pattern) args.append(self._pattern)
args.append(self._params) args.append(self._make_params())
args.append('--priority') args.append('--priority')
args.append(self._priority) args.append(self._priority)
return self._exec(args) return self._exec(args)
@ -116,7 +133,7 @@ def main():
name=dict(required=True), name=dict(required=True),
vhost=dict(default='/'), vhost=dict(default='/'),
pattern=dict(required=True), pattern=dict(required=True),
params=dict(required=True), tags=dict(required=True),
priority=dict(default='0'), priority=dict(default='0'),
node=dict(default='rabbit'), node=dict(default='rabbit'),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),

Loading…
Cancel
Save