Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for operator policy #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 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
community.rabbitmq.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 @@ -139,11 +156,16 @@ def _rabbit_version(self):
return None

def _list_policies(self):
command = {
"policy": "list_policies",
"operator_policy": "list_operator_policies"
}.get(self._policy_type)

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([command], True)[1:]

return self._exec(['list_policies'], True)
return self._exec([command], True)

def has_modifications(self):
if self._pattern is None or self._tags is None:
Expand All @@ -166,7 +188,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 +200,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 +248,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