From c2d34efd212fd63af1358b8697aec594fbef05c0 Mon Sep 17 00:00:00 2001 From: Troy C Date: Fri, 13 Dec 2013 16:27:21 -0600 Subject: [PATCH 1/2] import novaclient.exceptions for cs.images.find cs.images.find(human_id= throws novaclient.exceptions.NotFound, resulting in the try/except block with image = cs.images.find(name=image) being skipped. catching novaclient.exception.NotFound allows images to be specified with the human readable name. Example: tasks: - name: Server build request local_action: module: rax region: DFW image: Ubuntu 12.04 LTS (Precise Pangolin) Also, the import is placed after try: import pyrax, because pyrax imports novaclient and should fail if novaclient is missing. --- cloud/rax | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cloud/rax b/cloud/rax index 4ec1391c821..2b379081789 100644 --- a/cloud/rax +++ b/cloud/rax @@ -162,6 +162,8 @@ except ImportError: print("failed=True msg='pyrax is required for this module'") sys.exit(1) +import novaclient.exceptions + ACTIVE_STATUSES = ('ACTIVE', 'BUILD', 'HARD_REBOOT', 'MIGRATING', 'PASSWORD', 'REBOOT', 'REBUILD', 'RESCUE', 'RESIZE', 'REVERT_RESIZE') FINAL_STATUSES = ('ACTIVE', 'ERROR') @@ -366,11 +368,13 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files, except ValueError: try: image = cs.images.find(human_id=image) - except (pyrax.exceptions.NotFound, + except (novaclient.exceptions.NotFound, + pyrax.exceptions.NotFound, pyrax.exceptions.NoUniqueMatch): try: image = cs.images.find(name=image) - except (pyrax.exceptions.NotFound, + except (novaclient.exceptions.NotFound, + pyrax.exceptions.NotFound, pyrax.exceptions.NoUniqueMatch): module.fail_json(msg='No matching image found (%s)' % image) From ff1570ea31b9cf09567b5c8a7a4944f0e75c435a Mon Sep 17 00:00:00 2001 From: Troy C Date: Mon, 16 Dec 2013 09:33:29 -0600 Subject: [PATCH 2/2] catch exposed cs.exceptions instead of novaclient --- cloud/rax | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cloud/rax b/cloud/rax index 2b379081789..d67802ce1e0 100644 --- a/cloud/rax +++ b/cloud/rax @@ -162,8 +162,6 @@ except ImportError: print("failed=True msg='pyrax is required for this module'") sys.exit(1) -import novaclient.exceptions - ACTIVE_STATUSES = ('ACTIVE', 'BUILD', 'HARD_REBOOT', 'MIGRATING', 'PASSWORD', 'REBOOT', 'REBUILD', 'RESCUE', 'RESIZE', 'REVERT_RESIZE') FINAL_STATUSES = ('ACTIVE', 'ERROR') @@ -368,14 +366,12 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files, except ValueError: try: image = cs.images.find(human_id=image) - except (novaclient.exceptions.NotFound, - pyrax.exceptions.NotFound, - pyrax.exceptions.NoUniqueMatch): + except(cs.exceptions.NotFound, + cs.exceptions.NoUniqueMatch): try: image = cs.images.find(name=image) - except (novaclient.exceptions.NotFound, - pyrax.exceptions.NotFound, - pyrax.exceptions.NoUniqueMatch): + except (cs.exceptions.NotFound, + cs.exceptions.NoUniqueMatch): module.fail_json(msg='No matching image found (%s)' % image)