2015-05-08 18:14:22 +02:00
|
|
|
#!/usr/bin/python
|
2015-05-14 00:28:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2015, Manuel Sousa <manuel.sousa@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2015-05-08 18:14:22 +02:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
2015-05-14 00:28:11 +02:00
|
|
|
---
|
2015-05-08 18:14:22 +02:00
|
|
|
module: rabbitmq_binding
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Manuel Sousa (@manuel-sousa)"
|
2015-05-14 00:28:11 +02:00
|
|
|
version_added: "2.0"
|
2015-05-08 18:14:22 +02:00
|
|
|
|
|
|
|
short_description: This module manages rabbitMQ bindings
|
|
|
|
description:
|
|
|
|
- This module uses rabbitMQ Rest API to create/delete bindings
|
2016-08-25 18:45:37 +02:00
|
|
|
requirements: [ "requests >= 1.0.0" ]
|
2015-05-08 18:14:22 +02:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether the exchange should be present or absent
|
|
|
|
- Only present implemented atm
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- source exchange to create binding on
|
|
|
|
required: true
|
|
|
|
aliases: [ "src", "source" ]
|
|
|
|
login_user:
|
|
|
|
description:
|
|
|
|
- rabbitMQ user for connection
|
|
|
|
required: false
|
|
|
|
default: guest
|
|
|
|
login_password:
|
|
|
|
description:
|
|
|
|
- rabbitMQ password for connection
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
login_host:
|
|
|
|
description:
|
|
|
|
- rabbitMQ host for connection
|
|
|
|
required: false
|
|
|
|
default: localhost
|
|
|
|
login_port:
|
|
|
|
description:
|
|
|
|
- rabbitMQ management api port
|
|
|
|
required: false
|
|
|
|
default: 15672
|
|
|
|
vhost:
|
|
|
|
description:
|
|
|
|
- rabbitMQ virtual host
|
|
|
|
- default vhost is /
|
|
|
|
required: false
|
|
|
|
default: "/"
|
|
|
|
destination:
|
|
|
|
description:
|
|
|
|
- destination exchange or queue for the binding
|
|
|
|
required: true
|
|
|
|
aliases: [ "dst", "dest" ]
|
2015-05-15 17:12:43 +02:00
|
|
|
destination_type:
|
2015-05-08 18:14:22 +02:00
|
|
|
description:
|
|
|
|
- Either queue or exchange
|
|
|
|
required: true
|
|
|
|
choices: [ "queue", "exchange" ]
|
2015-05-15 17:12:43 +02:00
|
|
|
aliases: [ "type", "dest_type" ]
|
|
|
|
routing_key:
|
2015-05-08 18:14:22 +02:00
|
|
|
description:
|
|
|
|
- routing key for the binding
|
|
|
|
- default is #
|
|
|
|
required: false
|
|
|
|
default: "#"
|
|
|
|
arguments:
|
|
|
|
description:
|
|
|
|
- extra arguments for exchange. If defined this argument is a key/value dictionary
|
2015-05-18 14:22:35 +02:00
|
|
|
required: false
|
|
|
|
default: {}
|
2015-05-08 18:14:22 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Bind myQueue to directExchange with routing key info
|
2015-05-15 17:12:43 +02:00
|
|
|
- rabbitmq_binding: name=directExchange destination=myQueue type=queue routing_key=info
|
2015-05-08 18:14:22 +02:00
|
|
|
|
|
|
|
# Bind directExchange to topicExchange with routing key *.info
|
2015-05-15 17:12:43 +02:00
|
|
|
- rabbitmq_binding: name=topicExchange destination=topicExchange type=exchange routing_key="*.info"
|
2015-05-08 18:14:22 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import urllib
|
|
|
|
import json
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
state = dict(default='present', choices=['present', 'absent'], type='str'),
|
|
|
|
name = dict(required=True, aliases=[ "src", "source" ], type='str'),
|
|
|
|
login_user = dict(default='guest', type='str'),
|
2015-05-14 00:28:11 +02:00
|
|
|
login_password = dict(default='guest', type='str', no_log=True),
|
2015-05-08 18:14:22 +02:00
|
|
|
login_host = dict(default='localhost', type='str'),
|
|
|
|
login_port = dict(default='15672', type='str'),
|
|
|
|
vhost = dict(default='/', type='str'),
|
|
|
|
destination = dict(required=True, aliases=[ "dst", "dest"], type='str'),
|
2015-05-15 17:12:43 +02:00
|
|
|
destination_type = dict(required=True, aliases=[ "type", "dest_type"], choices=[ "queue", "exchange" ],type='str'),
|
|
|
|
routing_key = dict(default='#', type='str'),
|
2015-05-08 18:14:22 +02:00
|
|
|
arguments = dict(default=dict(), type='dict')
|
|
|
|
),
|
|
|
|
supports_check_mode = True
|
|
|
|
)
|
|
|
|
|
2015-05-15 17:12:43 +02:00
|
|
|
if module.params['destination_type'] == "queue":
|
|
|
|
dest_type="q"
|
2015-05-14 16:26:20 +02:00
|
|
|
else:
|
2015-05-15 17:12:43 +02:00
|
|
|
dest_type="e"
|
2015-05-14 16:26:20 +02:00
|
|
|
|
2016-08-06 18:27:27 +02:00
|
|
|
if module.params['routing_key'] == "":
|
|
|
|
props = "~"
|
|
|
|
else:
|
|
|
|
props = urllib.quote(module.params['routing_key'],'')
|
|
|
|
|
2015-05-08 18:14:22 +02:00
|
|
|
url = "http://%s:%s/api/bindings/%s/e/%s/%s/%s/%s" % (
|
|
|
|
module.params['login_host'],
|
|
|
|
module.params['login_port'],
|
|
|
|
urllib.quote(module.params['vhost'],''),
|
2015-10-21 13:36:23 +02:00
|
|
|
urllib.quote(module.params['name'],''),
|
2015-05-15 17:12:43 +02:00
|
|
|
dest_type,
|
2015-10-21 13:36:23 +02:00
|
|
|
urllib.quote(module.params['destination'],''),
|
2016-08-06 18:27:27 +02:00
|
|
|
props
|
2015-05-08 18:14:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check if exchange already exists
|
|
|
|
r = requests.get( url, auth=(module.params['login_user'],module.params['login_password']))
|
|
|
|
|
|
|
|
if r.status_code==200:
|
2015-05-15 17:12:43 +02:00
|
|
|
binding_exists = True
|
2015-05-08 18:14:22 +02:00
|
|
|
response = r.json()
|
|
|
|
elif r.status_code==404:
|
2015-05-15 17:12:43 +02:00
|
|
|
binding_exists = False
|
2015-05-08 18:14:22 +02:00
|
|
|
response = r.text
|
|
|
|
else:
|
|
|
|
module.fail_json(
|
|
|
|
msg = "Invalid response from RESTAPI when trying to check if exchange exists",
|
|
|
|
details = r.text
|
|
|
|
)
|
|
|
|
|
2015-05-14 16:26:20 +02:00
|
|
|
if module.params['state']=='present':
|
2015-05-15 17:12:43 +02:00
|
|
|
change_required = not binding_exists
|
2015-05-14 16:26:20 +02:00
|
|
|
else:
|
2015-05-15 17:12:43 +02:00
|
|
|
change_required = binding_exists
|
2015-05-08 18:14:22 +02:00
|
|
|
|
|
|
|
# Exit if check_mode
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(
|
2015-05-15 17:12:43 +02:00
|
|
|
changed= change_required,
|
2015-05-08 18:14:22 +02:00
|
|
|
name = module.params['name'],
|
|
|
|
details = response,
|
|
|
|
arguments = module.params['arguments']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Do changes
|
2015-05-15 17:12:43 +02:00
|
|
|
if change_required:
|
2015-05-08 18:14:22 +02:00
|
|
|
if module.params['state'] == 'present':
|
|
|
|
url = "http://%s:%s/api/bindings/%s/e/%s/%s/%s" % (
|
|
|
|
module.params['login_host'],
|
|
|
|
module.params['login_port'],
|
|
|
|
urllib.quote(module.params['vhost'],''),
|
2015-10-21 13:36:23 +02:00
|
|
|
urllib.quote(module.params['name'],''),
|
2015-05-15 17:12:43 +02:00
|
|
|
dest_type,
|
2015-10-21 13:36:23 +02:00
|
|
|
urllib.quote(module.params['destination'],'')
|
2015-05-08 18:14:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
r = requests.post(
|
|
|
|
url,
|
|
|
|
auth = (module.params['login_user'],module.params['login_password']),
|
|
|
|
headers = { "content-type": "application/json"},
|
|
|
|
data = json.dumps({
|
2015-05-15 17:12:43 +02:00
|
|
|
"routing_key": module.params['routing_key'],
|
2015-05-08 18:14:22 +02:00
|
|
|
"arguments": module.params['arguments']
|
|
|
|
})
|
|
|
|
)
|
|
|
|
elif module.params['state'] == 'absent':
|
|
|
|
r = requests.delete( url, auth = (module.params['login_user'],module.params['login_password']))
|
|
|
|
|
|
|
|
if r.status_code == 204 or r.status_code == 201:
|
|
|
|
module.exit_json(
|
|
|
|
changed = True,
|
|
|
|
name = module.params['name'],
|
|
|
|
destination = module.params['destination']
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
module.fail_json(
|
|
|
|
msg = "Error creating exchange",
|
|
|
|
status = r.status_code,
|
|
|
|
details = r.text
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
module.exit_json(
|
|
|
|
changed = False,
|
|
|
|
name = module.params['name']
|
|
|
|
)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|