From 81f1e3c4d69583b028688da9a6c77f2772f932c7 Mon Sep 17 00:00:00 2001 From: Matthieu Reussner Date: Thu, 10 Oct 2024 14:11:26 +0000 Subject: [PATCH] add support for operator policy This commit add support for rabbitmq *operator* policy. An Example have been added to the inline documentation. Related to https://github.com/ansible-collections/community.rabbitmq/issues/49 --- plugins/modules/rabbitmq_policy.py | 31 +++++++++++-- .../targets/rabbitmq_policy/tasks/tests.yml | 46 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/plugins/modules/rabbitmq_policy.py b/plugins/modules/rabbitmq_policy.py index 441a72c..c845947 100644 --- a/plugins/modules/rabbitmq_policy.py +++ b/plugins/modules/rabbitmq_policy.py @@ -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''' @@ -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 @@ -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() @@ -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: @@ -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)) @@ -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, @@ -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( diff --git a/tests/integration/targets/rabbitmq_policy/tasks/tests.yml b/tests/integration/targets/rabbitmq_policy/tasks/tests.yml index ca13fa7..67c2d1e 100644 --- a/tests/integration/targets/rabbitmq_policy/tasks/tests.yml +++ b/tests/integration/targets/rabbitmq_policy/tasks/tests.yml @@ -2,6 +2,7 @@ - set_fact: vhost_name: /policytest ha_policy_name: HA + operator_policy_name: OP_POLICY - name: Add host rabbitmq_vhost: @@ -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