Pep8 fixes for rabbitmq modules (#24590)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-08-10 15:27:11 +05:30 committed by René Moser
parent 85fc4c67ef
commit f7321a87ca
9 changed files with 192 additions and 207 deletions

View file

@ -116,117 +116,110 @@ from ansible.module_utils.six.moves.urllib import parse as urllib_parse
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'),
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'),
destination = dict(required=True, aliases=[ "dst", "dest"], type='str'),
destination_type = dict(required=True, aliases=[ "type", "dest_type"], choices=[ "queue", "exchange" ],type='str'),
routing_key = dict(default='#', type='str'),
arguments = dict(default=dict(), type='dict')
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'),
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'),
destination=dict(required=True, aliases=["dst", "dest"], type='str'),
destination_type=dict(required=True, aliases=["type", "dest_type"], choices=["queue", "exchange"], type='str'),
routing_key=dict(default='#', type='str'),
arguments=dict(default=dict(), type='dict')
),
supports_check_mode = True
supports_check_mode=True
)
if not HAS_REQUESTS:
module.fail_json(msg="requests library is required for this module. To install, use `pip install requests`")
result = dict(changed=False, name=module.params['name'])
if module.params['destination_type'] == "queue":
dest_type="q"
dest_type = "q"
else:
dest_type="e"
dest_type = "e"
if module.params['routing_key'] == "":
props = "~"
else:
props = urllib_parse.quote(module.params['routing_key'],'')
props = urllib_parse.quote(module.params['routing_key'], '')
url = "http://%s:%s/api/bindings/%s/e/%s/%s/%s/%s" % (
module.params['login_host'],
module.params['login_port'],
urllib_parse.quote(module.params['vhost'],''),
urllib_parse.quote(module.params['name'],''),
dest_type,
urllib_parse.quote(module.params['destination'],''),
props
)
base_url = "http://%s:%s/api/bindings" % (module.params['login_host'], module.params['login_port'])
url = "%s/%s/e/%s/%s/%s/%s" % (base_url,
urllib_parse.quote(module.params['vhost'], ''),
urllib_parse.quote(module.params['name'], ''),
dest_type,
urllib_parse.quote(module.params['destination'], ''),
props
)
# Check if exchange already exists
r = requests.get( url, auth=(module.params['login_user'],module.params['login_password']))
r = requests.get(url, auth=(module.params['login_user'], module.params['login_password']))
if r.status_code==200:
if r.status_code == 200:
binding_exists = True
response = r.json()
elif r.status_code==404:
elif r.status_code == 404:
binding_exists = False
response = r.text
else:
module.fail_json(
msg = "Invalid response from RESTAPI when trying to check if exchange exists",
details = r.text
msg="Invalid response from RESTAPI when trying to check if exchange exists",
details=r.text
)
if module.params['state']=='present':
if module.params['state'] == 'present':
change_required = not binding_exists
else:
change_required = binding_exists
# Exit if check_mode
if module.check_mode:
module.exit_json(
changed= change_required,
name = module.params['name'],
details = response,
arguments = module.params['arguments']
)
result['changed'] = change_required
result['details'] = response
result['arguments'] = module.params['arguments']
module.exit_json(**result)
# Do changes
if change_required:
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_parse.quote(module.params['vhost'],''),
urllib_parse.quote(module.params['name'],''),
url = "%s/%s/e/%s/%s/%s" % (
base_url,
urllib_parse.quote(module.params['vhost'], ''),
urllib_parse.quote(module.params['name'], ''),
dest_type,
urllib_parse.quote(module.params['destination'],'')
urllib_parse.quote(module.params['destination'], '')
)
r = requests.post(
url,
auth = (module.params['login_user'],module.params['login_password']),
headers = { "content-type": "application/json"},
data = json.dumps({
auth=(module.params['login_user'], module.params['login_password']),
headers={"content-type": "application/json"},
data=json.dumps({
"routing_key": module.params['routing_key'],
"arguments": module.params['arguments']
})
})
)
elif module.params['state'] == 'absent':
r = requests.delete( url, auth = (module.params['login_user'],module.params['login_password']))
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']
)
result['changed'] = True
result['destination'] = module.params['destination']
module.exit_json(**result)
else:
module.fail_json(
msg = "Error creating exchange",
status = r.status_code,
details = r.text
msg="Error creating exchange",
status=r.status_code,
details=r.text
)
else:
module.exit_json(
changed = False,
name = module.params['name']
)
result['changed'] = False
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -118,55 +118,57 @@ from ansible.module_utils.six.moves.urllib import parse as urllib_parse
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', no_log=True),
login_host = dict(default='localhost', type='str'),
login_port = dict(default='15672', type='str'),
vhost = dict(default='/', type='str'),
durable = dict(default=True, type='bool'),
auto_delete = dict(default=False, type='bool'),
internal = dict(default=False, type='bool'),
exchange_type = dict(default='direct', aliases=['type'], type='str'),
arguments = dict(default=dict(), type='dict')
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', no_log=True),
login_host=dict(default='localhost', type='str'),
login_port=dict(default='15672', type='str'),
vhost=dict(default='/', type='str'),
durable=dict(default=True, type='bool'),
auto_delete=dict(default=False, type='bool'),
internal=dict(default=False, type='bool'),
exchange_type=dict(default='direct', aliases=['type'], type='str'),
arguments=dict(default=dict(), type='dict')
),
supports_check_mode = True
supports_check_mode=True
)
result = dict(changed=False, name=module.params['name'])
url = "http://%s:%s/api/exchanges/%s/%s" % (
module.params['login_host'],
module.params['login_port'],
urllib_parse.quote(module.params['vhost'],''),
urllib_parse.quote(module.params['name'],'')
urllib_parse.quote(module.params['vhost'], ''),
urllib_parse.quote(module.params['name'], '')
)
if not HAS_REQUESTS:
module.fail_json(msg="requests library is required for this module. To install, use `pip install requests`")
# Check if exchange already exists
r = requests.get( url, auth=(module.params['login_user'],module.params['login_password']))
r = requests.get(url, auth=(module.params['login_user'], module.params['login_password']))
if r.status_code==200:
if r.status_code == 200:
exchange_exists = True
response = r.json()
elif r.status_code==404:
elif r.status_code == 404:
exchange_exists = False
response = r.text
else:
module.fail_json(
msg = "Invalid response from RESTAPI when trying to check if exchange exists",
details = r.text
msg="Invalid response from RESTAPI when trying to check if exchange exists",
details=r.text
)
if module.params['state']=='present':
if module.params['state'] == 'present':
change_required = not exchange_exists
else:
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':
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['auto_delete'] and
@ -174,55 +176,48 @@ def main():
response['type'] == module.params['exchange_type']
):
module.fail_json(
msg = "RabbitMQ RESTAPI doesn't support attribute changes for existing exchanges"
msg="RabbitMQ RESTAPI doesn't support attribute changes for existing exchanges"
)
# Exit if check_mode
if module.check_mode:
module.exit_json(
changed= change_required,
name = module.params['name'],
details = response,
arguments = module.params['arguments']
)
result['changed'] = change_required
result['details'] = response
result['arguments'] = module.params['arguments']
module.exit_json(**result)
# Do changes
if change_required:
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({
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['auto_delete'],
"internal": module.params['internal'],
"type": module.params['exchange_type'],
"arguments": module.params['arguments']
})
})
)
elif module.params['state'] == 'absent':
r = requests.delete( url, auth = (module.params['login_user'],module.params['login_password']))
r = requests.delete(url, auth=(module.params['login_user'], module.params['login_password']))
# RabbitMQ 3.6.7 changed this response code from 204 to 201
if r.status_code == 204 or r.status_code == 201:
module.exit_json(
changed = True,
name = module.params['name']
)
result['changed'] = True
module.exit_json(**result)
else:
module.fail_json(
msg = "Error creating exchange",
status = r.status_code,
details = r.text
msg="Error creating exchange",
status=r.status_code,
details=r.text
)
else:
module.exit_json(
changed = False,
name = module.params['name']
)
result['changed'] = False
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -65,7 +65,6 @@ EXAMPLES = """
state: present
"""
import json
from ansible.module_utils.basic import AnsibleModule
@ -114,6 +113,7 @@ class RabbitMqParameter(object):
def has_modifications(self):
return self.value != self._value
def main():
arg_spec = dict(
component=dict(required=True),
@ -137,23 +137,26 @@ def main():
state = module.params['state']
node = module.params['node']
result = dict(changed=False)
rabbitmq_parameter = RabbitMqParameter(module, component, name, value, vhost, node)
changed = False
if rabbitmq_parameter.get():
if state == 'absent':
rabbitmq_parameter.delete()
changed = True
result['changed'] = True
else:
if rabbitmq_parameter.has_modifications():
rabbitmq_parameter.set()
changed = True
result['changed'] = True
elif state == 'present':
rabbitmq_parameter.set()
changed = True
module.exit_json(changed=changed, component=component, name=name, vhost=vhost, state=state)
result['changed'] = True
result['component'] = component
result['name'] = name
result['vhost'] = vhost
result['state'] = state
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -57,11 +57,14 @@ EXAMPLES = '''
'''
import os
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import AnsibleModule
class RabbitMqPlugins(object):
def __init__(self, module):
self.module = module
@ -73,7 +76,7 @@ class RabbitMqPlugins(object):
else:
# No such path exists.
raise Exception("No binary folder in prefix %s" %
module.params['prefix'])
module.params['prefix'])
self._rabbitmq_plugins = bin_path + "/rabbitmq-plugins"
@ -116,6 +119,7 @@ def main():
supports_check_mode=True
)
result = dict()
names = module.params['names'].split(',')
new_only = module.params['new_only']
state = module.params['state']
@ -142,8 +146,10 @@ def main():
rabbitmq_plugins.disable(plugin)
disabled.append(plugin)
changed = len(enabled) > 0 or len(disabled) > 0
module.exit_json(changed=changed, enabled=enabled, disabled=disabled)
result['changed'] = len(enabled) > 0 or len(disabled) > 0
result['enabled'] = enabled
result['disabled'] = disabled
module.exit_json(**result)
if __name__ == '__main__':

View file

@ -82,10 +82,13 @@ EXAMPLES = '''
tags:
ha-mode: all
'''
import json
from ansible.module_utils.basic import AnsibleModule
class RabbitMqPolicy(object):
def __init__(self, module, name):
self._module = module
self._name = name
@ -116,14 +119,13 @@ class RabbitMqPolicy(object):
return False
def set(self):
import json
args = ['set_policy']
args.append(self._name)
args.append(self._pattern)
args.append(json.dumps(self._tags))
args.append('--priority')
args.append(self._priority)
if (self._apply_to != 'all'):
if self._apply_to != 'all':
args.append('--apply-to')
args.append(self._apply_to)
return self._exec(args)
@ -153,19 +155,18 @@ def main():
state = module.params['state']
rabbitmq_policy = RabbitMqPolicy(module, name)
changed = False
result = dict(changed=False, name=name, state=state)
if rabbitmq_policy.list():
if state == 'absent':
rabbitmq_policy.clear()
changed = True
result['changed'] = True
else:
changed = False
result['changed'] = False
elif state == 'present':
rabbitmq_policy.set()
changed = True
module.exit_json(changed=changed, name=name, state=state)
result['changed'] = True
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -133,72 +133,74 @@ from ansible.module_utils.six.moves.urllib import parse as urllib_parse
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', no_log=True),
login_host = dict(default='localhost', type='str'),
login_port = dict(default='15672', type='str'),
vhost = dict(default='/', type='str'),
durable = dict(default=True, type='bool'),
auto_delete = dict(default=False, 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')
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', no_log=True),
login_host=dict(default='localhost', type='str'),
login_port=dict(default='15672', type='str'),
vhost=dict(default='/', type='str'),
durable=dict(default=True, type='bool'),
auto_delete=dict(default=False, 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
supports_check_mode=True
)
url = "http://%s:%s/api/queues/%s/%s" % (
module.params['login_host'],
module.params['login_port'],
urllib_parse.quote(module.params['vhost'],''),
urllib_parse.quote(module.params['vhost'], ''),
module.params['name']
)
if not HAS_REQUESTS:
module.fail_json(msg="requests library is required for this module. To install, use `pip install requests`")
# Check if queue already exists
r = requests.get( url, auth=(module.params['login_user'],module.params['login_password']))
result = dict(changed=False, name=module.params['name'])
if r.status_code==200:
# Check if queue already exists
r = requests.get(url, auth=(module.params['login_user'], module.params['login_password']))
if r.status_code == 200:
queue_exists = True
response = r.json()
elif r.status_code==404:
elif r.status_code == 404:
queue_exists = False
response = r.text
else:
module.fail_json(
msg = "Invalid response from RESTAPI when trying to check if queue exists",
details = r.text
msg="Invalid response from RESTAPI when trying to check if queue exists",
details=r.text
)
if module.params['state']=='present':
if module.params['state'] == 'present':
change_required = not queue_exists
else:
change_required = queue_exists
# Check if attributes change on existing queue
if not change_required 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['auto_delete'] and
(
( '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 )
('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['auto_expires'] ) or
( 'x-expires' not in response['arguments'] and module.params['auto_expires'] 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['max_length'] ) or
( 'x-max-length' not in response['arguments'] and module.params['max_length'] 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
@ -212,12 +214,11 @@ def main():
)
):
module.fail_json(
msg = "RabbitMQ RESTAPI doesn't support attribute changes for existing queues",
msg="RabbitMQ RESTAPI doesn't support attribute changes for existing queues",
)
# Copy parameters to arguments as used by RabbitMQ
for k,v in {
for k, v in {
'message_ttl': 'x-message-ttl',
'auto_expires': 'x-expires',
'max_length': 'x-max-length',
@ -229,48 +230,41 @@ def main():
# Exit if check_mode
if module.check_mode:
module.exit_json(
changed= change_required,
name = module.params['name'],
details = response,
arguments = module.params['arguments']
)
result['changed'] = change_required
result['details'] = response
result['arguments'] = module.params['arguments']
module.exit_json(**result)
# Do changes
if change_required:
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({
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['auto_delete'],
"arguments": module.params['arguments']
})
})
)
elif module.params['state'] == 'absent':
r = requests.delete( url, auth = (module.params['login_user'],module.params['login_password']))
r = requests.delete(url, auth=(module.params['login_user'], module.params['login_password']))
# RabbitMQ 3.6.7 changed this response code from 204 to 201
if r.status_code == 204 or r.status_code == 201:
module.exit_json(
changed = True,
name = module.params['name']
)
result['changed'] = True
module.exit_json(**result)
else:
module.fail_json(
msg = "Error creating queue",
status = r.status_code,
details = r.text
msg="Error creating queue",
status=r.status_code,
details=r.text
)
else:
module.exit_json(
changed = False,
name = module.params['name']
)
result['changed'] = False
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -165,7 +165,7 @@ class RabbitMqUser(object):
user, tags = user_tag.split('\t')
if user == self.username:
for c in ['[',']',' ']:
for c in ['[', ']', ' ']:
tags = tags.replace(c, '')
if tags != '':
@ -229,6 +229,7 @@ class RabbitMqUser(object):
def has_permissions_modifications(self):
return sorted(self._permissions) != sorted(self.permissions)
def main():
arg_spec = dict(
user=dict(required=True, aliases=['username', 'name']),
@ -261,7 +262,7 @@ def main():
node = module.params['node']
bulk_permissions = True
if permissions == []:
if not permissions:
perm = {
'vhost': vhost,
'configure_priv': configure_priv,
@ -274,33 +275,33 @@ def main():
rabbitmq_user = RabbitMqUser(module, username, password, tags, permissions,
node, bulk_permissions=bulk_permissions)
changed = False
result = dict(changed=False, user=username, state=state)
if rabbitmq_user.get():
if state == 'absent':
rabbitmq_user.delete()
changed = True
result['changed'] = True
else:
if force:
rabbitmq_user.delete()
rabbitmq_user.add()
rabbitmq_user.get()
changed = True
result['changed'] = True
if rabbitmq_user.has_tags_modifications():
rabbitmq_user.set_tags()
changed = True
result['changed'] = True
if rabbitmq_user.has_permissions_modifications():
rabbitmq_user.set_permissions()
changed = True
result['changed'] = True
elif state == 'present':
rabbitmq_user.add()
rabbitmq_user.set_tags()
rabbitmq_user.set_permissions()
changed = True
module.exit_json(changed=changed, user=username, state=state)
result['changed'] = True
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -123,24 +123,22 @@ def main():
tracing = module.params['tracing']
state = module.params['state']
node = module.params['node']
result = dict(changed=False, name=name, state=state)
rabbitmq_vhost = RabbitMqVhost(module, name, tracing, node)
changed = False
if rabbitmq_vhost.get():
if state == 'absent':
rabbitmq_vhost.delete()
changed = True
result['changed'] = True
else:
if rabbitmq_vhost.set_tracing():
changed = True
result['changed'] = True
elif state == 'present':
rabbitmq_vhost.add()
rabbitmq_vhost.set_tracing()
changed = True
module.exit_json(changed=changed, name=name, state=state)
result['changed'] = True
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -216,12 +216,6 @@ lib/ansible/modules/files/replace.py
lib/ansible/modules/files/synchronize.py
lib/ansible/modules/files/tempfile.py
lib/ansible/modules/files/xattr.py
lib/ansible/modules/messaging/rabbitmq_binding.py
lib/ansible/modules/messaging/rabbitmq_exchange.py
lib/ansible/modules/messaging/rabbitmq_parameter.py
lib/ansible/modules/messaging/rabbitmq_plugin.py
lib/ansible/modules/messaging/rabbitmq_queue.py
lib/ansible/modules/messaging/rabbitmq_user.py
lib/ansible/modules/monitoring/airbrake_deployment.py
lib/ansible/modules/monitoring/bigpanda.py
lib/ansible/modules/monitoring/boundary_meter.py