Fixing Rackspace compile time errors irt exception handling for Python 3 (#3849)

This commit is contained in:
@ 2016-06-03 06:39:59 -07:00 committed by Matt Clay
parent 7960e99310
commit 7e4b1ebff5
19 changed files with 94 additions and 94 deletions

View file

@ -291,7 +291,7 @@ def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
f = open(user_data)
user_data = f.read()
f.close()
except Exception, e:
except Exception as e:
module.fail_json(msg='Failed to load %s' % user_data)
# Handle the file contents
@ -301,7 +301,7 @@ def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
fileobj = open(lpath, 'r')
files[rpath] = fileobj.read()
fileobj.close()
except Exception, e:
except Exception as e:
module.fail_json(msg='Failed to load %s' % lpath)
try:
servers = []
@ -316,7 +316,7 @@ def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
userdata=user_data,
block_device_mapping_v2=bdmv2,
**extra_create_args))
except Exception, e:
except Exception as e:
if e.message:
msg = str(e.message)
else:
@ -398,7 +398,7 @@ def delete(module, instance_ids=[], wait=True, wait_timeout=300, kept=[]):
for server in servers:
try:
server.delete()
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
changed = True
@ -546,7 +546,7 @@ def cloudservers(module, state=None, name=None, flavor=None, image=None,
# %d to the end
try:
name % 0
except TypeError, e:
except TypeError as e:
if e.message.startswith('not all'):
name = '%s%%d' % name
else:
@ -636,7 +636,7 @@ def cloudservers(module, state=None, name=None, flavor=None, image=None,
# %d to the end
try:
name % 0
except TypeError, e:
except TypeError as e:
if e.message.startswith('not all'):
name = '%s%%d' % name
else:

View file

@ -151,7 +151,7 @@ def cloud_block_storage(module, state, name, description, meta, size,
metadata=meta,
snapshot_id=snapshot_id, **kwargs)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
if wait:
@ -180,7 +180,7 @@ def cloud_block_storage(module, state, name, description, meta, size,
try:
volume.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, volume=instance)

View file

@ -120,7 +120,7 @@ def cloud_block_storage_attachments(module, state, volume, server, device,
try:
volume.attach_to_instance(server, mountpoint=device)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
volume.get()
@ -159,7 +159,7 @@ def cloud_block_storage_attachments(module, state, volume, server, device,
interval=3, attempts=0,
verbose=False)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
volume.get()

View file

@ -136,7 +136,7 @@ def save_instance(module, name, flavor, volume, cdb_type, cdb_version, wait,
try:
instance = cdb.create(name=name, flavor=flavor, volume=volume,
type=cdb_type, version=cdb_version)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True
@ -187,7 +187,7 @@ def delete_instance(module, name, wait, wait_timeout):
try:
instance.delete()
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True

View file

@ -83,7 +83,7 @@ def save_database(module, cdb_id, name, character_set, collate):
try:
instance = cdb.get(cdb_id)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
changed = False
@ -95,7 +95,7 @@ def save_database(module, cdb_id, name, character_set, collate):
database = instance.create_database(name=name,
character_set=character_set,
collate=collate)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True
@ -109,7 +109,7 @@ def delete_database(module, cdb_id, name):
try:
instance = cdb.get(cdb_id)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
changed = False
@ -119,7 +119,7 @@ def delete_database(module, cdb_id, name):
if database:
try:
database.delete()
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True

View file

@ -98,7 +98,7 @@ def save_user(module, cdb_id, name, password, databases, host):
try:
instance = cdb.get(cdb_id)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
changed = False
@ -112,7 +112,7 @@ def save_user(module, cdb_id, name, password, databases, host):
password=password,
database_names=databases,
host=host)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True
@ -134,7 +134,7 @@ def save_user(module, cdb_id, name, password, databases, host):
new_dbs = [db for db in databases if db not in former_dbs]
user.grant_user_access(db_names=new_dbs)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True
@ -153,7 +153,7 @@ def delete_user(module, cdb_id, name):
try:
instance = cdb.get(cdb_id)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
changed = False
@ -163,7 +163,7 @@ def delete_user(module, cdb_id, name):
if user:
try:
user.delete()
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
changed = True

View file

@ -182,7 +182,7 @@ def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol,
algorithm=algorithm, protocol=protocol,
timeout=timeout, virtual_ips=virtual_ips)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
balancer = balancers[0]
@ -240,7 +240,7 @@ def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol,
try:
balancer.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
instance = rax_to_dict(balancer, 'clb')

View file

@ -192,7 +192,7 @@ def main():
if virtualenv:
try:
_activate_virtualenv(virtualenv)
except IOError, e:
except IOError as e:
module.fail_json(msg='Failed to activate virtualenv %s (%s)' % (
virtualenv, e))
@ -205,7 +205,7 @@ def main():
try:
lb = pyrax.cloud_loadbalancers.get(load_balancer_id)
except pyrax.exc.PyraxException, e:
except pyrax.exc.PyraxException as e:
module.fail_json(msg='%s' % e.message)
node = _get_node(lb, node_id, address, port)
@ -220,7 +220,7 @@ def main():
result = {}
except pyrax.exc.NotFound:
module.exit_json(changed=False, state=state)
except pyrax.exc.PyraxException, e:
except pyrax.exc.PyraxException as e:
module.fail_json(msg='%s' % e.message)
else: # present
if not node:
@ -237,7 +237,7 @@ def main():
weight=weight, type=typ)
resp, body = lb.add_nodes([node])
result.update(body['nodes'][0])
except pyrax.exc.PyraxException, e:
except pyrax.exc.PyraxException as e:
module.fail_json(msg='%s' % e.message)
else: # Updating an existing node
mutable = {
@ -258,7 +258,7 @@ def main():
# type; this should probably be fixed in pyrax
lb.update_node(node, diff=mutable)
result.update(mutable)
except pyrax.exc.PyraxException, e:
except pyrax.exc.PyraxException as e:
module.fail_json(msg='%s' % e.message)
if wait:

View file

@ -89,14 +89,14 @@ def rax_dns(module, comment, email, name, state, ttl):
try:
domain = dns.find(name=name)
except pyrax.exceptions.NoUniqueMatch, e:
except pyrax.exceptions.NoUniqueMatch as e:
module.fail_json(msg='%s' % e.message)
except pyrax.exceptions.NotFound:
try:
domain = dns.create(name=name, emailAddress=email, ttl=ttl,
comment=comment)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
update = {}
@ -112,7 +112,7 @@ def rax_dns(module, comment, email, name, state, ttl):
domain.update(**update)
changed = True
domain.get()
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif state == 'absent':
@ -121,14 +121,14 @@ def rax_dns(module, comment, email, name, state, ttl):
except pyrax.exceptions.NotFound:
domain = {}
pass
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
if domain:
try:
domain.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, domain=rax_to_dict(domain))

View file

@ -152,7 +152,7 @@ def rax_dns_record_ptr(module, data=None, comment=None, loadbalancer=None,
try:
dns.update_ptr_record(item, record, name, data, ttl)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
record.ttl = ttl
record.name = name
@ -168,7 +168,7 @@ def rax_dns_record_ptr(module, data=None, comment=None, loadbalancer=None,
try:
results = dns.add_ptr_records(item, [record])
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, records=results)
@ -184,7 +184,7 @@ def rax_dns_record_ptr(module, data=None, comment=None, loadbalancer=None,
try:
dns.delete_ptr_records(item, data)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, records=results)
@ -210,7 +210,7 @@ def rax_dns_record(module, comment=None, data=None, domain=None, name=None,
try:
domain = dns.find(name=domain)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
try:
@ -218,9 +218,9 @@ def rax_dns_record(module, comment=None, data=None, domain=None, name=None,
record = domain.find_record(record_type, name=name)
else:
record = domain.find_record(record_type, name=name, data=data)
except pyrax.exceptions.DomainRecordNotUnique, e:
except pyrax.exceptions.DomainRecordNotUnique as e:
module.fail_json(msg='overwrite=true and there are multiple matching records')
except pyrax.exceptions.DomainRecordNotFound, e:
except pyrax.exceptions.DomainRecordNotFound as e:
try:
record_data = {
'type': record_type,
@ -235,7 +235,7 @@ def rax_dns_record(module, comment=None, data=None, domain=None, name=None,
record = domain.add_records([record_data])[0]
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
update = {}
@ -253,28 +253,28 @@ def rax_dns_record(module, comment=None, data=None, domain=None, name=None,
record.update(**update)
changed = True
record.get()
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif state == 'absent':
try:
domain = dns.find(name=domain)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
try:
record = domain.find_record(record_type, name=name, data=data)
except pyrax.exceptions.DomainRecordNotFound, e:
except pyrax.exceptions.DomainRecordNotFound as e:
record = {}
pass
except pyrax.exceptions.DomainRecordNotUnique, e:
except pyrax.exceptions.DomainRecordNotUnique as e:
module.fail_json(msg='%s' % e.message)
if record:
try:
record.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, record=rax_to_dict(record))

View file

@ -79,7 +79,7 @@ def rax_facts(module, address, name, server_id):
search_opts = dict(name='^%s$' % name)
try:
servers = cs.servers.list(search_opts=search_opts)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif address:
servers = []
@ -89,13 +89,13 @@ def rax_facts(module, address, name, server_id):
if address in addresses:
servers.append(server)
break
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif server_id:
servers = []
try:
servers.append(cs.servers.get(server_id))
except Exception, e:
except Exception as e:
pass
servers[:] = [server for server in servers if server.status != "DELETED"]

View file

@ -142,7 +142,7 @@ EXAMPLES = '''
try:
import pyrax
HAS_PYRAX = True
except ImportError, e:
except ImportError as e:
HAS_PYRAX = False
EXIT_DICT = dict(success=True)
@ -152,7 +152,7 @@ META_PREFIX = 'x-container-meta-'
def _get_container(module, cf, container):
try:
return cf.get_container(container)
except pyrax.exc.NoSuchContainer, e:
except pyrax.exc.NoSuchContainer as e:
module.fail_json(msg=e.message)
@ -162,7 +162,7 @@ def _fetch_meta(module, container):
for k, v in container.get_metadata().items():
split_key = k.split(META_PREFIX)[-1]
EXIT_DICT['meta'][split_key] = v
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
@ -172,7 +172,7 @@ def meta(cf, module, container_, state, meta_, clear_meta):
if meta_ and state == 'present':
try:
meta_set = c.set_metadata(meta_, clear=clear_meta)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
elif meta_ and state == 'absent':
remove_results = []
@ -214,12 +214,12 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
try:
c = cf.get_container(container_)
except pyrax.exc.NoSuchContainer, e:
except pyrax.exc.NoSuchContainer as e:
# Make the container if state=present, otherwise bomb out
if state == 'present':
try:
c = cf.create_container(container_)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['changed'] = True
@ -232,7 +232,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if state == 'absent':
try:
cont_deleted = c.delete()
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['deleted'] = True
@ -240,7 +240,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if meta_:
try:
meta_set = c.set_metadata(meta_, clear=clear_meta)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
finally:
_fetch_meta(module, c)
@ -248,7 +248,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if ttl:
try:
c.cdn_ttl = ttl
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['ttl'] = c.cdn_ttl
@ -256,7 +256,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if public:
try:
cont_public = c.make_public()
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['container_urls'] = dict(url=c.cdn_uri,
@ -267,7 +267,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if private:
try:
cont_private = c.make_private()
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['set_private'] = True
@ -275,7 +275,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if web_index:
try:
cont_web_index = c.set_web_index_page(web_index)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['set_index'] = True
@ -285,7 +285,7 @@ def container(cf, module, container_, state, meta_, clear_meta, ttl, public,
if web_error:
try:
cont_err_index = c.set_web_error_page(web_error)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
EXIT_DICT['set_error'] = True

View file

@ -196,7 +196,7 @@ META_PREFIX = 'x-object-meta-'
def _get_container(module, cf, container):
try:
return cf.get_container(container)
except pyrax.exc.NoSuchContainer, e:
except pyrax.exc.NoSuchContainer as e:
module.fail_json(msg=e.message)
@ -238,17 +238,17 @@ def upload(module, cf, container, src, dest, meta, expires):
if dest and not is_dir:
try:
cont_obj = c.upload_file(src, obj_name=dest, ttl=expires, headers=meta)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
elif is_dir:
try:
total_bytes = _upload_folder(cf, src, c, ttl=expires, headers=meta)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
try:
cont_obj = c.upload_file(src, ttl=expires, headers=meta)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
EXIT_DICT['success'] = True
@ -299,7 +299,7 @@ def download(module, cf, container, src, dest, structure):
for obj in objs:
try:
c.download_object(obj, dest, structure=structure)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
results.append(obj)
@ -348,7 +348,7 @@ def delete(module, cf, container, src, dest):
for obj in objs:
try:
result = c.delete_object(obj)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
results.append(result)
@ -396,7 +396,7 @@ def get_meta(module, cf, container, src, dest):
for obj in objs:
try:
meta = c.get_object(obj).get_metadata()
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
results[obj] = dict()
@ -434,7 +434,7 @@ def put_meta(module, cf, container, src, dest, meta, clear_meta):
for obj in objs:
try:
result = c.get_object(obj).set_metadata(meta, clear=clear_meta)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
results.append(result)
@ -473,20 +473,20 @@ def delete_meta(module, cf, container, src, dest, meta):
for k, v in meta.items():
try:
result = c.get_object(obj).remove_metadata_key(k)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
else:
results.append(result)
else:
try:
o = c.get_object(obj)
except pyrax.exc.NoSuchObject, e:
except pyrax.exc.NoSuchObject as e:
module.fail_json(msg=e.message)
for k, v in o.get_metadata().items():
try:
result = o.remove_metadata_key(k)
except Exception, e:
except Exception as e:
module.fail_json(msg=e.message)
results.append(result)

View file

@ -109,7 +109,7 @@ def rax_keypair(module, name, public_key, state):
f = open(public_key)
public_key = f.read()
f.close()
except Exception, e:
except Exception as e:
module.fail_json(msg='Failed to load %s' % public_key)
try:
@ -118,9 +118,9 @@ def rax_keypair(module, name, public_key, state):
try:
keypair = cs.keypairs.create(name, public_key)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif state == 'absent':
@ -133,7 +133,7 @@ def rax_keypair(module, name, public_key, state):
try:
keypair.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, keypair=rax_to_dict(keypair))

View file

@ -91,7 +91,7 @@ def rax_meta(module, address, name, server_id, meta):
search_opts = dict(name='^%s$' % name)
try:
servers = cs.servers.list(search_opts=search_opts)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif address:
servers = []
@ -101,13 +101,13 @@ def rax_meta(module, address, name, server_id, meta):
if address in addresses:
servers.append(server)
break
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif server_id:
servers = []
try:
servers.append(cs.servers.get(server_id))
except Exception, e:
except Exception as e:
pass
if len(servers) > 1:

View file

@ -86,9 +86,9 @@ def cloud_network(module, state, label, cidr):
try:
network = pyrax.cloud_networks.create(label, cidr=cidr)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
elif state == 'absent':
@ -98,7 +98,7 @@ def cloud_network(module, state, label, cidr):
changed = True
except pyrax.exceptions.NetworkNotFound:
pass
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
if network:

View file

@ -93,7 +93,7 @@ def cloud_queue(module, state, name):
try:
queue = cq.create(name)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
queue = queues[0]
@ -108,7 +108,7 @@ def cloud_queue(module, state, name):
try:
queue.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, queue=instance)

View file

@ -173,7 +173,7 @@ def rax_asg(module, cooldown=300, disk_config=None, files={}, flavor=None,
f = open(user_data)
user_data = f.read()
f.close()
except Exception, e:
except Exception as e:
module.fail_json(msg='Failed to load %s' % user_data)
if state == 'present':
@ -215,7 +215,7 @@ def rax_asg(module, cooldown=300, disk_config=None, files={}, flavor=None,
'contents': f.read()
})
f.close()
except Exception, e:
except Exception as e:
module.fail_json(msg='Failed to load %s' % lpath)
lbs = []
@ -237,7 +237,7 @@ def rax_asg(module, cooldown=300, disk_config=None, files={}, flavor=None,
try:
sg = au.find(name=name)
except pyrax.exceptions.NoUniqueMatch, e:
except pyrax.exceptions.NoUniqueMatch as e:
module.fail_json(msg='%s' % e.message)
except pyrax.exceptions.NotFound:
try:
@ -252,7 +252,7 @@ def rax_asg(module, cooldown=300, disk_config=None, files={}, flavor=None,
key_name=key_name, config_drive=config_drive,
user_data=user_data)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
if not changed:
@ -344,9 +344,9 @@ def rax_asg(module, cooldown=300, disk_config=None, files={}, flavor=None,
sg = au.find(name=name)
sg.delete()
changed = True
except pyrax.exceptions.NotFound, e:
except pyrax.exceptions.NotFound as e:
sg = {}
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, autoscale_group=rax_to_dict(sg))

View file

@ -141,12 +141,12 @@ def rax_asp(module, at=None, change=0, cron=None, cooldown=300,
except ValueError:
try:
sg = au.find(name=scaling_group)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
try:
sg = au.get(scaling_group)
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
if state == 'present':
@ -168,7 +168,7 @@ def rax_asp(module, at=None, change=0, cron=None, cooldown=300,
desired_capacity=desired_capacity,
args=args)
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
else:
@ -217,7 +217,7 @@ def rax_asp(module, at=None, change=0, cron=None, cooldown=300,
else:
policy.delete()
changed = True
except Exception, e:
except Exception as e:
module.fail_json(msg='%s' % e.message)
module.exit_json(changed=changed, autoscale_policy=rax_to_dict(policy))