| 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_exchange |
| 14 |
author: Manuel Sousa (@manuel-sousa) |
| 15 |
|
| 16 |
short_description: Manage rabbitMQ exchange |
| 17 |
description: |
| 18 |
- This module uses rabbitMQ Rest API to create/delete exchanges |
| 19 |
requirements: [ "requests >= 1.0.0" ] |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Name of the exchange to create. |
| 24 |
type: str |
| 25 |
required: true |
| 26 |
state: |
| 27 |
description: |
| 28 |
- Whether the exchange should be present or absent. |
| 29 |
type: str |
| 30 |
choices: [ "present", "absent" ] |
| 31 |
required: false |
| 32 |
default: present |
| 33 |
durable: |
| 34 |
description: |
| 35 |
- Whether exchange is durable or not. |
| 36 |
type: bool |
| 37 |
required: false |
| 38 |
default: true |
| 39 |
exchange_type: |
| 40 |
description: |
| 41 |
- Type for the exchange. |
| 42 |
- If using I(x-delayed-message), I(x-random), I(x-consistent-hash) or I(x-recent-history) the respective plugin on |
| 43 |
- the RabbitMQ server must be enabled. |
| 44 |
type: str |
| 45 |
required: false |
| 46 |
choices: [ "fanout", "direct", "headers", "topic", "x-delayed-message", "x-random", "x-consistent-hash", "x-recent-history" ] |
| 47 |
aliases: [ "type" ] |
| 48 |
default: direct |
| 49 |
auto_delete: |
| 50 |
description: |
| 51 |
- If the exchange should delete itself after all queues/exchanges unbound from it. |
| 52 |
type: bool |
| 53 |
required: false |
| 54 |
default: false |
| 55 |
internal: |
| 56 |
description: |
| 57 |
- Exchange is available only for other exchanges. |
| 58 |
type: bool |
| 59 |
required: false |
| 60 |
default: false |
| 61 |
arguments: |
| 62 |
description: |
| 63 |
- Extra arguments for exchange. If defined this argument is a key/value dictionary. |
| 64 |
type: dict |
| 65 |
required: false |
| 66 |
default: {} |
| 67 |
extends_documentation_fragment: |
| 68 |
- community.rabbitmq.rabbitmq |
| 69 |
|
| 70 |
''' |
| 71 |
|
| 72 |
EXAMPLES = r''' |
| 73 |
- name: Create direct exchange |
| 74 |
community.rabbitmq.rabbitmq_exchange: |
| 75 |
name: directExchange |
| 76 |
|
| 77 |
- name: Create topic exchange on vhost |
| 78 |
community.rabbitmq.rabbitmq_exchange: |
| 79 |
name: topicExchange |
| 80 |
type: topic |
| 81 |
vhost: myVhost |
| 82 |
''' |
| 83 |
|
| 84 |
import json |
| 85 |
import traceback |
| 86 |
|
| 87 |
REQUESTS_IMP_ERR = None |
| 88 |
try: |
| 89 |
import requests |
| 90 |
HAS_REQUESTS = True |
| 91 |
except ImportError: |
| 92 |
REQUESTS_IMP_ERR = traceback.format_exc() |
| 93 |
HAS_REQUESTS = False |
| 94 |
|
| 95 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 96 |
from ansible.module_utils.six.moves.urllib import parse as urllib_parse |
| 97 |
from ansible_collections.community.rabbitmq.plugins.module_utils.rabbitmq import rabbitmq_argument_spec |
| 98 |
|
| 99 |
|
| 100 |
def main(): |
| 101 |
|
| 102 |
argument_spec = rabbitmq_argument_spec() |
| 103 |
argument_spec.update( |
| 104 |
dict( |
| 105 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 106 |
name=dict(required=True, type='str'), |
| 107 |
durable=dict(default=True, type='bool'), |
| 108 |
auto_delete=dict(default=False, type='bool'), |
| 109 |
internal=dict(default=False, type='bool'), |
| 110 |
exchange_type=dict(default='direct', aliases=['type'], |
| 111 |
choices=['fanout', 'direct', 'headers', 'topic', 'x-delayed-message', |
| 112 |
'x-random', 'x-consistent-hash', 'x-recent-history'], |
| 113 |
type='str'), |
| 114 |
arguments=dict(default=dict(), type='dict') |
| 115 |
) |
| 116 |
) |
| 117 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 118 |
|
| 119 |
url = "%s://%s:%s/api/exchanges/%s/%s" % ( |
| 120 |
module.params['login_protocol'], |
| 121 |
module.params['login_host'], |
| 122 |
module.params['login_port'], |
| 123 |
# Ensure provided data is safe to use in a URL. |
| 124 |
# https://docs.python.org/3/library/urllib.parse.html#url-quoting |
| 125 |
# NOTE: This will also encode '/' characters, as they are required |
| 126 |
# to be percent encoded in the RabbitMQ management API. |
| 127 |
urllib_parse.quote(module.params['vhost'], safe=''), |
| 128 |
urllib_parse.quote(module.params['name'], safe='') |
| 129 |
) |
| 130 |
|
| 131 |
if not HAS_REQUESTS: |
| 132 |
module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR) |
| 133 |
|
| 134 |
# exchange plugin type to plugin name mapping |
| 135 |
exchange_plugins = {'x-consistent-hash': 'rabbitmq_consistent_hash_exchange', |
| 136 |
'x-random': 'rabbitmq_random_exchange', |
| 137 |
'x-delayed-message': 'rabbitmq_delayed_message_exchange', |
| 138 |
'x-recent-history': 'rabbitmq_recent_history_exchange'} |
| 139 |
result = dict(changed=False, name=module.params['name']) |
| 140 |
|
| 141 |
# Check if exchange already exists |
| 142 |
r = requests.get(url, auth=(module.params['login_user'], module.params['login_password']), |
| 143 |
verify=module.params['ca_cert'], cert=(module.params['client_cert'], module.params['client_key'])) |
| 144 |
|
| 145 |
if r.status_code == 200: |
| 146 |
exchange_exists = True |
| 147 |
response = r.json() |
| 148 |
elif r.status_code == 404: |
| 149 |
exchange_exists = False |
| 150 |
response = r.text |
| 151 |
else: |
| 152 |
module.fail_json( |
| 153 |
msg="Invalid response from RESTAPI when trying to check if exchange exists", |
| 154 |
details=r.text |
| 155 |
) |
| 156 |
|
| 157 |
if module.params['state'] == 'present': |
| 158 |
change_required = not exchange_exists |
| 159 |
else: |
| 160 |
change_required = exchange_exists |
| 161 |
|
| 162 |
# Check if attributes change on existing exchange |
| 163 |
if not change_required and r.status_code == 200 and module.params['state'] == 'present': |
| 164 |
if not ( |
| 165 |
response['durable'] == module.params['durable'] and |
| 166 |
response['auto_delete'] == module.params['auto_delete'] and |
| 167 |
response['internal'] == module.params['internal'] and |
| 168 |
response['type'] == module.params['exchange_type'] |
| 169 |
): |
| 170 |
module.fail_json( |
| 171 |
msg="RabbitMQ RESTAPI doesn't support attribute changes for existing exchanges" |
| 172 |
) |
| 173 |
|
| 174 |
# Exit if check_mode |
| 175 |
if module.check_mode: |
| 176 |
result['changed'] = change_required |
| 177 |
result['details'] = response |
| 178 |
result['arguments'] = module.params['arguments'] |
| 179 |
module.exit_json(**result) |
| 180 |
|
| 181 |
# Do changes |
| 182 |
if change_required: |
| 183 |
if module.params['state'] == 'present': |
| 184 |
r = requests.put( |
| 185 |
url, |
| 186 |
auth=(module.params['login_user'], module.params['login_password']), |
| 187 |
headers={"content-type": "application/json"}, |
| 188 |
data=json.dumps({ |
| 189 |
"durable": module.params['durable'], |
| 190 |
"auto_delete": module.params['auto_delete'], |
| 191 |
"internal": module.params['internal'], |
| 192 |
"type": module.params['exchange_type'], |
| 193 |
"arguments": module.params['arguments'] |
| 194 |
}), |
| 195 |
verify=module.params['ca_cert'], |
| 196 |
cert=(module.params['client_cert'], module.params['client_key']) |
| 197 |
) |
| 198 |
elif module.params['state'] == 'absent': |
| 199 |
r = requests.delete(url, auth=(module.params['login_user'], module.params['login_password']), |
| 200 |
verify=module.params['ca_cert'], cert=(module.params['client_cert'], module.params['client_key'])) |
| 201 |
|
| 202 |
# RabbitMQ 3.6.7 changed this response code from 204 to 201 |
| 203 |
if r.status_code == 204 or r.status_code == 201: |
| 204 |
result['changed'] = True |
| 205 |
module.exit_json(**result) |
| 206 |
else: |
| 207 |
rjson = r.json() |
| 208 |
if (rjson['reason'].startswith('unknown exchange type')): |
| 209 |
try: |
| 210 |
module.fail_json( |
| 211 |
msg=("Error creating exchange. You may need to enable the '%s' plugin for exchange type %s" % |
| 212 |
(exchange_plugins[module.params['exchange_type']], module.params['exchange_type'])), |
| 213 |
status=r.status_code, |
| 214 |
details=r.text |
| 215 |
) |
| 216 |
except KeyError: |
| 217 |
module.fail_json( |
| 218 |
msg=("Error creating exchange. You may need to enable a plugin for exchange type %s" % |
| 219 |
module.params['exchange_type']), |
| 220 |
status=r.status_code, |
| 221 |
details=r.text |
| 222 |
) |
| 223 |
else: |
| 224 |
module.fail_json( |
| 225 |
msg="Error creating exchange", |
| 226 |
status=r.status_code, |
| 227 |
details=r.text |
| 228 |
) |
| 229 |
|
| 230 |
else: |
| 231 |
module.exit_json( |
| 232 |
changed=False, |
| 233 |
name=module.params['name'] |
| 234 |
) |
| 235 |
|
| 236 |
|
| 237 |
if __name__ == '__main__': |
| 238 |
main() |