From 308305a691c2b90fd354662b5d121db4de65a68d Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 8 May 2015 17:15:36 +0100 Subject: [PATCH 1/6] add module rabbitmq_exchange --- messaging/rabbitmq_exchange.py | 197 +++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 messaging/rabbitmq_exchange.py diff --git a/messaging/rabbitmq_exchange.py b/messaging/rabbitmq_exchange.py new file mode 100644 index 00000000000..ac2fee61812 --- /dev/null +++ b/messaging/rabbitmq_exchange.py @@ -0,0 +1,197 @@ +#!/usr/bin/python + +DOCUMENTATION = ''' +module: rabbitmq_exchange +author: Manuel Sousa +version_added: 1.5.4 + +short_description: This module manages rabbitMQ exchanges +description: + - This module uses rabbitMQ Rest API to create/delete exchanges +requirements: [ python requests ] +options: + name: + description: + - Name of the exchange to create + required: true + state: + description: + - Whether the exchange should be present or absent + - Only present implemented atm + choices: [ "present", "absent" ] + required: false + default: present + 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 + required: false + default: "/" + durable: + description: + - whether exchange is durable or not + required: false + choices: [ "yes", "no" ] + default: yes + exchangeType: + description: + - type for the exchange + required: false + choices: [ "fanout", "direct", "headers", "topic" ] + aliases: [ "type" ] + default: direct + autoDelete: + description: + - if the exchange should delete itself after all queues/exchanges unbound from it + required: false + choices: [ "yes", "no" ] + default: no + internal: + description: + - exchange is available only for other exchanges + required: false + choices: [ "yes", "no" ] + default: no + arguments: + description: + - extra arguments for exchange. If defined this argument is a key/value dictionary + required: false +''' + +EXAMPLES = ''' +# Create direct exchange +- rabbitmq_exchange: name=directExchange + +# Create topic exchange on vhost +- rabbitmq_exchange: name=topicExchange type=topic vhost=myVhost +''' + +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, type='str'), + login_user = dict(default='guest', type='str'), + login_password = dict(default='guest', type='str'), + login_host = dict(default='localhost', type='str'), + login_port = dict(default='15672', type='str'), + vhost = dict(default='/', type='str'), + durable = dict(default=True, choices=BOOLEANS, type='bool'), + autoDelete = dict(default=False, choices=BOOLEANS, type='bool'), + internal = dict(default=False, choices=BOOLEANS, type='bool'), + exchangeType = dict(default='direct', aliases=['type'], type='str'), + arguments = dict(default=dict(), type='dict') + ), + supports_check_mode = True + ) + + url = "http://%s:%s/api/exchanges/%s/%s" % ( + module.params['login_host'], + module.params['login_port'], + urllib.quote(module.params['vhost'],''), + module.params['name'] + ) + + # Check if exchange already exists + r = requests.get( url, auth=(module.params['login_user'],module.params['login_password'])) + + if r.status_code==200: + exchangeExists = True + response = r.json() + elif r.status_code==404: + exchangeExists = False + response = r.text + else: + module.fail_json( + msg = "Invalid response from RESTAPI when trying to check if exchange exists", + details = r.text + ) + + changeRequired = not exchangeExists if module.params['state']=='present' else exchangeExists + + # Check if attributes change on existing exchange + if not changeRequired and r.status_code==200 and module.params['state'] == 'present': + if not ( + response['durable'] == module.params['durable'] and + response['auto_delete'] == module.params['autoDelete'] and + response['internal'] == module.params['internal'] and + response['type'] == module.params['exchangeType'] + ): + module.fail_json( + msg = "RabbitMQ RESTAPI doesn't support attribute changes for existing exchanges" + ) + + # Exit if check_mode + if module.check_mode: + module.exit_json( + changed= changeRequired, + result = "Success", + name = module.params['name'], + details = response, + arguments = module.params['arguments'] + ) + + # Do changes + if changeRequired: + if module.params['state'] == 'present': + r = requests.put( + url, + auth = (module.params['login_user'],module.params['login_password']), + headers = { "content-type": "application/json"}, + data = json.dumps({ + "durable": module.params['durable'], + "auto_delete": module.params['autoDelete'], + "internal": module.params['internal'], + "type": module.params['exchangeType'], + "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: + module.exit_json( + changed = True, + result = "Success", + name = module.params['name'] + ) + else: + module.fail_json( + msg = "Error creating exchange", + status = r.status_code, + details = r.text + ) + + else: + module.exit_json( + changed = False, + result = "Success", + name = module.params['name'] + ) + +# import module snippets +from ansible.module_utils.basic import * +main() From 4843e06fcd232203e69f3a8a93a3ec1e94662e4d Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 8 May 2015 17:30:05 +0100 Subject: [PATCH 2/6] Change version and remove result="success" --- messaging/rabbitmq_exchange.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/messaging/rabbitmq_exchange.py b/messaging/rabbitmq_exchange.py index ac2fee61812..b7dbd00be04 100644 --- a/messaging/rabbitmq_exchange.py +++ b/messaging/rabbitmq_exchange.py @@ -3,7 +3,7 @@ DOCUMENTATION = ''' module: rabbitmq_exchange author: Manuel Sousa -version_added: 1.5.4 +version_added: 2.0 short_description: This module manages rabbitMQ exchanges description: @@ -148,7 +148,6 @@ def main(): if module.check_mode: module.exit_json( changed= changeRequired, - result = "Success", name = module.params['name'], details = response, arguments = module.params['arguments'] @@ -175,7 +174,6 @@ def main(): if r.status_code == 204: module.exit_json( changed = True, - result = "Success", name = module.params['name'] ) else: @@ -188,7 +186,6 @@ def main(): else: module.exit_json( changed = False, - result = "Success", name = module.params['name'] ) From 180fb13b2849126024228750193188e60afad7cb Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Wed, 13 May 2015 23:36:41 +0100 Subject: [PATCH 3/6] Added GPL license + Don't log password Implemented resmo suggestions. --- messaging/rabbitmq_exchange.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/messaging/rabbitmq_exchange.py b/messaging/rabbitmq_exchange.py index b7dbd00be04..f184acc024e 100644 --- a/messaging/rabbitmq_exchange.py +++ b/messaging/rabbitmq_exchange.py @@ -1,9 +1,29 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2015, Manuel Sousa +# +# 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 . +# DOCUMENTATION = ''' +--- module: rabbitmq_exchange author: Manuel Sousa -version_added: 2.0 +version_added: "2.0" short_description: This module manages rabbitMQ exchanges description: @@ -95,7 +115,7 @@ def main(): state = dict(default='present', choices=['present', 'absent'], type='str'), name = dict(required=True, type='str'), login_user = dict(default='guest', type='str'), - login_password = dict(default='guest', type='str'), + login_password = dict(default='guest', type='str', no_log=True), login_host = dict(default='localhost', type='str'), login_port = dict(default='15672', type='str'), vhost = dict(default='/', type='str'), From 4882f2bbbe046d382c14d12cff3418998ad2792a Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Thu, 14 May 2015 15:30:10 +0100 Subject: [PATCH 4/6] Make compatible with python Removed one line if else --- messaging/rabbitmq_exchange.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/messaging/rabbitmq_exchange.py b/messaging/rabbitmq_exchange.py index f184acc024e..4cceac6b85f 100644 --- a/messaging/rabbitmq_exchange.py +++ b/messaging/rabbitmq_exchange.py @@ -150,7 +150,10 @@ def main(): details = r.text ) - changeRequired = not exchangeExists if module.params['state']=='present' else exchangeExists + if module.params['state']=='present': + changeRequired = not exchangeExists + else: + changeRequired = exchangeExists # Check if attributes change on existing exchange if not changeRequired and r.status_code==200 and module.params['state'] == 'present': From abab60208d8be3074572dd6fdf287957584fc04e Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 15 May 2015 16:03:40 +0100 Subject: [PATCH 5/6] Change variables from camel case to underscore --- messaging/rabbitmq_exchange.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/messaging/rabbitmq_exchange.py b/messaging/rabbitmq_exchange.py index 4cceac6b85f..9bb8f69c75f 100644 --- a/messaging/rabbitmq_exchange.py +++ b/messaging/rabbitmq_exchange.py @@ -72,14 +72,14 @@ options: required: false choices: [ "yes", "no" ] default: yes - exchangeType: + exchange_type: description: - type for the exchange required: false choices: [ "fanout", "direct", "headers", "topic" ] aliases: [ "type" ] default: direct - autoDelete: + auto_delete: description: - if the exchange should delete itself after all queues/exchanges unbound from it required: false @@ -120,9 +120,9 @@ def main(): login_port = dict(default='15672', type='str'), vhost = dict(default='/', type='str'), durable = dict(default=True, choices=BOOLEANS, type='bool'), - autoDelete = dict(default=False, choices=BOOLEANS, type='bool'), + auto_delete = dict(default=False, choices=BOOLEANS, type='bool'), internal = dict(default=False, choices=BOOLEANS, type='bool'), - exchangeType = dict(default='direct', aliases=['type'], type='str'), + exchange_type = dict(default='direct', aliases=['type'], type='str'), arguments = dict(default=dict(), type='dict') ), supports_check_mode = True @@ -151,17 +151,17 @@ def main(): ) if module.params['state']=='present': - changeRequired = not exchangeExists + change_required = not exchangeExists else: - changeRequired = exchangeExists + change_required = exchangeExists # Check if attributes change on existing exchange - if not changeRequired and r.status_code==200 and module.params['state'] == 'present': + if not change_required and r.status_code==200 and module.params['state'] == 'present': if not ( response['durable'] == module.params['durable'] and - response['auto_delete'] == module.params['autoDelete'] and + response['auto_delete'] == module.params['auto_delete'] and response['internal'] == module.params['internal'] and - response['type'] == module.params['exchangeType'] + response['type'] == module.params['exchange_type'] ): module.fail_json( msg = "RabbitMQ RESTAPI doesn't support attribute changes for existing exchanges" @@ -170,14 +170,14 @@ def main(): # Exit if check_mode if module.check_mode: module.exit_json( - changed= changeRequired, + changed= change_required, name = module.params['name'], details = response, arguments = module.params['arguments'] ) # Do changes - if changeRequired: + if change_required: if module.params['state'] == 'present': r = requests.put( url, @@ -185,9 +185,9 @@ def main(): headers = { "content-type": "application/json"}, data = json.dumps({ "durable": module.params['durable'], - "auto_delete": module.params['autoDelete'], + "auto_delete": module.params['auto_delete'], "internal": module.params['internal'], - "type": module.params['exchangeType'], + "type": module.params['exchange_type'], "arguments": module.params['arguments'] }) ) From 709817bf19bd1a108ed96f69b90ba6a375cc92ea Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 15 May 2015 16:08:47 +0100 Subject: [PATCH 6/6] Missed variable exchangeExists -> exchange_exists --- messaging/rabbitmq_exchange.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/messaging/rabbitmq_exchange.py b/messaging/rabbitmq_exchange.py index 9bb8f69c75f..5f6c83c10e6 100644 --- a/messaging/rabbitmq_exchange.py +++ b/messaging/rabbitmq_exchange.py @@ -139,10 +139,10 @@ def main(): r = requests.get( url, auth=(module.params['login_user'],module.params['login_password'])) if r.status_code==200: - exchangeExists = True + exchange_exists = True response = r.json() elif r.status_code==404: - exchangeExists = False + exchange_exists = False response = r.text else: module.fail_json( @@ -151,9 +151,9 @@ def main(): ) if module.params['state']=='present': - change_required = not exchangeExists + change_required = not exchange_exists else: - change_required = exchangeExists + change_required = exchange_exists # Check if attributes change on existing exchange if not change_required and r.status_code==200 and module.params['state'] == 'present':