Skip to content

Commit 7b47579

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Complete Image v1"
2 parents 5d1d738 + cdaee1b commit 7b47579

6 files changed

Lines changed: 383 additions & 60 deletions

File tree

‎openstackclient/image/client.py‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2012-2013 OpenStack, LLC.
1+
# Copyright 2012-2013 OpenStack Foundation
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License"); you may
44
# not use this file except in compliance with the License. You may obtain
@@ -15,15 +15,18 @@
1515

1616
import logging
1717

18+
from glanceclient import exc as gc_exceptions
19+
from glanceclient.v1 import client as gc_v1_client
20+
from glanceclient.v1 import images as gc_v1_images
1821
from openstackclient.common import utils
1922

2023

2124
LOG = logging.getLogger(__name__)
2225

2326
API_NAME = "image"
2427
API_VERSIONS = {
25-
"1": "glanceclient.v1.client.Client",
26-
"2": "glanceclient.v2.client.Client"
28+
"1": "openstackclient.image.client.Client_v1",
29+
"2": "glanceclient.v2.client.Client",
2730
}
2831

2932

@@ -38,3 +41,54 @@ def make_client(instance):
3841
instance._url = instance.get_endpoint_for_service_type(API_NAME)
3942

4043
return image_client(instance._url, token=instance._token)
44+
45+
46+
# NOTE(dtroyer): glanceclient.v1.image.ImageManager() doesn't have a find()
47+
# method so add one here until the common client libs arrive
48+
# A similar subclass will be required for v2
49+
50+
class Client_v1(gc_v1_client.Client):
51+
"""An image v1 client that uses ImageManager_v1"""
52+
53+
def __init__(self, *args, **kwargs):
54+
super(Client_v1, self).__init__(*args, **kwargs)
55+
self.images = ImageManager_v1(self)
56+
57+
58+
class ImageManager_v1(gc_v1_images.ImageManager):
59+
"""Add find() and findall() to the ImageManager class"""
60+
61+
def find(self, **kwargs):
62+
"""Find a single item with attributes matching ``**kwargs``.
63+
64+
This isn't very efficient: it loads the entire list then filters on
65+
the Python side.
66+
"""
67+
rl = self.findall(**kwargs)
68+
num = len(rl)
69+
70+
if num == 0:
71+
raise gc_exceptions.NotFound
72+
elif num > 1:
73+
raise gc_exceptions.NoUniqueMatch
74+
else:
75+
return rl[0]
76+
77+
def findall(self, **kwargs):
78+
"""Find all items with attributes matching ``**kwargs``.
79+
80+
This isn't very efficient: it loads the entire list then filters on
81+
the Python side.
82+
"""
83+
found = []
84+
searches = kwargs.items()
85+
86+
for obj in self.list():
87+
try:
88+
if all(getattr(obj, attr) == value
89+
for (attr, value) in searches):
90+
found.append(obj)
91+
except AttributeError:
92+
continue
93+
94+
return found

0 commit comments

Comments
 (0)