| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Manuel Sousa <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: rabbitmq_queue |
| 14 |
author: Manuel Sousa (@manuel-sousa) |
| 15 |
|
| 16 |
short_description: Manage rabbitMQ queues |
| 17 |
description: |
| 18 |
- This module uses rabbitMQ Rest API to create/delete queues. |
| 19 |
- Due to limitations in the API, it cannot modify existing queues. |
| 20 |
requirements: [ "requests >= 1.0.0" ] |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the queue. |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Whether the queue should be present or absent. |
| 30 |
type: str |
| 31 |
choices: [ "present", "absent" ] |
| 32 |
default: present |
| 33 |
durable: |
| 34 |
description: |
| 35 |
- whether queue is durable or not. |
| 36 |
type: bool |
| 37 |
default: true |
| 38 |
auto_delete: |
| 39 |
description: |
| 40 |
- if the queue should delete itself after all queues/queues unbound from it. |
| 41 |
type: bool |
| 42 |
default: false |
| 43 |
message_ttl: |
| 44 |
description: |
| 45 |
- How long a message can live in queue before it is discarded (milliseconds). |
| 46 |
type: int |
| 47 |
auto_expires: |
| 48 |
description: |
| 49 |
- How long a queue can be unused before it is automatically deleted (milliseconds). |
| 50 |
type: int |
| 51 |
max_length: |
| 52 |
description: |
| 53 |
- How many messages can the queue contain before it starts rejecting. |
| 54 |
type: int |
| 55 |
dead_letter_exchange: |
| 56 |
description: |
| 57 |
- Optional name of an exchange to which messages will be republished if they |
| 58 |
- are rejected or expire. |
| 59 |
type: str |
| 60 |
dead_letter_routing_key: |
| 61 |
description: |
| 62 |
- Optional replacement routing key to use when a message is dead-lettered. |
| 63 |
- Original routing key will be used if unset. |
| 64 |
type: str |
| 65 |
max_priority: |
| 66 |
description: |
| 67 |
- Maximum number of priority levels for the queue to support. |
| 68 |
- If not set, the queue will not support message priorities. |
| 69 |
- Larger numbers indicate higher priority. |
| 70 |
type: int |
| 71 |
arguments: |
| 72 |
description: |
| 73 |
- extra arguments for queue. If defined this argument is a key/value dictionary |
| 74 |
- Arguments here take precedence over parameters. If both are defined, the |
| 75 |
argument will be used. |
| 76 |
type: dict |
| 77 |
default: {} |
| 78 |
extends_documentation_fragment: |
| 79 |
- community.rabbitmq.rabbitmq |
| 80 |
|
| 81 |
''' |
| 82 |
|
| 83 |
EXAMPLES = r''' |
| 84 |
- name: Create a queue |
| 85 |
community.rabbitmq.rabbitmq_queue: |
| 86 |
name: myQueue |
| 87 |
|
| 88 |
- name: Create a queue on remote host |
| 89 |
community.rabbitmq.rabbitmq_queue: |
| 90 |
name: myRemoteQueue |
| 91 |
login_user: user |
| 92 |
login_password: secret |
| 93 |
login_host: remote.example.org |
| 94 |
|
| 95 |
# You may specify different types of queues using the arguments parameter. |
| 96 |
- name: Create RabbitMQ stream |
| 97 |
become: yes |
| 98 |
community.rabbitmq.rabbitmq_queue: |
| 99 |
name: test-x |
| 100 |
arguments: |
| 101 |
x-queue-type: stream |
| 102 |
x-max-age: 24h |
| 103 |
''' |
| 104 |
|
| 105 |
import json |
| 106 |
import traceback |
| 107 |
|
| 108 |
REQUESTS_IMP_ERR = None |
| 109 |
try: |
| 110 |
import requests |
| 111 |
HAS_REQUESTS = True |
| 112 |
except ImportError: |
| 113 |
REQUESTS_IMP_ERR = traceback.format_exc() |
| 114 |
HAS_REQUESTS = False |
| 115 |
|
| 116 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 117 |
from ansible.module_utils.six.moves.urllib import parse as urllib_parse |
| 118 |
from ansible_collections.community.rabbitmq.plugins.module_utils.rabbitmq import rabbitmq_argument_spec |
| 119 |
|
| 120 |
|
| 121 |
def check_if_arg_changed(module, current_args, desired_args, arg_name): |
| 122 |
if arg_name not in current_args: |
| 123 |
if arg_name in desired_args: |
| 124 |
module.fail_json( |
| 125 |
msg=("RabbitMQ RESTAPI doesn't support attribute changes for existing queues." |
| 126 |
"Attempting to set %s which is not currently set." % arg_name), |
| 127 |
) |
| 128 |
# else don't care |
| 129 |
else: # arg_name in current_args |
| 130 |
if arg_name in desired_args: |
| 131 |
if current_args[arg_name] != desired_args[arg_name]: |
| 132 |
module.fail_json( |
| 133 |
msg=("RabbitMQ RESTAPI doesn't support attribute changes for existing queues.\n" |
| 134 |
"Attempting to change %s from '%s' to '%s'" % (arg_name, current_args[arg_name], desired_args[arg_name])) |
| 135 |
) |
| 136 |
else: |
| 137 |
module.fail_json( |
| 138 |
msg=("RabbitMQ RESTAPI doesn't support attribute changes for existing queues." |
| 139 |
"Attempting to unset %s which is currently set to '%s'." % (arg_name, current_args[arg_name])), |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
def main(): |
| 144 |
|
| 145 |
argument_spec = rabbitmq_argument_spec() |
| 146 |
argument_spec.update( |
| 147 |
dict( |
| 148 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 149 |
name=dict(required=True, type='str'), |
| 150 |
durable=dict(default=True, type='bool'), |
| 151 |
auto_delete=dict(default=False, type='bool'), |
| 152 |
message_ttl=dict(default=None, type='int'), |
| 153 |
auto_expires=dict(default=None, type='int'), |
| 154 |
max_length=dict(default=None, type='int'), |
| 155 |
dead_letter_exchange=dict(default=None, type='str'), |
| 156 |
dead_letter_routing_key=dict(default=None, type='str', no_log=False), |
| 157 |
arguments=dict(default=dict(), type='dict'), |
| 158 |
max_priority=dict(default=None, type='int') |
| 159 |
) |
| 160 |
) |
| 161 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 162 |
|
| 163 |
url = "%s://%s:%s/api/queues/%s/%s" % ( |
| 164 |
module.params['login_protocol'], |
| 165 |
module.params['login_host'], |
| 166 |
module.params['login_port'], |
| 167 |
# Ensure provided data is safe to use in a URL. |
| 168 |
# https://docs.python.org/3/library/urllib.parse.html#url-quoting |
| 169 |
# NOTE: This will also encode '/' characters, as they are required |
| 170 |
# to be percent encoded in the RabbitMQ management API. |
| 171 |
urllib_parse.quote(module.params['vhost'], safe=''), |
| 172 |
urllib_parse.quote(module.params['name'], safe='') |
| 173 |
) |
| 174 |
|
| 175 |
if not HAS_REQUESTS: |
| 176 |
module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR) |
| 177 |
|
| 178 |
result = dict(changed=False, name=module.params['name']) |
| 179 |
|
| 180 |
# Check if queue already exists |
| 181 |
r = requests.get(url, auth=(module.params['login_user'], module.params['login_password']), |
| 182 |
verify=module.params['ca_cert'], cert=(module.params['client_cert'], module.params['client_key'])) |
| 183 |
|
| 184 |
if r.status_code == 200: |
| 185 |
queue_exists = True |
| 186 |
response = r.json() |
| 187 |
elif r.status_code == 404: |
| 188 |
queue_exists = False |
| 189 |
response = r.text |
| 190 |
else: |
| 191 |
module.fail_json( |
| 192 |
msg="Invalid response from RESTAPI when trying to check if queue exists", |
| 193 |
details=r.text |
| 194 |
) |
| 195 |
|
| 196 |
arg_map = { |
| 197 |
'message_ttl': 'x-message-ttl', |
| 198 |
'auto_expires': 'x-expires', |
| 199 |
'max_length': 'x-max-length', |
| 200 |
'dead_letter_exchange': 'x-dead-letter-exchange', |
| 201 |
'dead_letter_routing_key': 'x-dead-letter-routing-key', |
| 202 |
'max_priority': 'x-max-priority' |
| 203 |
} |
| 204 |
|
| 205 |
# Sync arguments with parameters (the final request uses module.params['arguments']) |
| 206 |
for k, v in arg_map.items(): |
| 207 |
if module.params[k] is not None: |
| 208 |
module.params['arguments'][v] = module.params[k] |
| 209 |
|
| 210 |
if module.params['state'] == 'present': |
| 211 |
add_or_delete_required = not queue_exists |
| 212 |
else: |
| 213 |
add_or_delete_required = queue_exists |
| 214 |
|
| 215 |
# Check if attributes change on existing queue |
| 216 |
if not add_or_delete_required and r.status_code == 200 and module.params['state'] == 'present': |
| 217 |
check_if_arg_changed(module, response, module.params, 'durable') |
| 218 |
check_if_arg_changed(module, response, module.params, 'auto_delete') |
| 219 |
|
| 220 |
for arg in arg_map.values(): |
| 221 |
check_if_arg_changed(module, response['arguments'], module.params['arguments'], arg) |
| 222 |
|
| 223 |
# Exit if check_mode |
| 224 |
if module.check_mode: |
| 225 |
result['changed'] = add_or_delete_required |
| 226 |
result['details'] = response |
| 227 |
result['arguments'] = module.params['arguments'] |
| 228 |
module.exit_json(**result) |
| 229 |
|
| 230 |
# Do changes |
| 231 |
if add_or_delete_required: |
| 232 |
if module.params['state'] == 'present': |
| 233 |
r = requests.put( |
| 234 |
url, |
| 235 |
auth=(module.params['login_user'], module.params['login_password']), |
| 236 |
headers={"content-type": "application/json"}, |
| 237 |
data=json.dumps({ |
| 238 |
"durable": module.params['durable'], |
| 239 |
"auto_delete": module.params['auto_delete'], |
| 240 |
"arguments": module.params['arguments'] |
| 241 |
}), |
| 242 |
verify=module.params['ca_cert'], |
| 243 |
cert=(module.params['client_cert'], module.params['client_key']) |
| 244 |
) |
| 245 |
elif module.params['state'] == 'absent': |
| 246 |
r = requests.delete(url, auth=(module.params['login_user'], module.params['login_password']), |
| 247 |
verify=module.params['ca_cert'], cert=(module.params['client_cert'], module.params['client_key'])) |
| 248 |
|
| 249 |
# RabbitMQ 3.6.7 changed this response code from 204 to 201 |
| 250 |
if r.status_code == 204 or r.status_code == 201: |
| 251 |
result['changed'] = True |
| 252 |
module.exit_json(**result) |
| 253 |
else: |
| 254 |
module.fail_json( |
| 255 |
msg="Error creating queue", |
| 256 |
status=r.status_code, |
| 257 |
details=r.text |
| 258 |
) |
| 259 |
|
| 260 |
else: |
| 261 |
module.exit_json( |
| 262 |
changed=False, |
| 263 |
name=module.params['name'] |
| 264 |
) |
| 265 |
|
| 266 |
|
| 267 |
if __name__ == '__main__': |
| 268 |
main() |