From 99f4f592fabc8f9f84ccc55971a5fa2b1797a3a0 Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 8 May 2015 17:16:41 +0100 Subject: [PATCH 1/6] add module rabbitmq_queue --- messaging/rabbitmq_queue.py | 235 ++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 messaging/rabbitmq_queue.py diff --git a/messaging/rabbitmq_queue.py b/messaging/rabbitmq_queue.py new file mode 100644 index 00000000000..8abfb447551 --- /dev/null +++ b/messaging/rabbitmq_queue.py @@ -0,0 +1,235 @@ +#!/usr/bin/python + +DOCUMENTATION = ''' +module: rabbitmq_queue +author: Manuel Sousa +version_added: 1.5.4 + +short_description: This module manages rabbitMQ queues +description: + - This module uses rabbitMQ Rest API to create/delete queues +requirements: [ python requests ] +options: + name: + description: + - Name of the queue to create + required: true + state: + description: + - Whether the queue 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 queue is durable or not + required: false + choices: [ "yes", "no" ] + default: yes + autoDelete: + description: + - if the queue should delete itself after all queues/queues unbound from it + required: false + choices: [ "yes", "no" ] + default: no + messageTTL: + description: + - How long a message can live in queue before it is discarded (milliseconds) + required: False + autoExpires: + description: + - How long a queue can be unused before it is automatically deleted (milliseconds) + required: false + maxLength: + description: + - How many messages can the queue contain before it starts rejecting + required: false + deadLetterExchange: + description: + - Optional name of an exchange to which messages will be republished if they + - are rejected or expire + required: false + deadLetterRoutingKey: + description: + - Optional replacement routing key to use when a message is dead-lettered. + - Original routing key will be used if unset + required: false + arguments: + description: + - extra arguments for queue. If defined this argument is a key/value dictionary + required: false +''' + +EXAMPLES = ''' +# Create a queue +- rabbitmq_queue: name=myQueue + +# Create a queue on remote host +- rabbitmq_queue: name=myRemoteQueue login_user=user login_password=secret login_host=remote.example.org +''' + +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'), + messageTTL = dict(default=None, type='int'), + autoExpire = dict(default=None, type='int'), + maxLength = dict(default=None, type='int'), + deadLetterExchange = dict(default=None, type='str'), + deadLetterRoutingKey = dict(default=None, type='str'), + arguments = dict(default=dict(), type='dict') + ), + supports_check_mode = True + ) + + url = "http://%s:%s/api/queues/%s/%s" % ( + module.params['login_host'], + module.params['login_port'], + urllib.quote(module.params['vhost'],''), + module.params['name'] + ) + + # Check if queue already exists + r = requests.get( url, auth=(module.params['login_user'],module.params['login_password'])) + + if r.status_code==200: + queueExists = True + response = r.json() + elif r.status_code==404: + queueExists = False + response = r.text + else: + module.fail_json( + msg = "Invalid response from RESTAPI when trying to check if queue exists", + details = r.text + ) + + changeRequired = not queueExists if module.params['state']=='present' else queueExists + + # Check if attributes change on existing queue + 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['arguments']['x-message-ttl'] == module.params['messageTTL'] if 'x-message-ttl' in response['arguments'] else module.params['messageTTL'] is None + ) and + ( + response['arguments']['x-expires'] == module.params['autoExpire'] if 'x-expires' in response['arguments'] else module.params['autoExpire'] is None + ) and + ( + response['arguments']['x-max-length'] == module.params['maxLength'] if 'x-max-length' in response['arguments'] else module.params['maxLength'] is None + ) and + ( + response['arguments']['x-dead-letter-exchange'] == module.params['deadLetterExchange'] if 'x-dead-letter-exchange' in response['arguments'] else module.params['deadLetterExchange'] is None + ) and + ( + response['arguments']['x-dead-letter-routing-key'] == module.params['deadLetterRoutingKey'] if 'x-dead-letter-routing-key' in response['arguments'] else module.params['deadLetterRoutingKey'] is None + ) + ): + module.fail_json( + msg = "RabbitMQ RESTAPI doesn't support attribute changes for existing queues", + details = "XPTO", + src = json.dumps(response['arguments']), + dest = json.dumps(module.params) + ) + + + # Copy parameters to arguments as used by RabbitMQ + for k,v in { + 'messageTTL': 'x-message-ttl', + 'autoExpire': 'x-expires', + 'maxLength': 'x-max-length', + 'deadLetterExchange': 'x-dead-letter-exchange', + 'deadLetterRoutingKey': 'x-dead-letter-routing-key' + }.items(): + if module.params[k]: + module.params['arguments'][v] = module.params[k] + + # 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'], + "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 queue", + 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 4ea269f0babca77c119e6781b36d880849af3429 Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 8 May 2015 17:28:53 +0100 Subject: [PATCH 2/6] Change version and remove result="success" --- messaging/rabbitmq_queue.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/messaging/rabbitmq_queue.py b/messaging/rabbitmq_queue.py index 8abfb447551..34d107e3856 100644 --- a/messaging/rabbitmq_queue.py +++ b/messaging/rabbitmq_queue.py @@ -3,7 +3,7 @@ DOCUMENTATION = ''' module: rabbitmq_queue author: Manuel Sousa -version_added: 1.5.4 +version_added: 2.0 short_description: This module manages rabbitMQ queues description: @@ -188,7 +188,6 @@ def main(): if module.check_mode: module.exit_json( changed= changeRequired, - result = "Success", name = module.params['name'], details = response, arguments = module.params['arguments'] @@ -213,7 +212,6 @@ def main(): if r.status_code == 204: module.exit_json( changed = True, - result = "Success", name = module.params['name'] ) else: @@ -226,7 +224,6 @@ def main(): else: module.exit_json( changed = False, - result = "Success", name = module.params['name'] ) From 2fa269b01964fb8b447098684e8979b11a7f0840 Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Wed, 13 May 2015 23:38:53 +0100 Subject: [PATCH 3/6] Added GPL license + Don't log password Implemented resmo suggestions. --- messaging/rabbitmq_queue.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/messaging/rabbitmq_queue.py b/messaging/rabbitmq_queue.py index 34d107e3856..ac5743fef55 100644 --- a/messaging/rabbitmq_queue.py +++ b/messaging/rabbitmq_queue.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_queue author: Manuel Sousa -version_added: 2.0 +version_added: "2.0" short_description: This module manages rabbitMQ queues description: @@ -104,7 +124,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 6354e835d18816d5278798b5bd0ae286cee92035 Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Thu, 14 May 2015 15:36:31 +0100 Subject: [PATCH 4/6] Removed leftovers from testing the module --- messaging/rabbitmq_queue.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/messaging/rabbitmq_queue.py b/messaging/rabbitmq_queue.py index ac5743fef55..1abd9dfc2d7 100644 --- a/messaging/rabbitmq_queue.py +++ b/messaging/rabbitmq_queue.py @@ -187,9 +187,6 @@ def main(): ): module.fail_json( msg = "RabbitMQ RESTAPI doesn't support attribute changes for existing queues", - details = "XPTO", - src = json.dumps(response['arguments']), - dest = json.dumps(module.params) ) From c7fe644c70885f1d9f4656915e24a4ae3039ee7b Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Thu, 14 May 2015 16:37:51 +0100 Subject: [PATCH 5/6] Make compatible with python 2.4 Removed one line if else --- messaging/rabbitmq_queue.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/messaging/rabbitmq_queue.py b/messaging/rabbitmq_queue.py index 1abd9dfc2d7..1fb274328b6 100644 --- a/messaging/rabbitmq_queue.py +++ b/messaging/rabbitmq_queue.py @@ -162,7 +162,10 @@ def main(): details = r.text ) - changeRequired = not queueExists if module.params['state']=='present' else queueExists + if module.params['state']=='present': + changeRequired = not queueExists + else: + changeRequired = queueExists # Check if attributes change on existing queue if not changeRequired and r.status_code==200 and module.params['state'] == 'present': @@ -170,19 +173,24 @@ def main(): response['durable'] == module.params['durable'] and response['auto_delete'] == module.params['autoDelete'] and ( - response['arguments']['x-message-ttl'] == module.params['messageTTL'] if 'x-message-ttl' in response['arguments'] else module.params['messageTTL'] is None + ( 'x-message-ttl' in response['arguments'] and response['arguments']['x-message-ttl'] == module.params['messageTTL'] ) or + ( 'x-message-ttl' not in response['arguments'] and module.params['messageTTL'] is None ) ) and ( - response['arguments']['x-expires'] == module.params['autoExpire'] if 'x-expires' in response['arguments'] else module.params['autoExpire'] is None + ( 'x-expires' in response['arguments'] and response['arguments']['x-expires'] == module.params['autoExpire'] ) or + ( 'x-expires' not in response['arguments'] and module.params['autoExpire'] is None ) ) and ( - response['arguments']['x-max-length'] == module.params['maxLength'] if 'x-max-length' in response['arguments'] else module.params['maxLength'] is None + ( 'x-max-length' in response['arguments'] and response['arguments']['x-max-length'] == module.params['maxLength'] ) or + ( 'x-max-length' not in response['arguments'] and module.params['maxLength'] is None ) ) and ( - response['arguments']['x-dead-letter-exchange'] == module.params['deadLetterExchange'] if 'x-dead-letter-exchange' in response['arguments'] else module.params['deadLetterExchange'] is None + ( 'x-dead-letter-exchange' in response['arguments'] and response['arguments']['x-dead-letter-exchange'] == module.params['deadLetterExchange'] ) or + ( 'x-dead-letter-exchange' not in response['arguments'] and module.params['deadLetterExchange'] is None ) ) and ( - response['arguments']['x-dead-letter-routing-key'] == module.params['deadLetterRoutingKey'] if 'x-dead-letter-routing-key' in response['arguments'] else module.params['deadLetterRoutingKey'] is None + ( 'x-dead-letter-routing-key' in response['arguments'] and response['arguments']['x-dead-letter-routing-key'] == module.params['deadLetterRoutingKey'] ) or + ( 'x-dead-letter-routing-key' not in response['arguments'] and module.params['deadLetterRoutingKey'] is None ) ) ): module.fail_json( From 46eedaf36894e78a4000889e83108775a67dd345 Mon Sep 17 00:00:00 2001 From: Manuel Sousa Date: Fri, 15 May 2015 16:08:10 +0100 Subject: [PATCH 6/6] Change variables from camel case to underscore --- messaging/rabbitmq_queue.py | 72 ++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/messaging/rabbitmq_queue.py b/messaging/rabbitmq_queue.py index 1fb274328b6..dc79874b766 100644 --- a/messaging/rabbitmq_queue.py +++ b/messaging/rabbitmq_queue.py @@ -72,30 +72,30 @@ options: required: false choices: [ "yes", "no" ] default: yes - autoDelete: + auto_delete: description: - if the queue should delete itself after all queues/queues unbound from it required: false choices: [ "yes", "no" ] default: no - messageTTL: + message_ttl: description: - How long a message can live in queue before it is discarded (milliseconds) required: False - autoExpires: + auto_expires: description: - How long a queue can be unused before it is automatically deleted (milliseconds) required: false - maxLength: + max_length: description: - How many messages can the queue contain before it starts rejecting required: false - deadLetterExchange: + dead_letter_exchange: description: - Optional name of an exchange to which messages will be republished if they - are rejected or expire required: false - deadLetterRoutingKey: + dead_letter_routing_key: description: - Optional replacement routing key to use when a message is dead-lettered. - Original routing key will be used if unset @@ -129,12 +129,12 @@ 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'), - messageTTL = dict(default=None, type='int'), - autoExpire = dict(default=None, type='int'), - maxLength = dict(default=None, type='int'), - deadLetterExchange = dict(default=None, type='str'), - deadLetterRoutingKey = dict(default=None, type='str'), + auto_delete = dict(default=False, choices=BOOLEANS, type='bool'), + message_ttl = dict(default=None, type='int'), + auto_expires = dict(default=None, type='int'), + max_length = dict(default=None, type='int'), + dead_letter_exchange = dict(default=None, type='str'), + dead_letter_routing_key = dict(default=None, type='str'), arguments = dict(default=dict(), type='dict') ), supports_check_mode = True @@ -151,10 +151,10 @@ def main(): r = requests.get( url, auth=(module.params['login_user'],module.params['login_password'])) if r.status_code==200: - queueExists = True + queue_exists = True response = r.json() elif r.status_code==404: - queueExists = False + queue_exists = False response = r.text else: module.fail_json( @@ -163,34 +163,34 @@ def main(): ) if module.params['state']=='present': - changeRequired = not queueExists + change_required = not queue_exists else: - changeRequired = queueExists + change_required = queue_exists # Check if attributes change on existing queue - 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 ( - ( 'x-message-ttl' in response['arguments'] and response['arguments']['x-message-ttl'] == module.params['messageTTL'] ) or - ( 'x-message-ttl' not in response['arguments'] and module.params['messageTTL'] is None ) + ( 'x-message-ttl' in response['arguments'] and response['arguments']['x-message-ttl'] == module.params['message_ttl'] ) or + ( 'x-message-ttl' not in response['arguments'] and module.params['message_ttl'] is None ) ) and ( - ( 'x-expires' in response['arguments'] and response['arguments']['x-expires'] == module.params['autoExpire'] ) or - ( 'x-expires' not in response['arguments'] and module.params['autoExpire'] is None ) + ( 'x-expires' in response['arguments'] and response['arguments']['x-expires'] == module.params['auto_expires'] ) or + ( 'x-expires' not in response['arguments'] and module.params['auto_expires'] is None ) ) and ( - ( 'x-max-length' in response['arguments'] and response['arguments']['x-max-length'] == module.params['maxLength'] ) or - ( 'x-max-length' not in response['arguments'] and module.params['maxLength'] is None ) + ( 'x-max-length' in response['arguments'] and response['arguments']['x-max-length'] == module.params['max_length'] ) or + ( 'x-max-length' not in response['arguments'] and module.params['max_length'] is None ) ) and ( - ( 'x-dead-letter-exchange' in response['arguments'] and response['arguments']['x-dead-letter-exchange'] == module.params['deadLetterExchange'] ) or - ( 'x-dead-letter-exchange' not in response['arguments'] and module.params['deadLetterExchange'] is None ) + ( 'x-dead-letter-exchange' in response['arguments'] and response['arguments']['x-dead-letter-exchange'] == module.params['dead_letter_exchange'] ) or + ( 'x-dead-letter-exchange' not in response['arguments'] and module.params['dead_letter_exchange'] is None ) ) and ( - ( 'x-dead-letter-routing-key' in response['arguments'] and response['arguments']['x-dead-letter-routing-key'] == module.params['deadLetterRoutingKey'] ) or - ( 'x-dead-letter-routing-key' not in response['arguments'] and module.params['deadLetterRoutingKey'] is None ) + ( 'x-dead-letter-routing-key' in response['arguments'] and response['arguments']['x-dead-letter-routing-key'] == module.params['dead_letter_routing_key'] ) or + ( 'x-dead-letter-routing-key' not in response['arguments'] and module.params['dead_letter_routing_key'] is None ) ) ): module.fail_json( @@ -200,11 +200,11 @@ def main(): # Copy parameters to arguments as used by RabbitMQ for k,v in { - 'messageTTL': 'x-message-ttl', - 'autoExpire': 'x-expires', - 'maxLength': 'x-max-length', - 'deadLetterExchange': 'x-dead-letter-exchange', - 'deadLetterRoutingKey': 'x-dead-letter-routing-key' + 'message_ttl': 'x-message-ttl', + 'auto_expires': 'x-expires', + 'max_length': 'x-max-length', + 'dead_letter_exchange': 'x-dead-letter-exchange', + 'dead_letter_routing_key': 'x-dead-letter-routing-key' }.items(): if module.params[k]: module.params['arguments'][v] = module.params[k] @@ -212,14 +212,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, @@ -227,7 +227,7 @@ 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'], "arguments": module.params['arguments'] }) )