Skip to content

Commit

Permalink
add support for operator policy
Browse files Browse the repository at this point in the history
This commit add support for rabbitmq *operator* policy.
An Example have been added to the inline documentation.

Related to ansible-collections#49
  • Loading branch information
matthieu-reussner-wday committed Oct 10, 2024
1 parent d2964f6 commit 03dce77
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
26 changes: 22 additions & 4 deletions plugins/modules/rabbitmq_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
type: str
default: present
choices: [present, absent]
policy_type:
description:
- The type of policy, either policy or operator_policy.
type: str
default: policy
choices: [policy, operator_policy]
'''

EXAMPLES = r'''
Expand All @@ -79,6 +85,16 @@
pattern: .*
tags:
ha-mode: all
- name: Add an Operator Policy
rabbitmq_policy:
name: Operator_policy
apply_to: queues
pattern: .*
policy_type: operator_policy
tags:
max-length:100000
overflow:reject-publish
'''

import json
Expand All @@ -100,6 +116,7 @@ def __init__(self, module, name):
self._priority = module.params['priority']
self._node = module.params['node']
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
self._policy_type = module.params['policy_type']

self._version = self._rabbit_version()

Expand Down Expand Up @@ -141,9 +158,9 @@ def _rabbit_version(self):
def _list_policies(self):
if self._version and self._version >= Version('3.7.9'):
# Remove first header line from policies list for version > 3.7.9
return self._exec(['list_policies'], True)[1:]
return self._exec(["list_%s" % self._policy_type], True)[1:]

return self._exec(['list_policies'], True)
return self._exec(["list_%s" % self._policy_type], True)

def has_modifications(self):
if self._pattern is None or self._tags is None:
Expand All @@ -166,7 +183,7 @@ def should_be_deleted(self):
for policy in self._list_policies())

def set(self):
args = ['set_policy']
args = ["set_%s" % self._policy_type ]
args.append(self._name)
args.append(self._pattern)
args.append(json.dumps(self._tags))
Expand All @@ -178,7 +195,7 @@ def set(self):
return self._exec(args)

def clear(self):
return self._exec(['clear_policy', self._name])
return self._exec(["clear_%s" % self._policy_type, self._name])

def _policy_check(self,
policy,
Expand Down Expand Up @@ -226,6 +243,7 @@ def main():
priority=dict(default='0'),
node=dict(default='rabbit'),
state=dict(default='present', choices=['present', 'absent']),
policy_type=dict(default='policy', choices=['policy_type', 'operator_policy']),
)

module = AnsibleModule(
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/targets/rabbitmq_policy/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- set_fact:
vhost_name: /policytest
ha_policy_name: HA
operator_policy_name: OP_POLICY

- name: Add host
rabbitmq_vhost:
Expand Down Expand Up @@ -90,3 +91,48 @@
assert:
that:
- remove_policy is not changed

- name: Add an Operator Policy
rabbitmq_policy:
name: "{{ operator_policy_name }}"
apply_to: queues
pattern: ".*"
policy_type: "operator_policy"
tags:
max-length:100000
overflow:reject-publish
vhost: "{{ vhost_name }}"
register: add_operator_policy

- name: Check that the operator policy is added
assert:
that:
- add_operator_policy is changed
- add_operator_policy is success

- name: Remove the operator policy
rabbitmq_policy:
name: "{{ operator_policy_name }}"
state: absent
vhost: "{{ vhost_name }}"
policy_type: "operator_policy"
register: remove_operator_policy

- name: Check that the policy is removed
assert:
that:
- remove_operator_policy is changed
- remove_operator_policy is success

- name: Remove the HA Policy (idempotency)
rabbitmq_policy:
name: "{{ operator_policy_name }}"
state: absent
vhost: "{{ vhost_name }}"
policy_type: "operator_policy"
register: remove_operator_policy

- name: Check that the operator policy is removed (idempotency)
assert:
that:
- remove_operator_policy is not changed

0 comments on commit 03dce77

Please sign in to comment.