Commit 37f096a6 by Matt Martz Committed by Michael DeHaan

rax modules improvements

* Catch issues with invalid regions
* Ensure we send string only data as meta values in the rax module
* Add public_key/lookup example for rax_keypair
* Clean up import statements
parent 2c7275e8
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -175,18 +175,18 @@ EXAMPLES = ''' ...@@ -175,18 +175,18 @@ EXAMPLES = '''
register: rax register: rax
''' '''
import sys
import time
import os import os
import re import re
import time
from uuid import UUID from uuid import UUID
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax is required for this module'") HAS_PYRAX = False
sys.exit(1)
ACTIVE_STATUSES = ('ACTIVE', 'BUILD', 'HARD_REBOOT', 'MIGRATING', 'PASSWORD', ACTIVE_STATUSES = ('ACTIVE', 'BUILD', 'HARD_REBOOT', 'MIGRATING', 'PASSWORD',
'REBOOT', 'REBUILD', 'RESCUE', 'RESIZE', 'REVERT_RESIZE') 'REBOOT', 'REBUILD', 'RESCUE', 'RESIZE', 'REVERT_RESIZE')
...@@ -379,8 +379,16 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files, ...@@ -379,8 +379,16 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
auto_increment, extra_create_args): auto_increment, extra_create_args):
cs = pyrax.cloudservers cs = pyrax.cloudservers
cnw = pyrax.cloud_networks cnw = pyrax.cloud_networks
if not cnw:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
servers = [] servers = []
for key, value in meta.items():
meta[key] = repr(value)
# Add the group meta key # Add the group meta key
if group and 'group' not in meta: if group and 'group' not in meta:
meta['group'] = group meta['group'] = group
...@@ -641,6 +649,9 @@ def main(): ...@@ -641,6 +649,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
service = module.params.get('service') service = module.params.get('service')
if service is not None: if service is not None:
...@@ -672,11 +683,6 @@ def main(): ...@@ -672,11 +683,6 @@ def main():
setup_rax_module(module, pyrax) setup_rax_module(module, pyrax)
if pyrax.cloudservers is None:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
if extra_client_args: if extra_client_args:
pyrax.cloudservers = pyrax.connect_to_cloudservers( pyrax.cloudservers = pyrax.connect_to_cloudservers(
region=pyrax.cloudservers.client.region_name, region=pyrax.cloudservers.client.region_name,
...@@ -685,6 +691,11 @@ def main(): ...@@ -685,6 +691,11 @@ def main():
if 'bypass_url' in extra_client_args: if 'bypass_url' in extra_client_args:
client.management_url = extra_client_args['bypass_url'] client.management_url = extra_client_args['bypass_url']
if pyrax.cloudservers is None:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
cloudservers(module, state, name, flavor, image, meta, key_name, files, cloudservers(module, state, name, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, count, group, wait, wait_timeout, disk_config, count, group,
instance_ids, exact_count, networks, count_offset, instance_ids, exact_count, networks, count_offset,
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -130,15 +130,13 @@ EXAMPLES = ''' ...@@ -130,15 +130,13 @@ EXAMPLES = '''
register: my_lb register: my_lb
''' '''
import sys
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType) NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
ALGORITHMS = ['RANDOM', 'LEAST_CONNECTIONS', 'ROUND_ROBIN', ALGORITHMS = ['RANDOM', 'LEAST_CONNECTIONS', 'ROUND_ROBIN',
...@@ -190,6 +188,10 @@ def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol, ...@@ -190,6 +188,10 @@ def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol,
balancers = [] balancers = []
clb = pyrax.cloud_loadbalancers clb = pyrax.cloud_loadbalancers
if not clb:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
for balancer in clb.list(): for balancer in clb.list():
if name != balancer.name and name != balancer.id: if name != balancer.name and name != balancer.id:
...@@ -308,6 +310,9 @@ def main(): ...@@ -308,6 +310,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
algorithm = module.params.get('algorithm') algorithm = module.params.get('algorithm')
meta = module.params.get('meta') meta = module.params.get('meta')
name = module.params.get('name') name = module.params.get('name')
......
...@@ -121,13 +121,12 @@ EXAMPLES = ''' ...@@ -121,13 +121,12 @@ EXAMPLES = '''
''' '''
import os import os
import sys
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax is required for this module'") HAS_PYRAX = False
sys.exit(1)
def _activate_virtualenv(path): def _activate_virtualenv(path):
...@@ -196,6 +195,9 @@ def main(): ...@@ -196,6 +195,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
address = module.params['address'] address = module.params['address']
condition = (module.params['condition'] and condition = (module.params['condition'] and
module.params['condition'].upper()) module.params['condition'].upper())
...@@ -219,8 +221,9 @@ def main(): ...@@ -219,8 +221,9 @@ def main():
setup_rax_module(module, pyrax) setup_rax_module(module, pyrax)
if not pyrax.cloud_loadbalancers: if not pyrax.cloud_loadbalancers:
module.fail_json(msg='Failed to instantiate load balancer client ' module.fail_json(msg='Failed to instantiate client. This '
'(possibly incorrect region)') 'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
try: try:
lb = pyrax.cloud_loadbalancers.get(load_balancer_id) lb = pyrax.cloud_loadbalancers.get(load_balancer_id)
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -62,16 +62,13 @@ EXAMPLES = ''' ...@@ -62,16 +62,13 @@ EXAMPLES = '''
register: rax_dns register: rax_dns
''' '''
import sys
import os
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType) NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
...@@ -89,6 +86,10 @@ def rax_dns(module, comment, email, name, state, ttl): ...@@ -89,6 +86,10 @@ def rax_dns(module, comment, email, name, state, ttl):
changed = False changed = False
dns = pyrax.cloud_dns dns = pyrax.cloud_dns
if not dns:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
if state == 'present': if state == 'present':
if not email: if not email:
...@@ -159,6 +160,9 @@ def main(): ...@@ -159,6 +160,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
comment = module.params.get('comment') comment = module.params.get('comment')
email = module.params.get('email') email = module.params.get('email')
name = module.params.get('name') name = module.params.get('name')
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -87,16 +87,13 @@ EXAMPLES = ''' ...@@ -87,16 +87,13 @@ EXAMPLES = '''
register: rax_dns_record register: rax_dns_record
''' '''
import sys
import os
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType) NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
...@@ -115,6 +112,10 @@ def rax_dns_record(module, comment, data, domain, name, priority, record_type, ...@@ -115,6 +112,10 @@ def rax_dns_record(module, comment, data, domain, name, priority, record_type,
changed = False changed = False
dns = pyrax.cloud_dns dns = pyrax.cloud_dns
if not dns:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
if state == 'present': if state == 'present':
if not priority and record_type in ['MX', 'SRV']: if not priority and record_type in ['MX', 'SRV']:
...@@ -211,6 +212,9 @@ def main(): ...@@ -211,6 +212,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
comment = module.params.get('comment') comment = module.params.get('comment')
data = module.params.get('data') data = module.params.get('data')
domain = module.params.get('domain') domain = module.params.get('domain')
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -55,16 +55,13 @@ EXAMPLES = ''' ...@@ -55,16 +55,13 @@ EXAMPLES = '''
ansible_ssh_host: "{{ rax_accessipv4 }}" ansible_ssh_host: "{{ rax_accessipv4 }}"
''' '''
import sys
import os
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType) NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
...@@ -87,6 +84,12 @@ def rax_facts(module, address, name, server_id): ...@@ -87,6 +84,12 @@ def rax_facts(module, address, name, server_id):
changed = False changed = False
cs = pyrax.cloudservers cs = pyrax.cloudservers
if cs is None:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
ansible_facts = {} ansible_facts = {}
search_opts = {} search_opts = {}
...@@ -139,6 +142,9 @@ def main(): ...@@ -139,6 +142,9 @@ def main():
required_one_of=[['address', 'id', 'name']], required_one_of=[['address', 'id', 'name']],
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
address = module.params.get('address') address = module.params.get('address')
server_id = module.params.get('id') server_id = module.params.get('id')
name = module.params.get('name') name = module.params.get('name')
......
#!/usr/bin/python -tt #!/usr/bin/python
# (c) 2013, Paul Durivage <paul.durivage@rackspace.com> # (c) 2013, Paul Durivage <paul.durivage@rackspace.com>
# #
...@@ -143,9 +143,9 @@ from ansible import __version__ ...@@ -143,9 +143,9 @@ from ansible import __version__
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError, e: except ImportError, e:
print("failed=True msg='pyrax is required for this module'") HAS_PYRAX = False
sys.exit(1)
EXIT_DICT = dict(success=True) EXIT_DICT = dict(success=True)
META_PREFIX = 'x-container-meta-' META_PREFIX = 'x-container-meta-'
...@@ -200,7 +200,8 @@ def meta(cf, module, container_, state, meta_, clear_meta): ...@@ -200,7 +200,8 @@ def meta(cf, module, container_, state, meta_, clear_meta):
module.exit_json(**EXIT_DICT) module.exit_json(**EXIT_DICT)
def container(cf, module, container_, state, meta_, clear_meta, ttl, public, private, web_index, web_error): def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
private, web_index, web_error):
if public and private: if public and private:
module.fail_json(msg='container cannot be simultaneously ' module.fail_json(msg='container cannot be simultaneously '
'set to public and private') 'set to public and private')
...@@ -297,9 +298,8 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public, pri ...@@ -297,9 +298,8 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public, pri
EXIT_DICT['container'] = c.name EXIT_DICT['container'] = c.name
EXIT_DICT['objs_in_container'] = c.object_count EXIT_DICT['objs_in_container'] = c.object_count
EXIT_DICT['total_bytes'] = c.total_bytes EXIT_DICT['total_bytes'] = c.total_bytes
_locals = locals().keys() _locals = locals().keys()
if ('cont_deleted' in _locals if ('cont_deleted' in _locals
or 'meta_set' in _locals or 'meta_set' in _locals
or 'cont_public' in _locals or 'cont_public' in _locals
...@@ -311,15 +311,23 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public, pri ...@@ -311,15 +311,23 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public, pri
module.exit_json(**EXIT_DICT) module.exit_json(**EXIT_DICT)
def cloudfiles(module, container_, state, meta_, clear_meta, typ, ttl, public, private, web_index, web_error): def cloudfiles(module, container_, state, meta_, clear_meta, typ, ttl, public,
""" Dispatch from here to work with metadata or file objects """ private, web_index, web_error):
cf = pyrax.cloudfiles """ Dispatch from here to work with metadata or file objects """
cf.user_agent = USER_AGENT cf = pyrax.cloudfiles
if typ == "container": if cf is None:
container(cf, module, container_, state, meta_, clear_meta, ttl, public, private, web_index, web_error) module.fail_json(msg='Failed to instantiate client. This '
else: 'typically indicates an invalid region or an '
meta(cf, module, container_, state, meta_, clear_meta) 'incorrectly capitalized region name.')
cf.user_agent = USER_AGENT
if typ == "container":
container(cf, module, container_, state, meta_, clear_meta, ttl,
public, private, web_index, web_error)
else:
meta(cf, module, container_, state, meta_, clear_meta)
def main(): def main():
...@@ -327,7 +335,8 @@ def main(): ...@@ -327,7 +335,8 @@ def main():
argument_spec.update( argument_spec.update(
dict( dict(
container=dict(), container=dict(),
state=dict(choices=['present', 'absent', 'list'], default='present'), state=dict(choices=['present', 'absent', 'list'],
default='present'),
meta=dict(type='dict', default=dict()), meta=dict(type='dict', default=dict()),
clear_meta=dict(default=False, type='bool'), clear_meta=dict(default=False, type='bool'),
type=dict(choices=['container', 'meta'], default='container'), type=dict(choices=['container', 'meta'], default='container'),
...@@ -344,6 +353,9 @@ def main(): ...@@ -344,6 +353,9 @@ def main():
required_together=rax_required_together() required_together=rax_required_together()
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
container_ = module.params.get('container') container_ = module.params.get('container')
state = module.params.get('state') state = module.params.get('state')
meta_ = module.params.get('meta') meta_ = module.params.get('meta')
...@@ -358,10 +370,12 @@ def main(): ...@@ -358,10 +370,12 @@ def main():
if state in ['present', 'absent'] and not container_: if state in ['present', 'absent'] and not container_:
module.fail_json(msg='please specify a container name') module.fail_json(msg='please specify a container name')
if clear_meta and not typ == 'meta': if clear_meta and not typ == 'meta':
module.fail_json(msg='clear_meta can only be used when setting metadata') module.fail_json(msg='clear_meta can only be used when setting '
'metadata')
setup_rax_module(module, pyrax) setup_rax_module(module, pyrax)
cloudfiles(module, container_, state, meta_, clear_meta, typ, ttl, public, private, web_index, web_error) cloudfiles(module, container_, state, meta_, clear_meta, typ, ttl, public,
private, web_index, web_error)
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
......
#!/usr/bin/python -tt #!/usr/bin/python
# (c) 2013, Paul Durivage <paul.durivage@rackspace.com> # (c) 2013, Paul Durivage <paul.durivage@rackspace.com>
# #
...@@ -187,9 +187,9 @@ import os ...@@ -187,9 +187,9 @@ import os
try: try:
import pyrax import pyrax
except ImportError, e: HAS_PYRAX = True
print("failed=True msg='pyrax is required for this module'") except ImportError:
sys.exit(1) HAS_PYRAX = False
EXIT_DICT = dict(success=False) EXIT_DICT = dict(success=False)
META_PREFIX = 'x-object-meta-' META_PREFIX = 'x-object-meta-'
...@@ -433,7 +433,6 @@ def get_meta(module, cf, container, src, dest): ...@@ -433,7 +433,6 @@ def get_meta(module, cf, container, src, dest):
meta_key = k.split(META_PREFIX)[-1] meta_key = k.split(META_PREFIX)[-1]
results[obj][meta_key] = v results[obj][meta_key] = v
EXIT_DICT['container'] = c.name EXIT_DICT['container'] = c.name
if results: if results:
EXIT_DICT['meta_results'] = results EXIT_DICT['meta_results'] = results
...@@ -530,28 +529,33 @@ def delete_meta(module, cf, container, src, dest, meta): ...@@ -530,28 +529,33 @@ def delete_meta(module, cf, container, src, dest, meta):
def cloudfiles(module, container, src, dest, method, typ, meta, clear_meta, def cloudfiles(module, container, src, dest, method, typ, meta, clear_meta,
structure, expires): structure, expires):
""" Dispatch from here to work with metadata or file objects """ """ Dispatch from here to work with metadata or file objects """
cf = pyrax.cloudfiles cf = pyrax.cloudfiles
if typ == "file": if cf is None:
if method == 'put': module.fail_json(msg='Failed to instantiate client. This '
upload(module, cf, container, src, dest, meta, expires) 'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
elif method == 'get': if typ == "file":
download(module, cf, container, src, dest, structure) if method == 'put':
upload(module, cf, container, src, dest, meta, expires)
elif method == 'delete': elif method == 'get':
delete(module, cf, container, src, dest) download(module, cf, container, src, dest, structure)
else: elif method == 'delete':
if method == 'get': delete(module, cf, container, src, dest)
get_meta(module, cf, container, src, dest)
if method == 'put': else:
put_meta(module, cf, container, src, dest, meta, clear_meta) if method == 'get':
get_meta(module, cf, container, src, dest)
if method == 'delete': if method == 'put':
delete_meta(module, cf, container, src, dest, meta) put_meta(module, cf, container, src, dest, meta, clear_meta)
if method == 'delete':
delete_meta(module, cf, container, src, dest, meta)
def main(): def main():
...@@ -575,6 +579,9 @@ def main(): ...@@ -575,6 +579,9 @@ def main():
required_together=rax_required_together() required_together=rax_required_together()
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
container = module.params.get('container') container = module.params.get('container')
src = module.params.get('src') src = module.params.get('src')
dest = module.params.get('dest') dest = module.params.get('dest')
...@@ -595,4 +602,4 @@ def main(): ...@@ -595,4 +602,4 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.rax import * from ansible.module_utils.rax import *
main() main()
\ No newline at end of file
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -68,15 +68,13 @@ EXAMPLES = ''' ...@@ -68,15 +68,13 @@ EXAMPLES = '''
register: rackspace_identity register: rackspace_identity
''' '''
import sys
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType) NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
...@@ -118,10 +116,18 @@ def main(): ...@@ -118,10 +116,18 @@ def main():
required_together=rax_required_together() required_together=rax_required_together()
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
state = module.params.get('state') state = module.params.get('state')
setup_rax_module(module, pyrax) setup_rax_module(module, pyrax)
if pyrax.identity is None:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
cloud_identity(module, state, pyrax.identity) cloud_identity(module, state, pyrax.identity)
# import module snippets # import module snippets
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -48,7 +48,7 @@ extends_documentation_fragment: rackspace.openstack ...@@ -48,7 +48,7 @@ extends_documentation_fragment: rackspace.openstack
EXAMPLES = ''' EXAMPLES = '''
- name: Create a keypair - name: Create a keypair
hosts: local hosts: localhost
gather_facts: False gather_facts: False
tasks: tasks:
- name: keypair request - name: keypair request
...@@ -68,17 +68,28 @@ EXAMPLES = ''' ...@@ -68,17 +68,28 @@ EXAMPLES = '''
module: copy module: copy
content: "{{ keypair.keypair.private_key }}" content: "{{ keypair.keypair.private_key }}"
dest: "{{ inventory_dir }}/{{ keypair.keypair.name }}" dest: "{{ inventory_dir }}/{{ keypair.keypair.name }}"
'''
import sys - name: Create a keypair
hosts: localhost
gather_facts: False
tasks:
- name: keypair request
local_action:
module: rax_keypair
credentials: ~/.raxpub
name: my_keypair
public_key: "{{ lookup('file', 'authorized_keys/id_rsa.pub') }}"
region: DFW
register: keypair
'''
from types import NoneType from types import NoneType
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType) NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
...@@ -96,6 +107,12 @@ def rax_keypair(module, name, public_key, state): ...@@ -96,6 +107,12 @@ def rax_keypair(module, name, public_key, state):
changed = False changed = False
cs = pyrax.cloudservers cs = pyrax.cloudservers
if cs is None:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
keypair = {} keypair = {}
if state == 'present': if state == 'present':
...@@ -141,6 +158,9 @@ def main(): ...@@ -141,6 +158,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
name = module.params.get('name') name = module.params.get('name')
public_key = module.params.get('public_key') public_key = module.params.get('public_key')
state = module.params.get('state') state = module.params.get('state')
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -57,16 +57,11 @@ EXAMPLES = ''' ...@@ -57,16 +57,11 @@ EXAMPLES = '''
state: present state: present
''' '''
import sys
import os
try: try:
import pyrax import pyrax
import pyrax.utils HAS_PYRAX = True
from pyrax import exc
except ImportError: except ImportError:
print("failed=True msg='pyrax required for this module'") HAS_PYRAX = False
sys.exit(1)
def cloud_network(module, state, label, cidr): def cloud_network(module, state, label, cidr):
...@@ -78,10 +73,15 @@ def cloud_network(module, state, label, cidr): ...@@ -78,10 +73,15 @@ def cloud_network(module, state, label, cidr):
network = None network = None
networks = [] networks = []
if not pyrax.cloud_networks:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
if state == 'present': if state == 'present':
try: try:
network = pyrax.cloud_networks.find_network_by_label(label) network = pyrax.cloud_networks.find_network_by_label(label)
except exc.NetworkNotFound: except pyrax.exceptions.NetworkNotFound:
try: try:
network = pyrax.cloud_networks.create(label, cidr=cidr) network = pyrax.cloud_networks.create(label, cidr=cidr)
changed = True changed = True
...@@ -95,7 +95,7 @@ def cloud_network(module, state, label, cidr): ...@@ -95,7 +95,7 @@ def cloud_network(module, state, label, cidr):
network = pyrax.cloud_networks.find_network_by_label(label) network = pyrax.cloud_networks.find_network_by_label(label)
network.delete() network.delete()
changed = True changed = True
except exc.NetworkNotFound: except pyrax.exceptions.NetworkNotFound:
pass pass
except Exception, e: except Exception, e:
module.fail_json(msg='%s' % e.message) module.fail_json(msg='%s' % e.message)
...@@ -125,6 +125,9 @@ def main(): ...@@ -125,6 +125,9 @@ def main():
required_together=rax_required_together(), required_together=rax_required_together(),
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
state = module.params.get('state') state = module.params.get('state')
label = module.params.get('label') label = module.params.get('label')
cidr = module.params.get('cidr') cidr = module.params.get('cidr')
......
#!/usr/bin/python -tt #!/usr/bin/python
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
...@@ -55,15 +55,11 @@ EXAMPLES = ''' ...@@ -55,15 +55,11 @@ EXAMPLES = '''
register: my_queue register: my_queue
''' '''
import sys
import os
try: try:
import pyrax import pyrax
HAS_PYRAX = True
except ImportError: except ImportError:
print("failed=True msg='pyrax is required for this module'") HAS_PYRAX = False
sys.exit(1)
def cloud_queue(module, state, name): def cloud_queue(module, state, name):
...@@ -76,6 +72,10 @@ def cloud_queue(module, state, name): ...@@ -76,6 +72,10 @@ def cloud_queue(module, state, name):
instance = {} instance = {}
cq = pyrax.queues cq = pyrax.queues
if not cq:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
for queue in cq.list(): for queue in cq.list():
if name != queue.name: if name != queue.name:
...@@ -126,6 +126,9 @@ def main(): ...@@ -126,6 +126,9 @@ def main():
required_together=rax_required_together() required_together=rax_required_together()
) )
if not HAS_PYRAX:
module.fail_json(msg='pyrax is required for this module')
name = module.params.get('name') name = module.params.get('name')
state = module.params.get('state') state = module.params.get('state')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment