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
1515
1616import 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
1821from openstackclient .common import utils
1922
2023
2124LOG = logging .getLogger (__name__ )
2225
2326API_NAME = "image"
2427API_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