Log errors better in case of unknown REST errors

If we get an unexpected exception, it's helpful to know the
cloud and region involved. It's also nice to return that information
for maybe better readability.

Change-Id: I1c589744103512d981e64e1a3f9506d40e1bf4cf
This commit is contained in:
Monty Taylor 2019-08-14 12:41:07 -04:00
parent 6e865fa04a
commit a019e7d89d
1 changed files with 29 additions and 11 deletions

View File

@ -133,6 +133,14 @@ def get_mime_icon(mime, filename=''):
return "data:image/png;base64,%s" % ICON_IMAGES[icon]
def get_cloud(cloud):
if isinstance(cloud, dict):
config = openstack.config.loader.OpenStackConfig().get_one(**cloud)
return openstack.connection.Connection(config=config)
else:
return openstack.connect(cloud=cloud)
def sizeof_fmt(num, suffix='B'):
# From http://stackoverflow.com/questions/1094841/
# reusable-library-to-get-human-readable-version-of-file-size
@ -668,17 +676,27 @@ def ansible_main():
)
)
cloud = get_cloud(p.get('cloud'))
p = module.params
url = run(p.get('cloud'), p.get('container'), p.get('files'),
indexes=p.get('indexes'),
parent_links=p.get('parent_links'),
topdir_parent_link=p.get('topdir_parent_link'),
partition=p.get('partition'),
footer=p.get('footer'),
delete_after=p.get('delete_after', 15552000),
prefix=p.get('prefix'),
public=p.get('public'))
try:
url = run(cloud, p.get('container'), p.get('files'),
indexes=p.get('indexes'),
parent_links=p.get('parent_links'),
topdir_parent_link=p.get('topdir_parent_link'),
partition=p.get('partition'),
footer=p.get('footer'),
delete_after=p.get('delete_after', 15552000),
prefix=p.get('prefix'),
public=p.get('public'))
except (keystoneauth1.exceptions.HTTPError,
requests.exceptions.RequestException) as e:
module.fail_json(
changed=False,
msg=str(e),
cloud=cloud.name,
region_name=cloud.config.region_name)
logging.exception("Error uploading to %s.%s",
cloud.name, cloud.config.region_name)
module.exit_json(changed=True,
url=url)
@ -738,7 +756,7 @@ def cli_main():
if append_footer.lower() == 'none':
append_footer = None
url = run(args.cloud, args.container, args.files,
url = run(get_cloud(args.cloud), args.container, args.files,
indexes=not args.no_indexes,
parent_links=not args.no_parent_links,
topdir_parent_link=args.create_topdir_parent_link,