summaryrefslogtreecommitdiff
path: root/novaclient/v2
diff options
context:
space:
mode:
authorDan Smith <dansmith@redhat.com>2019-10-18 07:37:19 -0700
committerDan Smith <dansmith@redhat.com>2019-10-18 10:49:53 -0700
commite1bb4378db1e5be85a59861c2d473388fa336da8 (patch)
treef7f83828a170ebda30bfc2af3b774b26318c4e4d /novaclient/v2
parent8744bea0e3ebe5bc4d0d899189bfa0bcdcb0a08f (diff)
downloadpython-novaclient-e1bb4378db1e5be85a59861c2d473388fa336da8.tar.gz
Add images.GlanceManager.find_images() bulk query
This adds a bulk query method to GlanceManager that takes multiple image names or ids and attempts to query glance for all of them in fewer requests than one-per-image. Change-Id: I1c6ce27b65920356df6b7001c581a2922765ce0c
Diffstat (limited to 'novaclient/v2')
-rw-r--r--novaclient/v2/images.py37
-rw-r--r--novaclient/v2/shell.py8
2 files changed, 45 insertions, 0 deletions
diff --git a/novaclient/v2/images.py b/novaclient/v2/images.py
index e83d9b2f..0aced5c7 100644
--- a/novaclient/v2/images.py
+++ b/novaclient/v2/images.py
@@ -66,6 +66,43 @@ class GlanceManager(base.Manager):
matches[0].append_request_ids(matches.request_ids)
return matches[0]
+ def find_images(self, names_or_ids):
+ """Find multiple images by name or id (user provided input).
+
+ :param names_or_ids: A list of strings to use to find images.
+ :returns: novaclient.v2.images.Image objects for each images found
+ :raises exceptions.NotFound: If one or more images is not found
+ :raises exceptions.ClientException: If the image service returns any
+ unexpected images.
+
+ NOTE: This method always makes two calls to the image service, even if
+ only one image is provided by ID and is returned in the first query.
+ """
+ with self.alternate_service_type(
+ 'image', allowed_types=('image',)):
+ matches = self._list('/v2/images?id=in:%s' % ','.join(
+ names_or_ids), 'images')
+ matches.extend(self._list('/v2/images?names=in:%s' % ','.join(
+ names_or_ids), 'images'))
+ missed = (set(names_or_ids) -
+ set(m.name for m in matches) -
+ set(m.id for m in matches))
+ if missed:
+ msg = _("Unable to find image(s): %(images)s") % {
+ "images": ",".join(missed)}
+ raise exceptions.NotFound(404, msg)
+ for match in matches:
+ match.append_request_ids(matches.request_ids)
+
+ additional = []
+ for i in matches:
+ if i.name not in names_or_ids and i.id not in names_or_ids:
+ additional.append(i)
+ if additional:
+ msg = _('Additional images found in response')
+ raise exceptions.ClientException(500, msg)
+ return matches
+
def list(self):
"""
Get a detailed list of all images.
diff --git a/novaclient/v2/shell.py b/novaclient/v2/shell.py
index 2125ea99..818006b9 100644
--- a/novaclient/v2/shell.py
+++ b/novaclient/v2/shell.py
@@ -2570,6 +2570,14 @@ def _find_image(cs, image):
raise exceptions.CommandError(six.text_type(e))
+def _find_images(cs, images):
+ """Get images by name or ID."""
+ try:
+ return cs.glance.find_images(images)
+ except (exceptions.NotFound, exceptions.NoUniqueMatch) as e:
+ raise exceptions.CommandError(six.text_type(e))
+
+
def _find_flavor(cs, flavor):
"""Get a flavor by name, ID, or RAM size."""
try: