Merge lp:~rackspace-titan/glance/api-results-ordering into lp:~hudson-openstack/glance/trunk
- api-results-ordering
- Merge into trunk
Status: | Merged |
---|---|
Approved by: | Jay Pipes |
Approved revision: | 155 |
Merged at revision: | 146 |
Proposed branch: | lp:~rackspace-titan/glance/api-results-ordering |
Merge into: | lp:~hudson-openstack/glance/trunk |
Diff against target: |
2032 lines (+1333/-256) 15 files modified
bin/glance (+1/-1) doc/source/client.rst (+30/-1) doc/source/glanceapi.rst (+14/-0) doc/source/registries.rst (+14/-0) glance/api/v1/images.py (+17/-16) glance/client.py (+13/-173) glance/common/client.py (+169/-0) glance/common/exception.py (+5/-0) glance/registry/client.py (+10/-19) glance/registry/db/api.py (+16/-6) glance/registry/server.py (+54/-17) tests/functional/test_curl_api.py (+115/-0) tests/stubs.py (+19/-10) tests/unit/test_api.py (+470/-0) tests/unit/test_clients.py (+386/-13) |
To merge this branch: | bzr merge lp:~rackspace-titan/glance/api-results-ordering |
Related bugs: | |
Related blueprints: |
API Results Ordering
(Low)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jay Pipes (community) | Approve | ||
Review via email:
|
Commit message
Description of the change
Added sort_key and sort_dir query params to apis and clients.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Waldon (bcwaldon) wrote : | # |
I'll take care of your comments later today, Jay. I also just pushed some updates to the glance client docs. Missed that one spot last night.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Waldon (bcwaldon) wrote : | # |
> For the SUPPORTED_PARAMS, and SUPPORTED_
> you make a glance.
> supported parameters? Either that, or make the constant at that module-level
> and import them into glance.client. That will ensure that future modifications
> to that constant do not have to make the modifications in two places. You did
> this for the SUPPORTED_SORT_KEYS constant in the registry, so might as well do
> it for SUPPORTED_PARAMS, too. :)
I think I took care of this now. Can you double-check?
> 278 + _sort_dir = sort_dir or 'desc'
> 279 + sort_dir_func = {
> 280 + 'asc': asc,
> 281 + 'desc': desc,
> 282 + }[_sort_dir]
>
> Instead of having the sort_dir param default to None, how about having it
> default to 'desc', in which case, the above can be simplified:
>
>
> sort_dir_func = {
> 'asc': asc,
> 'desc': desc
> }[sort_dir]
>
> Same can be said for this:
>
> 284 + _sort_key = sort_key or 'created_at'
> 285 + sort_key_attr = getattr(
>
> Just make the sort_key parameter default to 'created_at', and shorten to:
>
> sort_key_attr = getattr(
There are cases where None is being passed in explicitly. Is it okay if I leave it?
> For this repeated block of code:
>
> 174 + params = kwargs.
> 175 + params.
>
> Go ahead and put the first line in the _extract_
> _extract_
Got it.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Jay Pipes (jaypipes) wrote : | # |
> I think I took care of this now. Can you double-check?
For the /glance/
For the client, though:
161 +# parameters accepted in get_images* methods
162 +SUPPORTED_
I'd prefer you re-use the above constant in V1Client like so:
from glance.api.v1 import images as v1_images
and then move _extract_
> > 278 + _sort_dir = sort_dir or 'desc'
> > 279 + sort_dir_func = {
> > 280 + 'asc': asc,
> > 281 + 'desc': desc,
> > 282 + }[_sort_dir]
> >
> > Instead of having the sort_dir param default to None, how about having it
> > default to 'desc', in which case, the above can be simplified:
> >
> >
> > sort_dir_func = {
> > 'asc': asc,
> > 'desc': desc
> > }[sort_dir]
> >
> > Same can be said for this:
> >
> > 284 + _sort_key = sort_key or 'created_at'
> > 285 + sort_key_attr = getattr(
> >
> > Just make the sort_key parameter default to 'created_at', and shorten to:
> >
> > sort_key_attr = getattr(
>
> There are cases where None is being passed in explicitly. Is it okay if I
> leave it?
In what case is None being passed explicitly? Can we remove those cases and rely on default parameter values?
-jay
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Waldon (bcwaldon) wrote : | # |
> > I think I took care of this now. Can you double-check?
>
> For the /glance/
>
> For the client, though:
>
> 161 +# parameters accepted in get_images* methods
> 162 +SUPPORTED_
>
> I'd prefer you re-use the above constant in V1Client like so:
>
> from glance.api.v1 import images as v1_images
>
> and then move _extract_
> V1Client class, and reference v1_images.
> copied SUPPORTED_
> sense?
I had to move BaseClient out of glance/client.py and into glance/
I got all of the SUPPORTED_PARAMS stuff organized like you wanted. I ended up leaving the _extract_params function in BaseClient, as it is used by the registry client and the api client. I just have it passing in the allowed params.
> > > 278 + _sort_dir = sort_dir or 'desc'
> > > 279 + sort_dir_func = {
> > > 280 + 'asc': asc,
> > > 281 + 'desc': desc,
> > > 282 + }[_sort_dir]
> > >
> > > Instead of having the sort_dir param default to None, how about having it
> > > default to 'desc', in which case, the above can be simplified:
> > >
> > >
> > > sort_dir_func = {
> > > 'asc': asc,
> > > 'desc': desc
> > > }[sort_dir]
> > >
> > > Same can be said for this:
> > >
> > > 284 + _sort_key = sort_key or 'created_at'
> > > 285 + sort_key_attr = getattr(
> > >
> > > Just make the sort_key parameter default to 'created_at', and shorten to:
> > >
> > > sort_key_attr = getattr(
> >
> > There are cases where None is being passed in explicitly. Is it okay if I
> > leave it?
>
> In what case is None being passed explicitly? Can we remove those cases and
> rely on default parameter values?
The function in registry/server.py that extracts the query params was returning None for keys that weren't provided. Now it filters out None. It's definitely cleaner to define the defaults in kwargs.
- 148. By Brian Waldon
-
merging trunk
- 149. By Brian Waldon
-
restructuring client code
- 150. By Brian Waldon
-
adding base client module
- 151. By Brian Waldon
-
cleaning up None values being passed into images_
get_all_ public db call
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Jay Pipes (jaypipes) wrote : | # |
Wicked. Great work, Brian!
Teeny weeny style thing:
789 + """Extract necessary query parameters from http request.
790 +
791 + :param req: the Request object coming from the wsgi layer
792 + :retval dictionary of filters to apply to list of images
793 +
794 + """
Should be:
"""
Extract necessary query parameters from http request.
:param req: the Request object coming from the wsgi layer
:retval dictionary of filters to apply to list of images
"""
There are a number of docstrings in the test cases that also need fixing for that. Sorry for being picky :(
-jay
- 152. By Brian Waldon
-
docstring
- 153. By Brian Waldon
-
reverting one import change; another docstring fix
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Waldon (bcwaldon) wrote : | # |
I'm a little confused on what you want in the tests. Do you want me to change all of the comments like this:
"""Some really long dosctring that
wraps at 80 chars.
"""
to this:
"""
Some really long docstring that
wraps at 80 chars.
"""
I ask because the precedent is firmly set as the first style. I'm fine making changes, I just want to make sure I do it correctly.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Lamar (blamar) wrote : | # |
I think Nova and Glance have slightly differences in this aspect. Nova 'requires' a single line summary followed by a multi-line description:
"""This is the first line summary.
This is the multiple line description which in itself
can have multiple line breaks and stuff. Plus it has a newline
at the end of it all.
"""
If it's just one line then it has to be all on one line:
"""This is a one line summary that needs to be 80 char or less."""
But meh, I've had nightmares about this ;)
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Waldon (bcwaldon) wrote : | # |
Brian: So how do we do it in glance?
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Jay Pipes (jaypipes) wrote : | # |
On Mon, Jun 27, 2011 at 1:19 PM, Brian Waldon
<email address hidden> wrote:
> Brian: So how do we do it in glance?
The way I wrote above...
"""
This is a description. It can go more than one
line long. It's silly to force a one-line description
and then a newline and a multi-line description. Sometimes that
just doesn't make much sense.
And having an extra newline at the end of the docstring doesn't make
much sense either. Oh, and I don't care if you end your sentences with
periods
"""
Cheers,
jay
- 154. By Brian Waldon
-
docstrings\!
- 155. By Brian Waldon
-
fixing one last docstring
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Brian Waldon (bcwaldon) wrote : | # |
I updated all the doctrings I touched. Would you be cool with a follow-up branch to clean up docstrings across the project?
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Jay Pipes (jaypipes) wrote : | # |
But of course monsieur.
On Jun 28, 2011 8:52 AM, "Brian Waldon" <email address hidden> wrote:
> I updated all the doctrings I touched. Would you be cool with a follow-up
branch to clean up docstrings across the project?
> --
>
https:/
> You are reviewing the proposed merge of
lp:~rackspace-titan/glance/api-results-ordering into lp:glance.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Jay Pipes (jaypipes) : | # |
Preview Diff
1 | === modified file 'bin/glance' | |||
2 | --- bin/glance 2011-04-27 18:00:49 +0000 | |||
3 | +++ bin/glance 2011-06-28 13:52:02 +0000 | |||
4 | @@ -194,7 +194,7 @@ | |||
5 | 194 | print "Returned the following metadata for the new image:" | 194 | print "Returned the following metadata for the new image:" |
6 | 195 | for k, v in sorted(image_meta.items()): | 195 | for k, v in sorted(image_meta.items()): |
7 | 196 | print " %(k)30s => %(v)s" % locals() | 196 | print " %(k)30s => %(v)s" % locals() |
9 | 197 | except client.ClientConnectionError, e: | 197 | except exception.ClientConnectionError, e: |
10 | 198 | host = options.host | 198 | host = options.host |
11 | 199 | port = options.port | 199 | port = options.port |
12 | 200 | print ("Failed to connect to the Glance API server " | 200 | print ("Failed to connect to the Glance API server " |
13 | 201 | 201 | ||
14 | === modified file 'doc/source/client.rst' | |||
15 | --- doc/source/client.rst 2011-05-25 20:03:00 +0000 | |||
16 | +++ doc/source/client.rst 2011-06-28 13:52:02 +0000 | |||
17 | @@ -113,7 +113,36 @@ | |||
18 | 113 | c = Client("glance.example.com", 9292) | 113 | c = Client("glance.example.com", 9292) |
19 | 114 | 114 | ||
20 | 115 | filters = {'status': 'saving', 'size_max': (5 * 1024 * 1024 * 1024)} | 115 | filters = {'status': 'saving', 'size_max': (5 * 1024 * 1024 * 1024)} |
22 | 116 | print c.get_images_detailed(filters) | 116 | print c.get_images_detailed(filters=filters) |
23 | 117 | |||
24 | 118 | Sorting Images Returned via ``get_images()`` and ``get_images_detailed()`` | ||
25 | 119 | -------------------------------------------------------------------------- | ||
26 | 120 | |||
27 | 121 | Two parameters are available to sort the list of images returned by | ||
28 | 122 | these methods. | ||
29 | 123 | |||
30 | 124 | * ``sort_key: KEY`` | ||
31 | 125 | |||
32 | 126 | Images can be ordered by the image attribute ``KEY``. Acceptable values: | ||
33 | 127 | ``id``, ``name``, ``status``, ``container_format``, ``disk_format``, | ||
34 | 128 | ``created_at`` (default) and ``updated_at``. | ||
35 | 129 | |||
36 | 130 | * ``sort_dir: DIR`` | ||
37 | 131 | |||
38 | 132 | The direction of the sort may be defined by ``DIR``. Accepted values: | ||
39 | 133 | ``asc`` for ascending or ``desc`` (default) for descending. | ||
40 | 134 | |||
41 | 135 | The following example will return a list of images sorted alphabetically | ||
42 | 136 | by name in ascending order. | ||
43 | 137 | |||
44 | 138 | .. code-block:: python | ||
45 | 139 | |||
46 | 140 | from glance.client import Client | ||
47 | 141 | |||
48 | 142 | c = Client("glance.example.com", 9292) | ||
49 | 143 | |||
50 | 144 | print c.get_images(sort_key='name', sort_dir='asc') | ||
51 | 145 | |||
52 | 117 | 146 | ||
53 | 118 | Requesting Detailed Metadata on a Specific Image | 147 | Requesting Detailed Metadata on a Specific Image |
54 | 119 | ------------------------------------------------ | 148 | ------------------------------------------------ |
55 | 120 | 149 | ||
56 | === modified file 'doc/source/glanceapi.rst' | |||
57 | --- doc/source/glanceapi.rst 2011-05-26 12:53:48 +0000 | |||
58 | +++ doc/source/glanceapi.rst 2011-06-28 13:52:02 +0000 | |||
59 | @@ -129,6 +129,20 @@ | |||
60 | 129 | 129 | ||
61 | 130 | Filters images having a ``size`` attribute less than or equal to ``BYTES`` | 130 | Filters images having a ``size`` attribute less than or equal to ``BYTES`` |
62 | 131 | 131 | ||
63 | 132 | These two resources also accept sort parameters: | ||
64 | 133 | |||
65 | 134 | * ``sort_key=KEY`` | ||
66 | 135 | |||
67 | 136 | Results will be ordered by the specified image attribute ``KEY``. Accepted | ||
68 | 137 | values include ``id``, ``name``, ``status``, ``disk_format``, | ||
69 | 138 | ``container_format``, ``size``, ``created_at`` (default) and ``updated_at``. | ||
70 | 139 | |||
71 | 140 | * ``sort_dir=DIR`` | ||
72 | 141 | |||
73 | 142 | Results will be sorted in the direction ``DIR``. Accepted values are ``asc`` | ||
74 | 143 | for ascending or ``desc`` (default) for descending. | ||
75 | 144 | |||
76 | 145 | |||
77 | 132 | Requesting Detailed Metadata on a Specific Image | 146 | Requesting Detailed Metadata on a Specific Image |
78 | 133 | ------------------------------------------------ | 147 | ------------------------------------------------ |
79 | 134 | 148 | ||
80 | 135 | 149 | ||
81 | === modified file 'doc/source/registries.rst' | |||
82 | --- doc/source/registries.rst 2011-05-26 12:53:48 +0000 | |||
83 | +++ doc/source/registries.rst 2011-06-28 13:52:02 +0000 | |||
84 | @@ -83,6 +83,20 @@ | |||
85 | 83 | 83 | ||
86 | 84 | Filters images having a ``size`` attribute less than or equal to ``BYTES`` | 84 | Filters images having a ``size`` attribute less than or equal to ``BYTES`` |
87 | 85 | 85 | ||
88 | 86 | These two resources also accept sort parameters: | ||
89 | 87 | |||
90 | 88 | * ``sort_key=KEY`` | ||
91 | 89 | |||
92 | 90 | Results will be ordered by the specified image attribute ``KEY``. Accepted | ||
93 | 91 | values include ``id``, ``name``, ``status``, ``disk_format``, | ||
94 | 92 | ``container_format``, ``size``, ``created_at`` (default) and ``updated_at``. | ||
95 | 93 | |||
96 | 94 | * ``sort_dir=DIR`` | ||
97 | 95 | |||
98 | 96 | Results will be sorted in the direction ``DIR``. Accepted values are ``asc`` | ||
99 | 97 | for ascending or ``desc`` (default) for descending. | ||
100 | 98 | |||
101 | 99 | |||
102 | 86 | ``POST /images`` | 100 | ``POST /images`` |
103 | 87 | ---------------- | 101 | ---------------- |
104 | 88 | 102 | ||
105 | 89 | 103 | ||
106 | === modified file 'glance/api/v1/images.py' | |||
107 | --- glance/api/v1/images.py 2011-06-27 14:37:40 +0000 | |||
108 | +++ glance/api/v1/images.py 2011-06-28 13:52:02 +0000 | |||
109 | @@ -45,6 +45,8 @@ | |||
110 | 45 | SUPPORTED_FILTERS = ['name', 'status', 'container_format', 'disk_format', | 45 | SUPPORTED_FILTERS = ['name', 'status', 'container_format', 'disk_format', |
111 | 46 | 'size_min', 'size_max'] | 46 | 'size_min', 'size_max'] |
112 | 47 | 47 | ||
113 | 48 | SUPPORTED_PARAMS = ('limit', 'marker', 'sort_key', 'sort_dir') | ||
114 | 49 | |||
115 | 48 | 50 | ||
116 | 49 | class Controller(object): | 51 | class Controller(object): |
117 | 50 | 52 | ||
118 | @@ -92,14 +94,7 @@ | |||
119 | 92 | 'size': <SIZE>}, ... | 94 | 'size': <SIZE>}, ... |
120 | 93 | ]} | 95 | ]} |
121 | 94 | """ | 96 | """ |
130 | 95 | params = {'filters': self._get_filters(req)} | 97 | params = self._get_query_params(req) |
123 | 96 | |||
124 | 97 | if 'limit' in req.str_params: | ||
125 | 98 | params['limit'] = req.str_params.get('limit') | ||
126 | 99 | |||
127 | 100 | if 'marker' in req.str_params: | ||
128 | 101 | params['marker'] = req.str_params.get('marker') | ||
129 | 102 | |||
131 | 103 | images = registry.get_images_list(self.options, **params) | 98 | images = registry.get_images_list(self.options, **params) |
132 | 104 | return dict(images=images) | 99 | return dict(images=images) |
133 | 105 | 100 | ||
134 | @@ -125,17 +120,23 @@ | |||
135 | 125 | 'properties': {'distro': 'Ubuntu 10.04 LTS', ...}}, ... | 120 | 'properties': {'distro': 'Ubuntu 10.04 LTS', ...}}, ... |
136 | 126 | ]} | 121 | ]} |
137 | 127 | """ | 122 | """ |
146 | 128 | params = {'filters': self._get_filters(req)} | 123 | params = self._get_query_params(req) |
139 | 129 | |||
140 | 130 | if 'limit' in req.str_params: | ||
141 | 131 | params['limit'] = req.str_params.get('limit') | ||
142 | 132 | |||
143 | 133 | if 'marker' in req.str_params: | ||
144 | 134 | params['marker'] = req.str_params.get('marker') | ||
145 | 135 | |||
147 | 136 | images = registry.get_images_detail(self.options, **params) | 124 | images = registry.get_images_detail(self.options, **params) |
148 | 137 | return dict(images=images) | 125 | return dict(images=images) |
149 | 138 | 126 | ||
150 | 127 | def _get_query_params(self, req): | ||
151 | 128 | """ | ||
152 | 129 | Extracts necessary query params from request. | ||
153 | 130 | |||
154 | 131 | :param req: the WSGI Request object | ||
155 | 132 | :retval dict of parameters that can be used by registry client | ||
156 | 133 | """ | ||
157 | 134 | params = {'filters': self._get_filters(req)} | ||
158 | 135 | for PARAM in SUPPORTED_PARAMS: | ||
159 | 136 | if PARAM in req.str_params: | ||
160 | 137 | params[PARAM] = req.str_params.get(PARAM) | ||
161 | 138 | return params | ||
162 | 139 | |||
163 | 139 | def _get_filters(self, req): | 140 | def _get_filters(self, req): |
164 | 140 | """ | 141 | """ |
165 | 141 | Return a dictionary of query param filters from the request | 142 | Return a dictionary of query param filters from the request |
166 | 142 | 143 | ||
167 | === modified file 'glance/client.py' | |||
168 | --- glance/client.py 2011-05-31 13:21:16 +0000 | |||
169 | +++ glance/client.py 2011-06-28 13:52:02 +0000 | |||
170 | @@ -19,172 +19,17 @@ | |||
171 | 19 | Client classes for callers of a Glance system | 19 | Client classes for callers of a Glance system |
172 | 20 | """ | 20 | """ |
173 | 21 | 21 | ||
174 | 22 | import httplib | ||
175 | 23 | import json | 22 | import json |
176 | 24 | import logging | ||
177 | 25 | import urlparse | ||
178 | 26 | import socket | ||
179 | 27 | import sys | ||
180 | 28 | import urllib | ||
181 | 29 | 23 | ||
182 | 24 | from glance.api.v1 import images as v1_images | ||
183 | 25 | from glance.common import client as base_client | ||
184 | 26 | from glance.common import exception | ||
185 | 30 | from glance import utils | 27 | from glance import utils |
186 | 31 | from glance.common import exception | ||
187 | 32 | 28 | ||
188 | 33 | #TODO(jaypipes) Allow a logger param for client classes | 29 | #TODO(jaypipes) Allow a logger param for client classes |
189 | 34 | 30 | ||
190 | 35 | 31 | ||
343 | 36 | class ClientConnectionError(Exception): | 32 | class V1Client(base_client.BaseClient): |
192 | 37 | """Error resulting from a client connecting to a server""" | ||
193 | 38 | pass | ||
194 | 39 | |||
195 | 40 | |||
196 | 41 | class ImageBodyIterator(object): | ||
197 | 42 | |||
198 | 43 | """ | ||
199 | 44 | A class that acts as an iterator over an image file's | ||
200 | 45 | chunks of data. This is returned as part of the result | ||
201 | 46 | tuple from `glance.client.Client.get_image` | ||
202 | 47 | """ | ||
203 | 48 | |||
204 | 49 | CHUNKSIZE = 65536 | ||
205 | 50 | |||
206 | 51 | def __init__(self, response): | ||
207 | 52 | """ | ||
208 | 53 | Constructs the object from an HTTPResponse object | ||
209 | 54 | """ | ||
210 | 55 | self.response = response | ||
211 | 56 | |||
212 | 57 | def __iter__(self): | ||
213 | 58 | """ | ||
214 | 59 | Exposes an iterator over the chunks of data in the | ||
215 | 60 | image file. | ||
216 | 61 | """ | ||
217 | 62 | while True: | ||
218 | 63 | chunk = self.response.read(ImageBodyIterator.CHUNKSIZE) | ||
219 | 64 | if chunk: | ||
220 | 65 | yield chunk | ||
221 | 66 | else: | ||
222 | 67 | break | ||
223 | 68 | |||
224 | 69 | |||
225 | 70 | class BaseClient(object): | ||
226 | 71 | |||
227 | 72 | """A base client class""" | ||
228 | 73 | |||
229 | 74 | CHUNKSIZE = 65536 | ||
230 | 75 | |||
231 | 76 | def __init__(self, host, port, use_ssl): | ||
232 | 77 | """ | ||
233 | 78 | Creates a new client to some service. | ||
234 | 79 | |||
235 | 80 | :param host: The host where service resides | ||
236 | 81 | :param port: The port where service resides | ||
237 | 82 | :param use_ssl: Should we use HTTPS? | ||
238 | 83 | """ | ||
239 | 84 | self.host = host | ||
240 | 85 | self.port = port | ||
241 | 86 | self.use_ssl = use_ssl | ||
242 | 87 | self.connection = None | ||
243 | 88 | |||
244 | 89 | def get_connection_type(self): | ||
245 | 90 | """ | ||
246 | 91 | Returns the proper connection type | ||
247 | 92 | """ | ||
248 | 93 | if self.use_ssl: | ||
249 | 94 | return httplib.HTTPSConnection | ||
250 | 95 | else: | ||
251 | 96 | return httplib.HTTPConnection | ||
252 | 97 | |||
253 | 98 | def do_request(self, method, action, body=None, headers=None, | ||
254 | 99 | params=None): | ||
255 | 100 | """ | ||
256 | 101 | Connects to the server and issues a request. Handles converting | ||
257 | 102 | any returned HTTP error status codes to OpenStack/Glance exceptions | ||
258 | 103 | and closing the server connection. Returns the result data, or | ||
259 | 104 | raises an appropriate exception. | ||
260 | 105 | |||
261 | 106 | :param method: HTTP method ("GET", "POST", "PUT", etc...) | ||
262 | 107 | :param action: part of URL after root netloc | ||
263 | 108 | :param body: string of data to send, or None (default) | ||
264 | 109 | :param headers: mapping of key/value pairs to add as headers | ||
265 | 110 | :param params: dictionary of key/value pairs to add to append | ||
266 | 111 | to action | ||
267 | 112 | |||
268 | 113 | :note | ||
269 | 114 | |||
270 | 115 | If the body param has a read attribute, and method is either | ||
271 | 116 | POST or PUT, this method will automatically conduct a chunked-transfer | ||
272 | 117 | encoding and use the body as a file object, transferring chunks | ||
273 | 118 | of data using the connection's send() method. This allows large | ||
274 | 119 | objects to be transferred efficiently without buffering the entire | ||
275 | 120 | body in memory. | ||
276 | 121 | """ | ||
277 | 122 | if type(params) is dict: | ||
278 | 123 | action += '?' + urllib.urlencode(params) | ||
279 | 124 | |||
280 | 125 | try: | ||
281 | 126 | connection_type = self.get_connection_type() | ||
282 | 127 | headers = headers or {} | ||
283 | 128 | c = connection_type(self.host, self.port) | ||
284 | 129 | |||
285 | 130 | # Do a simple request or a chunked request, depending | ||
286 | 131 | # on whether the body param is a file-like object and | ||
287 | 132 | # the method is PUT or POST | ||
288 | 133 | if hasattr(body, 'read') and method.lower() in ('post', 'put'): | ||
289 | 134 | # Chunk it, baby... | ||
290 | 135 | c.putrequest(method, action) | ||
291 | 136 | |||
292 | 137 | for header, value in headers.items(): | ||
293 | 138 | c.putheader(header, value) | ||
294 | 139 | c.putheader('Transfer-Encoding', 'chunked') | ||
295 | 140 | c.endheaders() | ||
296 | 141 | |||
297 | 142 | chunk = body.read(self.CHUNKSIZE) | ||
298 | 143 | while chunk: | ||
299 | 144 | c.send('%x\r\n%s\r\n' % (len(chunk), chunk)) | ||
300 | 145 | chunk = body.read(self.CHUNKSIZE) | ||
301 | 146 | c.send('0\r\n\r\n') | ||
302 | 147 | else: | ||
303 | 148 | # Simple request... | ||
304 | 149 | c.request(method, action, body, headers) | ||
305 | 150 | res = c.getresponse() | ||
306 | 151 | status_code = self.get_status_code(res) | ||
307 | 152 | if status_code in (httplib.OK, | ||
308 | 153 | httplib.CREATED, | ||
309 | 154 | httplib.ACCEPTED, | ||
310 | 155 | httplib.NO_CONTENT): | ||
311 | 156 | return res | ||
312 | 157 | elif status_code == httplib.UNAUTHORIZED: | ||
313 | 158 | raise exception.NotAuthorized | ||
314 | 159 | elif status_code == httplib.FORBIDDEN: | ||
315 | 160 | raise exception.NotAuthorized | ||
316 | 161 | elif status_code == httplib.NOT_FOUND: | ||
317 | 162 | raise exception.NotFound | ||
318 | 163 | elif status_code == httplib.CONFLICT: | ||
319 | 164 | raise exception.Duplicate(res.read()) | ||
320 | 165 | elif status_code == httplib.BAD_REQUEST: | ||
321 | 166 | raise exception.Invalid(res.read()) | ||
322 | 167 | elif status_code == httplib.INTERNAL_SERVER_ERROR: | ||
323 | 168 | raise Exception("Internal Server error: %s" % res.read()) | ||
324 | 169 | else: | ||
325 | 170 | raise Exception("Unknown error occurred! %s" % res.read()) | ||
326 | 171 | |||
327 | 172 | except (socket.error, IOError), e: | ||
328 | 173 | raise ClientConnectionError("Unable to connect to " | ||
329 | 174 | "server. Got error: %s" % e) | ||
330 | 175 | |||
331 | 176 | def get_status_code(self, response): | ||
332 | 177 | """ | ||
333 | 178 | Returns the integer status code from the response, which | ||
334 | 179 | can be either a Webob.Response (used in testing) or httplib.Response | ||
335 | 180 | """ | ||
336 | 181 | if hasattr(response, 'status_int'): | ||
337 | 182 | return response.status_int | ||
338 | 183 | else: | ||
339 | 184 | return response.status | ||
340 | 185 | |||
341 | 186 | |||
342 | 187 | class V1Client(BaseClient): | ||
344 | 188 | 33 | ||
345 | 189 | """Main client class for accessing Glance resources""" | 34 | """Main client class for accessing Glance resources""" |
346 | 190 | 35 | ||
347 | @@ -209,7 +54,7 @@ | |||
348 | 209 | return super(V1Client, self).do_request(method, action, body, | 54 | return super(V1Client, self).do_request(method, action, body, |
349 | 210 | headers, params) | 55 | headers, params) |
350 | 211 | 56 | ||
352 | 212 | def get_images(self, filters=None, marker=None, limit=None): | 57 | def get_images(self, **kwargs): |
353 | 213 | """ | 58 | """ |
354 | 214 | Returns a list of image id/name mappings from Registry | 59 | Returns a list of image id/name mappings from Registry |
355 | 215 | 60 | ||
356 | @@ -217,18 +62,15 @@ | |||
357 | 217 | collection of images should be filtered | 62 | collection of images should be filtered |
358 | 218 | :param marker: id after which to start the page of images | 63 | :param marker: id after which to start the page of images |
359 | 219 | :param limit: maximum number of items to return | 64 | :param limit: maximum number of items to return |
360 | 65 | :param sort_key: results will be ordered by this image attribute | ||
361 | 66 | :param sort_dir: direction in which to to order results (asc, desc) | ||
362 | 220 | """ | 67 | """ |
369 | 221 | 68 | params = self._extract_params(kwargs, v1_images.SUPPORTED_PARAMS) | |
364 | 222 | params = filters or {} | ||
365 | 223 | if marker: | ||
366 | 224 | params['marker'] = marker | ||
367 | 225 | if limit: | ||
368 | 226 | params['limit'] = limit | ||
370 | 227 | res = self.do_request("GET", "/images", params=params) | 69 | res = self.do_request("GET", "/images", params=params) |
371 | 228 | data = json.loads(res.read())['images'] | 70 | data = json.loads(res.read())['images'] |
372 | 229 | return data | 71 | return data |
373 | 230 | 72 | ||
375 | 231 | def get_images_detailed(self, filters=None, marker=None, limit=None): | 73 | def get_images_detailed(self, **kwargs): |
376 | 232 | """ | 74 | """ |
377 | 233 | Returns a list of detailed image data mappings from Registry | 75 | Returns a list of detailed image data mappings from Registry |
378 | 234 | 76 | ||
379 | @@ -236,13 +78,11 @@ | |||
380 | 236 | collection of images should be filtered | 78 | collection of images should be filtered |
381 | 237 | :param marker: id after which to start the page of images | 79 | :param marker: id after which to start the page of images |
382 | 238 | :param limit: maximum number of items to return | 80 | :param limit: maximum number of items to return |
383 | 81 | :param sort_key: results will be ordered by this image attribute | ||
384 | 82 | :param sort_dir: direction in which to to order results (asc, desc) | ||
385 | 239 | """ | 83 | """ |
386 | 240 | 84 | ||
392 | 241 | params = filters or {} | 85 | params = self._extract_params(kwargs, v1_images.SUPPORTED_PARAMS) |
388 | 242 | if marker: | ||
389 | 243 | params['marker'] = marker | ||
390 | 244 | if limit: | ||
391 | 245 | params['limit'] = limit | ||
393 | 246 | res = self.do_request("GET", "/images/detail", params=params) | 86 | res = self.do_request("GET", "/images/detail", params=params) |
394 | 247 | data = json.loads(res.read())['images'] | 87 | data = json.loads(res.read())['images'] |
395 | 248 | return data | 88 | return data |
396 | @@ -260,7 +100,7 @@ | |||
397 | 260 | res = self.do_request("GET", "/images/%s" % image_id) | 100 | res = self.do_request("GET", "/images/%s" % image_id) |
398 | 261 | 101 | ||
399 | 262 | image = utils.get_image_meta_from_headers(res) | 102 | image = utils.get_image_meta_from_headers(res) |
401 | 263 | return image, ImageBodyIterator(res) | 103 | return image, base_client.ImageBodyIterator(res) |
402 | 264 | 104 | ||
403 | 265 | def get_image_meta(self, image_id): | 105 | def get_image_meta(self, image_id): |
404 | 266 | """ | 106 | """ |
405 | 267 | 107 | ||
406 | === added file 'glance/common/client.py' | |||
407 | --- glance/common/client.py 1970-01-01 00:00:00 +0000 | |||
408 | +++ glance/common/client.py 2011-06-28 13:52:02 +0000 | |||
409 | @@ -0,0 +1,169 @@ | |||
410 | 1 | import httplib | ||
411 | 2 | import logging | ||
412 | 3 | import socket | ||
413 | 4 | import urllib | ||
414 | 5 | |||
415 | 6 | from glance.common import exception | ||
416 | 7 | |||
417 | 8 | |||
418 | 9 | class ImageBodyIterator(object): | ||
419 | 10 | |||
420 | 11 | """ | ||
421 | 12 | A class that acts as an iterator over an image file's | ||
422 | 13 | chunks of data. This is returned as part of the result | ||
423 | 14 | tuple from `glance.client.Client.get_image` | ||
424 | 15 | """ | ||
425 | 16 | |||
426 | 17 | CHUNKSIZE = 65536 | ||
427 | 18 | |||
428 | 19 | def __init__(self, response): | ||
429 | 20 | """ | ||
430 | 21 | Constructs the object from an HTTPResponse object | ||
431 | 22 | """ | ||
432 | 23 | self.response = response | ||
433 | 24 | |||
434 | 25 | def __iter__(self): | ||
435 | 26 | """ | ||
436 | 27 | Exposes an iterator over the chunks of data in the | ||
437 | 28 | image file. | ||
438 | 29 | """ | ||
439 | 30 | while True: | ||
440 | 31 | chunk = self.response.read(ImageBodyIterator.CHUNKSIZE) | ||
441 | 32 | if chunk: | ||
442 | 33 | yield chunk | ||
443 | 34 | else: | ||
444 | 35 | break | ||
445 | 36 | |||
446 | 37 | |||
447 | 38 | class BaseClient(object): | ||
448 | 39 | |||
449 | 40 | """A base client class""" | ||
450 | 41 | |||
451 | 42 | CHUNKSIZE = 65536 | ||
452 | 43 | |||
453 | 44 | def __init__(self, host, port, use_ssl): | ||
454 | 45 | """ | ||
455 | 46 | Creates a new client to some service. | ||
456 | 47 | |||
457 | 48 | :param host: The host where service resides | ||
458 | 49 | :param port: The port where service resides | ||
459 | 50 | :param use_ssl: Should we use HTTPS? | ||
460 | 51 | """ | ||
461 | 52 | self.host = host | ||
462 | 53 | self.port = port | ||
463 | 54 | self.use_ssl = use_ssl | ||
464 | 55 | self.connection = None | ||
465 | 56 | |||
466 | 57 | def get_connection_type(self): | ||
467 | 58 | """ | ||
468 | 59 | Returns the proper connection type | ||
469 | 60 | """ | ||
470 | 61 | if self.use_ssl: | ||
471 | 62 | return httplib.HTTPSConnection | ||
472 | 63 | else: | ||
473 | 64 | return httplib.HTTPConnection | ||
474 | 65 | |||
475 | 66 | def do_request(self, method, action, body=None, headers=None, | ||
476 | 67 | params=None): | ||
477 | 68 | """ | ||
478 | 69 | Connects to the server and issues a request. Handles converting | ||
479 | 70 | any returned HTTP error status codes to OpenStack/Glance exceptions | ||
480 | 71 | and closing the server connection. Returns the result data, or | ||
481 | 72 | raises an appropriate exception. | ||
482 | 73 | |||
483 | 74 | :param method: HTTP method ("GET", "POST", "PUT", etc...) | ||
484 | 75 | :param action: part of URL after root netloc | ||
485 | 76 | :param body: string of data to send, or None (default) | ||
486 | 77 | :param headers: mapping of key/value pairs to add as headers | ||
487 | 78 | :param params: dictionary of key/value pairs to add to append | ||
488 | 79 | to action | ||
489 | 80 | |||
490 | 81 | :note | ||
491 | 82 | |||
492 | 83 | If the body param has a read attribute, and method is either | ||
493 | 84 | POST or PUT, this method will automatically conduct a chunked-transfer | ||
494 | 85 | encoding and use the body as a file object, transferring chunks | ||
495 | 86 | of data using the connection's send() method. This allows large | ||
496 | 87 | objects to be transferred efficiently without buffering the entire | ||
497 | 88 | body in memory. | ||
498 | 89 | """ | ||
499 | 90 | if type(params) is dict: | ||
500 | 91 | action += '?' + urllib.urlencode(params) | ||
501 | 92 | |||
502 | 93 | try: | ||
503 | 94 | connection_type = self.get_connection_type() | ||
504 | 95 | headers = headers or {} | ||
505 | 96 | c = connection_type(self.host, self.port) | ||
506 | 97 | |||
507 | 98 | # Do a simple request or a chunked request, depending | ||
508 | 99 | # on whether the body param is a file-like object and | ||
509 | 100 | # the method is PUT or POST | ||
510 | 101 | if hasattr(body, 'read') and method.lower() in ('post', 'put'): | ||
511 | 102 | # Chunk it, baby... | ||
512 | 103 | c.putrequest(method, action) | ||
513 | 104 | |||
514 | 105 | for header, value in headers.items(): | ||
515 | 106 | c.putheader(header, value) | ||
516 | 107 | c.putheader('Transfer-Encoding', 'chunked') | ||
517 | 108 | c.endheaders() | ||
518 | 109 | |||
519 | 110 | chunk = body.read(self.CHUNKSIZE) | ||
520 | 111 | while chunk: | ||
521 | 112 | c.send('%x\r\n%s\r\n' % (len(chunk), chunk)) | ||
522 | 113 | chunk = body.read(self.CHUNKSIZE) | ||
523 | 114 | c.send('0\r\n\r\n') | ||
524 | 115 | else: | ||
525 | 116 | # Simple request... | ||
526 | 117 | c.request(method, action, body, headers) | ||
527 | 118 | res = c.getresponse() | ||
528 | 119 | status_code = self.get_status_code(res) | ||
529 | 120 | if status_code in (httplib.OK, | ||
530 | 121 | httplib.CREATED, | ||
531 | 122 | httplib.ACCEPTED, | ||
532 | 123 | httplib.NO_CONTENT): | ||
533 | 124 | return res | ||
534 | 125 | elif status_code == httplib.UNAUTHORIZED: | ||
535 | 126 | raise exception.NotAuthorized | ||
536 | 127 | elif status_code == httplib.FORBIDDEN: | ||
537 | 128 | raise exception.NotAuthorized | ||
538 | 129 | elif status_code == httplib.NOT_FOUND: | ||
539 | 130 | raise exception.NotFound | ||
540 | 131 | elif status_code == httplib.CONFLICT: | ||
541 | 132 | raise exception.Duplicate(res.read()) | ||
542 | 133 | elif status_code == httplib.BAD_REQUEST: | ||
543 | 134 | raise exception.Invalid(res.read()) | ||
544 | 135 | elif status_code == httplib.INTERNAL_SERVER_ERROR: | ||
545 | 136 | raise Exception("Internal Server error: %s" % res.read()) | ||
546 | 137 | else: | ||
547 | 138 | raise Exception("Unknown error occurred! %s" % res.read()) | ||
548 | 139 | |||
549 | 140 | except (socket.error, IOError), e: | ||
550 | 141 | raise exception.ClientConnectionError("Unable to connect to " | ||
551 | 142 | "server. Got error: %s" % e) | ||
552 | 143 | |||
553 | 144 | def get_status_code(self, response): | ||
554 | 145 | """ | ||
555 | 146 | Returns the integer status code from the response, which | ||
556 | 147 | can be either a Webob.Response (used in testing) or httplib.Response | ||
557 | 148 | """ | ||
558 | 149 | if hasattr(response, 'status_int'): | ||
559 | 150 | return response.status_int | ||
560 | 151 | else: | ||
561 | 152 | return response.status | ||
562 | 153 | |||
563 | 154 | def _extract_params(self, actual_params, allowed_params): | ||
564 | 155 | """ | ||
565 | 156 | Extract a subset of keys from a dictionary. The filters key | ||
566 | 157 | will also be extracted, and each of its values will be returned | ||
567 | 158 | as an individual param. | ||
568 | 159 | |||
569 | 160 | :param actual_params: dict of keys to filter | ||
570 | 161 | :param allowed_params: list of keys that 'actual_params' will be | ||
571 | 162 | reduced to | ||
572 | 163 | :retval subset of 'params' dict | ||
573 | 164 | """ | ||
574 | 165 | result = actual_params.get('filters', {}) | ||
575 | 166 | for allowed_param in allowed_params: | ||
576 | 167 | if allowed_param in actual_params: | ||
577 | 168 | result[allowed_param] = actual_params[allowed_param] | ||
578 | 169 | return result | ||
579 | 0 | 170 | ||
580 | === modified file 'glance/common/exception.py' | |||
581 | --- glance/common/exception.py 2011-06-15 14:47:00 +0000 | |||
582 | +++ glance/common/exception.py 2011-06-28 13:52:02 +0000 | |||
583 | @@ -83,6 +83,11 @@ | |||
584 | 83 | pass | 83 | pass |
585 | 84 | 84 | ||
586 | 85 | 85 | ||
587 | 86 | class ClientConnectionError(Exception): | ||
588 | 87 | """Error resulting from a client connecting to a server""" | ||
589 | 88 | pass | ||
590 | 89 | |||
591 | 90 | |||
592 | 86 | def wrap_exception(f): | 91 | def wrap_exception(f): |
593 | 87 | def _wrap(*args, **kw): | 92 | def _wrap(*args, **kw): |
594 | 88 | try: | 93 | try: |
595 | 89 | 94 | ||
596 | === modified file 'glance/registry/client.py' | |||
597 | --- glance/registry/client.py 2011-06-11 13:47:47 +0000 | |||
598 | +++ glance/registry/client.py 2011-06-28 13:52:02 +0000 | |||
599 | @@ -23,7 +23,8 @@ | |||
600 | 23 | import json | 23 | import json |
601 | 24 | import urllib | 24 | import urllib |
602 | 25 | 25 | ||
604 | 26 | from glance.client import BaseClient | 26 | from glance.common.client import BaseClient |
605 | 27 | from glance.registry import server | ||
606 | 27 | 28 | ||
607 | 28 | 29 | ||
608 | 29 | class RegistryClient(BaseClient): | 30 | class RegistryClient(BaseClient): |
609 | @@ -44,42 +45,32 @@ | |||
610 | 44 | port = port or self.DEFAULT_PORT | 45 | port = port or self.DEFAULT_PORT |
611 | 45 | super(RegistryClient, self).__init__(host, port, use_ssl) | 46 | super(RegistryClient, self).__init__(host, port, use_ssl) |
612 | 46 | 47 | ||
614 | 47 | def get_images(self, filters=None, marker=None, limit=None): | 48 | def get_images(self, **kwargs): |
615 | 48 | """ | 49 | """ |
616 | 49 | Returns a list of image id/name mappings from Registry | 50 | Returns a list of image id/name mappings from Registry |
617 | 50 | 51 | ||
618 | 51 | :param filters: dict of keys & expected values to filter results | 52 | :param filters: dict of keys & expected values to filter results |
619 | 52 | :param marker: image id after which to start page | 53 | :param marker: image id after which to start page |
620 | 53 | :param limit: max number of images to return | 54 | :param limit: max number of images to return |
621 | 55 | :param sort_key: results will be ordered by this image attribute | ||
622 | 56 | :param sort_dir: direction in which to to order results (asc, desc) | ||
623 | 54 | """ | 57 | """ |
632 | 55 | params = filters or {} | 58 | params = self._extract_params(kwargs, server.SUPPORTED_PARAMS) |
625 | 56 | |||
626 | 57 | if marker != None: | ||
627 | 58 | params['marker'] = marker | ||
628 | 59 | |||
629 | 60 | if limit != None: | ||
630 | 61 | params['limit'] = limit | ||
631 | 62 | |||
633 | 63 | res = self.do_request("GET", "/images", params=params) | 59 | res = self.do_request("GET", "/images", params=params) |
634 | 64 | data = json.loads(res.read())['images'] | 60 | data = json.loads(res.read())['images'] |
635 | 65 | return data | 61 | return data |
636 | 66 | 62 | ||
638 | 67 | def get_images_detailed(self, filters=None, marker=None, limit=None): | 63 | def get_images_detailed(self, **kwargs): |
639 | 68 | """ | 64 | """ |
640 | 69 | Returns a list of detailed image data mappings from Registry | 65 | Returns a list of detailed image data mappings from Registry |
641 | 70 | 66 | ||
642 | 71 | :param filters: dict of keys & expected values to filter results | 67 | :param filters: dict of keys & expected values to filter results |
643 | 72 | :param marker: image id after which to start page | 68 | :param marker: image id after which to start page |
644 | 73 | :param limit: max number of images to return | 69 | :param limit: max number of images to return |
645 | 70 | :param sort_key: results will be ordered by this image attribute | ||
646 | 71 | :param sort_dir: direction in which to to order results (asc, desc) | ||
647 | 74 | """ | 72 | """ |
656 | 75 | params = filters or {} | 73 | params = self._extract_params(kwargs, server.SUPPORTED_PARAMS) |
649 | 76 | |||
650 | 77 | if marker != None: | ||
651 | 78 | params['marker'] = marker | ||
652 | 79 | |||
653 | 80 | if limit != None: | ||
654 | 81 | params['limit'] = limit | ||
655 | 82 | |||
657 | 83 | res = self.do_request("GET", "/images/detail", params=params) | 74 | res = self.do_request("GET", "/images/detail", params=params) |
658 | 84 | data = json.loads(res.read())['images'] | 75 | data = json.loads(res.read())['images'] |
659 | 85 | return data | 76 | return data |
660 | 86 | 77 | ||
661 | === modified file 'glance/registry/db/api.py' | |||
662 | --- glance/registry/db/api.py 2011-05-31 19:32:17 +0000 | |||
663 | +++ glance/registry/db/api.py 2011-06-28 13:52:02 +0000 | |||
664 | @@ -23,7 +23,7 @@ | |||
665 | 23 | 23 | ||
666 | 24 | import logging | 24 | import logging |
667 | 25 | 25 | ||
669 | 26 | from sqlalchemy import create_engine, desc | 26 | from sqlalchemy import asc, create_engine, desc |
670 | 27 | from sqlalchemy.ext.declarative import declarative_base | 27 | from sqlalchemy.ext.declarative import declarative_base |
671 | 28 | from sqlalchemy.orm import exc | 28 | from sqlalchemy.orm import exc |
672 | 29 | from sqlalchemy.orm import joinedload | 29 | from sqlalchemy.orm import joinedload |
673 | @@ -129,7 +129,8 @@ | |||
674 | 129 | raise exception.NotFound("No image found with ID %s" % image_id) | 129 | raise exception.NotFound("No image found with ID %s" % image_id) |
675 | 130 | 130 | ||
676 | 131 | 131 | ||
678 | 132 | def image_get_all_public(context, filters=None, marker=None, limit=None): | 132 | def image_get_all_public(context, filters=None, marker=None, limit=None, |
679 | 133 | sort_key='created_at', sort_dir='desc'): | ||
680 | 133 | """Get all public images that match zero or more filters. | 134 | """Get all public images that match zero or more filters. |
681 | 134 | 135 | ||
682 | 135 | :param filters: dict of filter keys and values. If a 'properties' | 136 | :param filters: dict of filter keys and values. If a 'properties' |
683 | @@ -137,7 +138,8 @@ | |||
684 | 137 | filters on the image properties attribute | 138 | filters on the image properties attribute |
685 | 138 | :param marker: image id after which to start page | 139 | :param marker: image id after which to start page |
686 | 139 | :param limit: maximum number of images to return | 140 | :param limit: maximum number of images to return |
688 | 140 | 141 | :param sort_key: image attribute by which results should be sorted | |
689 | 142 | :param sort_dir: direction in which results should be sorted (asc, desc) | ||
690 | 141 | """ | 143 | """ |
691 | 142 | filters = filters or {} | 144 | filters = filters or {} |
692 | 143 | 145 | ||
693 | @@ -146,9 +148,17 @@ | |||
694 | 146 | options(joinedload(models.Image.properties)).\ | 148 | options(joinedload(models.Image.properties)).\ |
695 | 147 | filter_by(deleted=_deleted(context)).\ | 149 | filter_by(deleted=_deleted(context)).\ |
696 | 148 | filter_by(is_public=True).\ | 150 | filter_by(is_public=True).\ |
700 | 149 | filter(models.Image.status != 'killed').\ | 151 | filter(models.Image.status != 'killed') |
701 | 150 | order_by(desc(models.Image.created_at)).\ | 152 | |
702 | 151 | order_by(desc(models.Image.id)) | 153 | sort_dir_func = { |
703 | 154 | 'asc': asc, | ||
704 | 155 | 'desc': desc, | ||
705 | 156 | }[sort_dir] | ||
706 | 157 | |||
707 | 158 | sort_key_attr = getattr(models.Image, sort_key) | ||
708 | 159 | |||
709 | 160 | query = query.order_by(sort_dir_func(sort_key_attr)).\ | ||
710 | 161 | order_by(sort_dir_func(models.Image.id)) | ||
711 | 152 | 162 | ||
712 | 153 | if 'size_min' in filters: | 163 | if 'size_min' in filters: |
713 | 154 | query = query.filter(models.Image.size >= filters['size_min']) | 164 | query = query.filter(models.Image.size >= filters['size_min']) |
714 | 155 | 165 | ||
715 | === modified file 'glance/registry/server.py' | |||
716 | --- glance/registry/server.py 2011-06-15 14:47:00 +0000 | |||
717 | +++ glance/registry/server.py 2011-06-28 13:52:02 +0000 | |||
718 | @@ -39,8 +39,15 @@ | |||
719 | 39 | SUPPORTED_FILTERS = ['name', 'status', 'container_format', 'disk_format', | 39 | SUPPORTED_FILTERS = ['name', 'status', 'container_format', 'disk_format', |
720 | 40 | 'size_min', 'size_max'] | 40 | 'size_min', 'size_max'] |
721 | 41 | 41 | ||
722 | 42 | SUPPORTED_SORT_KEYS = ('name', 'status', 'container_format', 'disk_format', | ||
723 | 43 | 'size', 'id', 'created_at', 'updated_at') | ||
724 | 44 | |||
725 | 45 | SUPPORTED_SORT_DIRS = ('asc', 'desc') | ||
726 | 46 | |||
727 | 42 | MAX_ITEM_LIMIT = 25 | 47 | MAX_ITEM_LIMIT = 25 |
728 | 43 | 48 | ||
729 | 49 | SUPPORTED_PARAMS = ('limit', 'marker', 'sort_key', 'sort_dir') | ||
730 | 50 | |||
731 | 44 | 51 | ||
732 | 45 | class Controller(object): | 52 | class Controller(object): |
733 | 46 | """Controller for the reference implementation registry server""" | 53 | """Controller for the reference implementation registry server""" |
734 | @@ -69,14 +76,7 @@ | |||
735 | 69 | } | 76 | } |
736 | 70 | 77 | ||
737 | 71 | """ | 78 | """ |
746 | 72 | params = { | 79 | params = self._get_query_params(req) |
739 | 73 | 'filters': self._get_filters(req), | ||
740 | 74 | 'limit': self._get_limit(req), | ||
741 | 75 | } | ||
742 | 76 | |||
743 | 77 | if 'marker' in req.str_params: | ||
744 | 78 | params['marker'] = self._get_marker(req) | ||
745 | 79 | |||
747 | 80 | images = db_api.image_get_all_public(None, **params) | 80 | images = db_api.image_get_all_public(None, **params) |
748 | 81 | 81 | ||
749 | 82 | results = [] | 82 | results = [] |
750 | @@ -99,19 +99,33 @@ | |||
751 | 99 | all image model fields. | 99 | all image model fields. |
752 | 100 | 100 | ||
753 | 101 | """ | 101 | """ |
762 | 102 | params = { | 102 | params = self._get_query_params(req) |
755 | 103 | 'filters': self._get_filters(req), | ||
756 | 104 | 'limit': self._get_limit(req), | ||
757 | 105 | } | ||
758 | 106 | |||
759 | 107 | if 'marker' in req.str_params: | ||
760 | 108 | params['marker'] = self._get_marker(req) | ||
761 | 109 | |||
763 | 110 | images = db_api.image_get_all_public(None, **params) | 103 | images = db_api.image_get_all_public(None, **params) |
764 | 111 | 104 | ||
765 | 112 | image_dicts = [make_image_dict(i) for i in images] | 105 | image_dicts = [make_image_dict(i) for i in images] |
766 | 113 | return dict(images=image_dicts) | 106 | return dict(images=image_dicts) |
767 | 114 | 107 | ||
768 | 108 | def _get_query_params(self, req): | ||
769 | 109 | """ | ||
770 | 110 | Extract necessary query parameters from http request. | ||
771 | 111 | |||
772 | 112 | :param req: the Request object coming from the wsgi layer | ||
773 | 113 | :retval dictionary of filters to apply to list of images | ||
774 | 114 | """ | ||
775 | 115 | params = { | ||
776 | 116 | 'filters': self._get_filters(req), | ||
777 | 117 | 'limit': self._get_limit(req), | ||
778 | 118 | 'sort_key': self._get_sort_key(req), | ||
779 | 119 | 'sort_dir': self._get_sort_dir(req), | ||
780 | 120 | 'marker': self._get_marker(req), | ||
781 | 121 | } | ||
782 | 122 | |||
783 | 123 | for key, value in params.items(): | ||
784 | 124 | if value is None: | ||
785 | 125 | del params[key] | ||
786 | 126 | |||
787 | 127 | return params | ||
788 | 128 | |||
789 | 115 | def _get_filters(self, req): | 129 | def _get_filters(self, req): |
790 | 116 | """Return a dictionary of query param filters from the request | 130 | """Return a dictionary of query param filters from the request |
791 | 117 | 131 | ||
792 | @@ -148,12 +162,35 @@ | |||
793 | 148 | 162 | ||
794 | 149 | def _get_marker(self, req): | 163 | def _get_marker(self, req): |
795 | 150 | """Parse a marker query param into something usable.""" | 164 | """Parse a marker query param into something usable.""" |
796 | 165 | marker = req.str_params.get('marker', None) | ||
797 | 166 | |||
798 | 167 | if marker is None: | ||
799 | 168 | return None | ||
800 | 169 | |||
801 | 151 | try: | 170 | try: |
803 | 152 | marker = int(req.str_params.get('marker', None)) | 171 | marker = int(marker) |
804 | 153 | except ValueError: | 172 | except ValueError: |
805 | 154 | raise exc.HTTPBadRequest("marker param must be an integer") | 173 | raise exc.HTTPBadRequest("marker param must be an integer") |
806 | 155 | return marker | 174 | return marker |
807 | 156 | 175 | ||
808 | 176 | def _get_sort_key(self, req): | ||
809 | 177 | """Parse a sort key query param from the request object.""" | ||
810 | 178 | sort_key = req.str_params.get('sort_key', None) | ||
811 | 179 | if sort_key is not None and sort_key not in SUPPORTED_SORT_KEYS: | ||
812 | 180 | _keys = ', '.join(SUPPORTED_SORT_KEYS) | ||
813 | 181 | msg = "Unsupported sort_key. Acceptable values: %s" % (_keys,) | ||
814 | 182 | raise exc.HTTPBadRequest(explanation=msg) | ||
815 | 183 | return sort_key | ||
816 | 184 | |||
817 | 185 | def _get_sort_dir(self, req): | ||
818 | 186 | """Parse a sort direction query param from the request object.""" | ||
819 | 187 | sort_dir = req.str_params.get('sort_dir', None) | ||
820 | 188 | if sort_dir is not None and sort_dir not in SUPPORTED_SORT_DIRS: | ||
821 | 189 | _keys = ', '.join(SUPPORTED_SORT_DIRS) | ||
822 | 190 | msg = "Unsupported sort_dir. Acceptable values: %s" % (_keys,) | ||
823 | 191 | raise exc.HTTPBadRequest(explanation=msg) | ||
824 | 192 | return sort_dir | ||
825 | 193 | |||
826 | 157 | def show(self, req, id): | 194 | def show(self, req, id): |
827 | 158 | """Return data about the given image id.""" | 195 | """Return data about the given image id.""" |
828 | 159 | try: | 196 | try: |
829 | 160 | 197 | ||
830 | === modified file 'tests/functional/test_curl_api.py' | |||
831 | --- tests/functional/test_curl_api.py 2011-06-22 14:19:33 +0000 | |||
832 | +++ tests/functional/test_curl_api.py 2011-06-28 13:52:02 +0000 | |||
833 | @@ -1117,3 +1117,118 @@ | |||
834 | 1117 | self.assertEqual(int(images[0]['id']), 2) | 1117 | self.assertEqual(int(images[0]['id']), 2) |
835 | 1118 | 1118 | ||
836 | 1119 | self.stop_servers() | 1119 | self.stop_servers() |
837 | 1120 | |||
838 | 1121 | def test_ordered_images(self): | ||
839 | 1122 | """ | ||
840 | 1123 | Set up three test images and ensure each query param filter works | ||
841 | 1124 | """ | ||
842 | 1125 | self.cleanup() | ||
843 | 1126 | self.start_servers() | ||
844 | 1127 | |||
845 | 1128 | api_port = self.api_port | ||
846 | 1129 | registry_port = self.registry_port | ||
847 | 1130 | |||
848 | 1131 | # 0. GET /images | ||
849 | 1132 | # Verify no public images | ||
850 | 1133 | cmd = "curl http://0.0.0.0:%d/v1/images" % api_port | ||
851 | 1134 | |||
852 | 1135 | exitcode, out, err = execute(cmd) | ||
853 | 1136 | |||
854 | 1137 | self.assertEqual(0, exitcode) | ||
855 | 1138 | self.assertEqual('{"images": []}', out.strip()) | ||
856 | 1139 | |||
857 | 1140 | # 1. POST /images with three public images with various attributes | ||
858 | 1141 | cmd = ("curl -i -X POST " | ||
859 | 1142 | "-H 'Expect: ' " # Necessary otherwise sends 100 Continue | ||
860 | 1143 | "-H 'X-Image-Meta-Name: Image1' " | ||
861 | 1144 | "-H 'X-Image-Meta-Status: active' " | ||
862 | 1145 | "-H 'X-Image-Meta-Container-Format: ovf' " | ||
863 | 1146 | "-H 'X-Image-Meta-Disk-Format: vdi' " | ||
864 | 1147 | "-H 'X-Image-Meta-Size: 19' " | ||
865 | 1148 | "-H 'X-Image-Meta-Is-Public: True' " | ||
866 | 1149 | "http://0.0.0.0:%d/v1/images") % api_port | ||
867 | 1150 | |||
868 | 1151 | exitcode, out, err = execute(cmd) | ||
869 | 1152 | self.assertEqual(0, exitcode) | ||
870 | 1153 | |||
871 | 1154 | lines = out.split("\r\n") | ||
872 | 1155 | status_line = lines[0] | ||
873 | 1156 | |||
874 | 1157 | self.assertEqual("HTTP/1.1 201 Created", status_line) | ||
875 | 1158 | |||
876 | 1159 | cmd = ("curl -i -X POST " | ||
877 | 1160 | "-H 'Expect: ' " # Necessary otherwise sends 100 Continue | ||
878 | 1161 | "-H 'X-Image-Meta-Name: ASDF' " | ||
879 | 1162 | "-H 'X-Image-Meta-Status: active' " | ||
880 | 1163 | "-H 'X-Image-Meta-Container-Format: bare' " | ||
881 | 1164 | "-H 'X-Image-Meta-Disk-Format: iso' " | ||
882 | 1165 | "-H 'X-Image-Meta-Size: 2' " | ||
883 | 1166 | "-H 'X-Image-Meta-Is-Public: True' " | ||
884 | 1167 | "http://0.0.0.0:%d/v1/images") % api_port | ||
885 | 1168 | |||
886 | 1169 | exitcode, out, err = execute(cmd) | ||
887 | 1170 | self.assertEqual(0, exitcode) | ||
888 | 1171 | |||
889 | 1172 | lines = out.split("\r\n") | ||
890 | 1173 | status_line = lines[0] | ||
891 | 1174 | |||
892 | 1175 | self.assertEqual("HTTP/1.1 201 Created", status_line) | ||
893 | 1176 | cmd = ("curl -i -X POST " | ||
894 | 1177 | "-H 'Expect: ' " # Necessary otherwise sends 100 Continue | ||
895 | 1178 | "-H 'X-Image-Meta-Name: XYZ' " | ||
896 | 1179 | "-H 'X-Image-Meta-Status: saving' " | ||
897 | 1180 | "-H 'X-Image-Meta-Container-Format: ami' " | ||
898 | 1181 | "-H 'X-Image-Meta-Disk-Format: ami' " | ||
899 | 1182 | "-H 'X-Image-Meta-Size: 5' " | ||
900 | 1183 | "-H 'X-Image-Meta-Is-Public: True' " | ||
901 | 1184 | "http://0.0.0.0:%d/v1/images") % api_port | ||
902 | 1185 | |||
903 | 1186 | exitcode, out, err = execute(cmd) | ||
904 | 1187 | self.assertEqual(0, exitcode) | ||
905 | 1188 | |||
906 | 1189 | lines = out.split("\r\n") | ||
907 | 1190 | status_line = lines[0] | ||
908 | 1191 | |||
909 | 1192 | self.assertEqual("HTTP/1.1 201 Created", status_line) | ||
910 | 1193 | |||
911 | 1194 | # 2. GET /images with no query params | ||
912 | 1195 | # Verify three public images sorted by created_at desc | ||
913 | 1196 | cmd = "curl http://0.0.0.0:%d/v1/images" % api_port | ||
914 | 1197 | |||
915 | 1198 | exitcode, out, err = execute(cmd) | ||
916 | 1199 | |||
917 | 1200 | self.assertEqual(0, exitcode) | ||
918 | 1201 | images = json.loads(out.strip())['images'] | ||
919 | 1202 | |||
920 | 1203 | self.assertEqual(len(images), 3) | ||
921 | 1204 | self.assertEqual(images[0]['id'], 3) | ||
922 | 1205 | self.assertEqual(images[1]['id'], 2) | ||
923 | 1206 | self.assertEqual(images[2]['id'], 1) | ||
924 | 1207 | |||
925 | 1208 | # 3. GET /images sorted by name asc | ||
926 | 1209 | params = 'sort_key=name&sort_dir=asc' | ||
927 | 1210 | cmd = "curl 'http://0.0.0.0:%d/v1/images?%s'" % (api_port, params) | ||
928 | 1211 | |||
929 | 1212 | exitcode, out, err = execute(cmd) | ||
930 | 1213 | |||
931 | 1214 | self.assertEqual(0, exitcode) | ||
932 | 1215 | images = json.loads(out.strip())['images'] | ||
933 | 1216 | |||
934 | 1217 | self.assertEqual(len(images), 3) | ||
935 | 1218 | self.assertEqual(images[0]['id'], 2) | ||
936 | 1219 | self.assertEqual(images[1]['id'], 1) | ||
937 | 1220 | self.assertEqual(images[2]['id'], 3) | ||
938 | 1221 | |||
939 | 1222 | # 4. GET /images sorted by size desc | ||
940 | 1223 | params = 'sort_key=size&sort_dir=desc' | ||
941 | 1224 | cmd = "curl 'http://0.0.0.0:%d/v1/images?%s'" % (api_port, params) | ||
942 | 1225 | |||
943 | 1226 | exitcode, out, err = execute(cmd) | ||
944 | 1227 | |||
945 | 1228 | self.assertEqual(0, exitcode) | ||
946 | 1229 | images = json.loads(out.strip())['images'] | ||
947 | 1230 | |||
948 | 1231 | self.assertEqual(len(images), 3) | ||
949 | 1232 | self.assertEqual(images[0]['id'], 1) | ||
950 | 1233 | self.assertEqual(images[1]['id'], 3) | ||
951 | 1234 | self.assertEqual(images[2]['id'], 2) | ||
952 | 1120 | 1235 | ||
953 | === modified file 'tests/stubs.py' | |||
954 | --- tests/stubs.py 2011-05-31 19:32:17 +0000 | |||
955 | +++ tests/stubs.py 2011-06-28 13:52:02 +0000 | |||
956 | @@ -28,6 +28,7 @@ | |||
957 | 28 | import stubout | 28 | import stubout |
958 | 29 | import webob | 29 | import webob |
959 | 30 | 30 | ||
960 | 31 | import glance.common.client | ||
961 | 31 | from glance.common import exception | 32 | from glance.common import exception |
962 | 32 | from glance.registry import server as rserver | 33 | from glance.registry import server as rserver |
963 | 33 | from glance.api import v1 as server | 34 | from glance.api import v1 as server |
964 | @@ -254,9 +255,9 @@ | |||
965 | 254 | for i in self.response.app_iter: | 255 | for i in self.response.app_iter: |
966 | 255 | yield i | 256 | yield i |
967 | 256 | 257 | ||
969 | 257 | stubs.Set(glance.client.BaseClient, 'get_connection_type', | 258 | stubs.Set(glance.common.client.BaseClient, 'get_connection_type', |
970 | 258 | fake_get_connection_type) | 259 | fake_get_connection_type) |
972 | 259 | stubs.Set(glance.client.ImageBodyIterator, '__iter__', | 260 | stubs.Set(glance.common.client.ImageBodyIterator, '__iter__', |
973 | 260 | fake_image_iter) | 261 | fake_image_iter) |
974 | 261 | 262 | ||
975 | 262 | 263 | ||
976 | @@ -388,8 +389,8 @@ | |||
977 | 388 | else: | 389 | else: |
978 | 389 | return images[0] | 390 | return images[0] |
979 | 390 | 391 | ||
982 | 391 | def image_get_all_public(self, _context, filters=None, | 392 | def image_get_all_public(self, _context, filters=None, marker=None, |
983 | 392 | marker=None, limit=1000): | 393 | limit=1000, sort_key=None, sort_dir=None): |
984 | 393 | images = [f for f in self.images if f['is_public'] == True] | 394 | images = [f for f in self.images if f['is_public'] == True] |
985 | 394 | 395 | ||
986 | 395 | if 'size_min' in filters: | 396 | if 'size_min' in filters: |
987 | @@ -414,16 +415,24 @@ | |||
988 | 414 | for k, v in filters.items(): | 415 | for k, v in filters.items(): |
989 | 415 | images = [f for f in images if f[k] == v] | 416 | images = [f for f in images if f[k] == v] |
990 | 416 | 417 | ||
991 | 418 | # sorted func expects func that compares in descending order | ||
992 | 417 | def image_cmp(x, y): | 419 | def image_cmp(x, y): |
996 | 418 | if x['created_at'] > y['created_at']: | 420 | _sort_dir = sort_dir or 'desc' |
997 | 419 | return 1 | 421 | multiplier = { |
998 | 420 | elif x['created_at'] == y['created_at']: | 422 | 'asc': -1, |
999 | 423 | 'desc': 1, | ||
1000 | 424 | }[_sort_dir] | ||
1001 | 425 | |||
1002 | 426 | _sort_key = sort_key or 'created_at' | ||
1003 | 427 | if x[_sort_key] > y[_sort_key]: | ||
1004 | 428 | return 1 * multiplier | ||
1005 | 429 | elif x[_sort_key] == y[_sort_key]: | ||
1006 | 421 | if x['id'] > y['id']: | 430 | if x['id'] > y['id']: |
1008 | 422 | return 1 | 431 | return 1 * multiplier |
1009 | 423 | else: | 432 | else: |
1011 | 424 | return -1 | 433 | return -1 * multiplier |
1012 | 425 | else: | 434 | else: |
1014 | 426 | return -1 | 435 | return -1 * multiplier |
1015 | 427 | 436 | ||
1016 | 428 | images = sorted(images, cmp=image_cmp) | 437 | images = sorted(images, cmp=image_cmp) |
1017 | 429 | images.reverse() | 438 | images.reverse() |
1018 | 430 | 439 | ||
1019 | === modified file 'tests/unit/test_api.py' | |||
1020 | --- tests/unit/test_api.py 2011-06-24 20:57:02 +0000 | |||
1021 | +++ tests/unit/test_api.py 2011-06-28 13:52:02 +0000 | |||
1022 | @@ -285,6 +285,398 @@ | |||
1023 | 285 | for image in images: | 285 | for image in images: |
1024 | 286 | self.assertEqual('new name! #123', image['name']) | 286 | self.assertEqual('new name! #123', image['name']) |
1025 | 287 | 287 | ||
1026 | 288 | def test_get_index_sort_default_created_at_desc(self): | ||
1027 | 289 | """ | ||
1028 | 290 | Tests that the /images registry API returns list of | ||
1029 | 291 | public images that conforms to a default sort key/dir | ||
1030 | 292 | """ | ||
1031 | 293 | time1 = datetime.datetime.utcnow() + datetime.timedelta(seconds=5) | ||
1032 | 294 | time2 = datetime.datetime.utcnow() | ||
1033 | 295 | |||
1034 | 296 | extra_fixture = {'id': 3, | ||
1035 | 297 | 'status': 'active', | ||
1036 | 298 | 'is_public': True, | ||
1037 | 299 | 'disk_format': 'vhd', | ||
1038 | 300 | 'container_format': 'ovf', | ||
1039 | 301 | 'name': 'new name! #123', | ||
1040 | 302 | 'size': 19, | ||
1041 | 303 | 'checksum': None, | ||
1042 | 304 | 'created_at': time1} | ||
1043 | 305 | |||
1044 | 306 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1045 | 307 | |||
1046 | 308 | extra_fixture = {'id': 4, | ||
1047 | 309 | 'status': 'active', | ||
1048 | 310 | 'is_public': True, | ||
1049 | 311 | 'disk_format': 'vhd', | ||
1050 | 312 | 'container_format': 'ovf', | ||
1051 | 313 | 'name': 'new name! #123', | ||
1052 | 314 | 'size': 20, | ||
1053 | 315 | 'checksum': None, | ||
1054 | 316 | 'created_at': time1} | ||
1055 | 317 | |||
1056 | 318 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1057 | 319 | |||
1058 | 320 | extra_fixture = {'id': 5, | ||
1059 | 321 | 'status': 'active', | ||
1060 | 322 | 'is_public': True, | ||
1061 | 323 | 'disk_format': 'vhd', | ||
1062 | 324 | 'container_format': 'ovf', | ||
1063 | 325 | 'name': 'new name! #123', | ||
1064 | 326 | 'size': 20, | ||
1065 | 327 | 'checksum': None, | ||
1066 | 328 | 'created_at': time2} | ||
1067 | 329 | |||
1068 | 330 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1069 | 331 | |||
1070 | 332 | req = webob.Request.blank('/images') | ||
1071 | 333 | res = req.get_response(self.api) | ||
1072 | 334 | res_dict = json.loads(res.body) | ||
1073 | 335 | self.assertEquals(res.status_int, 200) | ||
1074 | 336 | |||
1075 | 337 | images = res_dict['images'] | ||
1076 | 338 | self.assertEquals(len(images), 4) | ||
1077 | 339 | self.assertEquals(int(images[0]['id']), 4) | ||
1078 | 340 | self.assertEquals(int(images[1]['id']), 3) | ||
1079 | 341 | self.assertEquals(int(images[2]['id']), 5) | ||
1080 | 342 | self.assertEquals(int(images[3]['id']), 2) | ||
1081 | 343 | |||
1082 | 344 | def test_get_index_bad_sort_key(self): | ||
1083 | 345 | """Ensure a 400 is returned when a bad sort_key is provided.""" | ||
1084 | 346 | req = webob.Request.blank('/images?sort_key=asdf') | ||
1085 | 347 | res = req.get_response(self.api) | ||
1086 | 348 | self.assertEqual(400, res.status_int) | ||
1087 | 349 | |||
1088 | 350 | def test_get_index_bad_sort_dir(self): | ||
1089 | 351 | """Ensure a 400 is returned when a bad sort_dir is provided.""" | ||
1090 | 352 | req = webob.Request.blank('/images?sort_dir=asdf') | ||
1091 | 353 | res = req.get_response(self.api) | ||
1092 | 354 | self.assertEqual(400, res.status_int) | ||
1093 | 355 | |||
1094 | 356 | def test_get_index_sort_id_desc(self): | ||
1095 | 357 | """ | ||
1096 | 358 | Tests that the /images registry API returns list of | ||
1097 | 359 | public images sorted by id in descending order. | ||
1098 | 360 | """ | ||
1099 | 361 | extra_fixture = {'id': 3, | ||
1100 | 362 | 'status': 'active', | ||
1101 | 363 | 'is_public': True, | ||
1102 | 364 | 'disk_format': 'vhd', | ||
1103 | 365 | 'container_format': 'ovf', | ||
1104 | 366 | 'name': 'asdf', | ||
1105 | 367 | 'size': 19, | ||
1106 | 368 | 'checksum': None} | ||
1107 | 369 | |||
1108 | 370 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1109 | 371 | |||
1110 | 372 | extra_fixture = {'id': 4, | ||
1111 | 373 | 'status': 'active', | ||
1112 | 374 | 'is_public': True, | ||
1113 | 375 | 'disk_format': 'vhd', | ||
1114 | 376 | 'container_format': 'ovf', | ||
1115 | 377 | 'name': 'xyz', | ||
1116 | 378 | 'size': 20, | ||
1117 | 379 | 'checksum': None} | ||
1118 | 380 | |||
1119 | 381 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1120 | 382 | |||
1121 | 383 | req = webob.Request.blank('/images?sort_key=id&sort_dir=desc') | ||
1122 | 384 | res = req.get_response(self.api) | ||
1123 | 385 | self.assertEquals(res.status_int, 200) | ||
1124 | 386 | res_dict = json.loads(res.body) | ||
1125 | 387 | |||
1126 | 388 | images = res_dict['images'] | ||
1127 | 389 | self.assertEquals(len(images), 3) | ||
1128 | 390 | self.assertEquals(int(images[0]['id']), 4) | ||
1129 | 391 | self.assertEquals(int(images[1]['id']), 3) | ||
1130 | 392 | self.assertEquals(int(images[2]['id']), 2) | ||
1131 | 393 | |||
1132 | 394 | def test_get_index_sort_name_asc(self): | ||
1133 | 395 | """ | ||
1134 | 396 | Tests that the /images registry API returns list of | ||
1135 | 397 | public images sorted alphabetically by name in | ||
1136 | 398 | ascending order. | ||
1137 | 399 | """ | ||
1138 | 400 | extra_fixture = {'id': 3, | ||
1139 | 401 | 'status': 'active', | ||
1140 | 402 | 'is_public': True, | ||
1141 | 403 | 'disk_format': 'vhd', | ||
1142 | 404 | 'container_format': 'ovf', | ||
1143 | 405 | 'name': 'asdf', | ||
1144 | 406 | 'size': 19, | ||
1145 | 407 | 'checksum': None} | ||
1146 | 408 | |||
1147 | 409 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1148 | 410 | |||
1149 | 411 | extra_fixture = {'id': 4, | ||
1150 | 412 | 'status': 'active', | ||
1151 | 413 | 'is_public': True, | ||
1152 | 414 | 'disk_format': 'vhd', | ||
1153 | 415 | 'container_format': 'ovf', | ||
1154 | 416 | 'name': 'xyz', | ||
1155 | 417 | 'size': 20, | ||
1156 | 418 | 'checksum': None} | ||
1157 | 419 | |||
1158 | 420 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1159 | 421 | |||
1160 | 422 | req = webob.Request.blank('/images?sort_key=name&sort_dir=asc') | ||
1161 | 423 | res = req.get_response(self.api) | ||
1162 | 424 | self.assertEquals(res.status_int, 200) | ||
1163 | 425 | res_dict = json.loads(res.body) | ||
1164 | 426 | |||
1165 | 427 | images = res_dict['images'] | ||
1166 | 428 | self.assertEquals(len(images), 3) | ||
1167 | 429 | self.assertEquals(int(images[0]['id']), 3) | ||
1168 | 430 | self.assertEquals(int(images[1]['id']), 2) | ||
1169 | 431 | self.assertEquals(int(images[2]['id']), 4) | ||
1170 | 432 | |||
1171 | 433 | def test_get_index_sort_status_desc(self): | ||
1172 | 434 | """ | ||
1173 | 435 | Tests that the /images registry API returns list of | ||
1174 | 436 | public images sorted alphabetically by status in | ||
1175 | 437 | descending order. | ||
1176 | 438 | """ | ||
1177 | 439 | extra_fixture = {'id': 3, | ||
1178 | 440 | 'status': 'killed', | ||
1179 | 441 | 'is_public': True, | ||
1180 | 442 | 'disk_format': 'vhd', | ||
1181 | 443 | 'container_format': 'ovf', | ||
1182 | 444 | 'name': 'asdf', | ||
1183 | 445 | 'size': 19, | ||
1184 | 446 | 'checksum': None} | ||
1185 | 447 | |||
1186 | 448 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1187 | 449 | |||
1188 | 450 | extra_fixture = {'id': 4, | ||
1189 | 451 | 'status': 'active', | ||
1190 | 452 | 'is_public': True, | ||
1191 | 453 | 'disk_format': 'vhd', | ||
1192 | 454 | 'container_format': 'ovf', | ||
1193 | 455 | 'name': 'xyz', | ||
1194 | 456 | 'size': 20, | ||
1195 | 457 | 'checksum': None} | ||
1196 | 458 | |||
1197 | 459 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1198 | 460 | |||
1199 | 461 | req = webob.Request.blank('/images?sort_key=status&sort_dir=desc') | ||
1200 | 462 | res = req.get_response(self.api) | ||
1201 | 463 | self.assertEquals(res.status_int, 200) | ||
1202 | 464 | res_dict = json.loads(res.body) | ||
1203 | 465 | |||
1204 | 466 | images = res_dict['images'] | ||
1205 | 467 | self.assertEquals(len(images), 3) | ||
1206 | 468 | self.assertEquals(int(images[0]['id']), 3) | ||
1207 | 469 | self.assertEquals(int(images[1]['id']), 4) | ||
1208 | 470 | self.assertEquals(int(images[2]['id']), 2) | ||
1209 | 471 | |||
1210 | 472 | def test_get_index_sort_disk_format_asc(self): | ||
1211 | 473 | """ | ||
1212 | 474 | Tests that the /images registry API returns list of | ||
1213 | 475 | public images sorted alphabetically by disk_format in | ||
1214 | 476 | ascending order. | ||
1215 | 477 | """ | ||
1216 | 478 | extra_fixture = {'id': 3, | ||
1217 | 479 | 'status': 'active', | ||
1218 | 480 | 'is_public': True, | ||
1219 | 481 | 'disk_format': 'ami', | ||
1220 | 482 | 'container_format': 'ami', | ||
1221 | 483 | 'name': 'asdf', | ||
1222 | 484 | 'size': 19, | ||
1223 | 485 | 'checksum': None} | ||
1224 | 486 | |||
1225 | 487 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1226 | 488 | |||
1227 | 489 | extra_fixture = {'id': 4, | ||
1228 | 490 | 'status': 'active', | ||
1229 | 491 | 'is_public': True, | ||
1230 | 492 | 'disk_format': 'vdi', | ||
1231 | 493 | 'container_format': 'ovf', | ||
1232 | 494 | 'name': 'xyz', | ||
1233 | 495 | 'size': 20, | ||
1234 | 496 | 'checksum': None} | ||
1235 | 497 | |||
1236 | 498 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1237 | 499 | |||
1238 | 500 | req = webob.Request.blank('/images?sort_key=disk_format&sort_dir=asc') | ||
1239 | 501 | res = req.get_response(self.api) | ||
1240 | 502 | self.assertEquals(res.status_int, 200) | ||
1241 | 503 | res_dict = json.loads(res.body) | ||
1242 | 504 | |||
1243 | 505 | images = res_dict['images'] | ||
1244 | 506 | self.assertEquals(len(images), 3) | ||
1245 | 507 | self.assertEquals(int(images[0]['id']), 3) | ||
1246 | 508 | self.assertEquals(int(images[1]['id']), 4) | ||
1247 | 509 | self.assertEquals(int(images[2]['id']), 2) | ||
1248 | 510 | |||
1249 | 511 | def test_get_index_sort_container_format_desc(self): | ||
1250 | 512 | """ | ||
1251 | 513 | Tests that the /images registry API returns list of | ||
1252 | 514 | public images sorted alphabetically by container_format in | ||
1253 | 515 | descending order. | ||
1254 | 516 | """ | ||
1255 | 517 | extra_fixture = {'id': 3, | ||
1256 | 518 | 'status': 'active', | ||
1257 | 519 | 'is_public': True, | ||
1258 | 520 | 'disk_format': 'ami', | ||
1259 | 521 | 'container_format': 'ami', | ||
1260 | 522 | 'name': 'asdf', | ||
1261 | 523 | 'size': 19, | ||
1262 | 524 | 'checksum': None} | ||
1263 | 525 | |||
1264 | 526 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1265 | 527 | |||
1266 | 528 | extra_fixture = {'id': 4, | ||
1267 | 529 | 'status': 'active', | ||
1268 | 530 | 'is_public': True, | ||
1269 | 531 | 'disk_format': 'iso', | ||
1270 | 532 | 'container_format': 'bare', | ||
1271 | 533 | 'name': 'xyz', | ||
1272 | 534 | 'size': 20, | ||
1273 | 535 | 'checksum': None} | ||
1274 | 536 | |||
1275 | 537 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1276 | 538 | |||
1277 | 539 | url = '/images?sort_key=container_format&sort_dir=desc' | ||
1278 | 540 | req = webob.Request.blank(url) | ||
1279 | 541 | res = req.get_response(self.api) | ||
1280 | 542 | self.assertEquals(res.status_int, 200) | ||
1281 | 543 | res_dict = json.loads(res.body) | ||
1282 | 544 | |||
1283 | 545 | images = res_dict['images'] | ||
1284 | 546 | self.assertEquals(len(images), 3) | ||
1285 | 547 | self.assertEquals(int(images[0]['id']), 2) | ||
1286 | 548 | self.assertEquals(int(images[1]['id']), 4) | ||
1287 | 549 | self.assertEquals(int(images[2]['id']), 3) | ||
1288 | 550 | |||
1289 | 551 | def test_get_index_sort_size_asc(self): | ||
1290 | 552 | """ | ||
1291 | 553 | Tests that the /images registry API returns list of | ||
1292 | 554 | public images sorted by size in ascending order. | ||
1293 | 555 | """ | ||
1294 | 556 | extra_fixture = {'id': 3, | ||
1295 | 557 | 'status': 'active', | ||
1296 | 558 | 'is_public': True, | ||
1297 | 559 | 'disk_format': 'ami', | ||
1298 | 560 | 'container_format': 'ami', | ||
1299 | 561 | 'name': 'asdf', | ||
1300 | 562 | 'size': 100, | ||
1301 | 563 | 'checksum': None} | ||
1302 | 564 | |||
1303 | 565 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1304 | 566 | |||
1305 | 567 | extra_fixture = {'id': 4, | ||
1306 | 568 | 'status': 'active', | ||
1307 | 569 | 'is_public': True, | ||
1308 | 570 | 'disk_format': 'iso', | ||
1309 | 571 | 'container_format': 'bare', | ||
1310 | 572 | 'name': 'xyz', | ||
1311 | 573 | 'size': 2, | ||
1312 | 574 | 'checksum': None} | ||
1313 | 575 | |||
1314 | 576 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1315 | 577 | |||
1316 | 578 | url = '/images?sort_key=size&sort_dir=asc' | ||
1317 | 579 | req = webob.Request.blank(url) | ||
1318 | 580 | res = req.get_response(self.api) | ||
1319 | 581 | self.assertEquals(res.status_int, 200) | ||
1320 | 582 | res_dict = json.loads(res.body) | ||
1321 | 583 | |||
1322 | 584 | images = res_dict['images'] | ||
1323 | 585 | self.assertEquals(len(images), 3) | ||
1324 | 586 | self.assertEquals(int(images[0]['id']), 4) | ||
1325 | 587 | self.assertEquals(int(images[1]['id']), 2) | ||
1326 | 588 | self.assertEquals(int(images[2]['id']), 3) | ||
1327 | 589 | |||
1328 | 590 | def test_get_index_sort_created_at_asc(self): | ||
1329 | 591 | """ | ||
1330 | 592 | Tests that the /images registry API returns list of | ||
1331 | 593 | public images sorted by created_at in ascending order. | ||
1332 | 594 | """ | ||
1333 | 595 | now = datetime.datetime.utcnow() | ||
1334 | 596 | time1 = now + datetime.timedelta(seconds=5) | ||
1335 | 597 | time2 = now | ||
1336 | 598 | |||
1337 | 599 | extra_fixture = {'id': 3, | ||
1338 | 600 | 'status': 'active', | ||
1339 | 601 | 'is_public': True, | ||
1340 | 602 | 'disk_format': 'vhd', | ||
1341 | 603 | 'container_format': 'ovf', | ||
1342 | 604 | 'name': 'new name! #123', | ||
1343 | 605 | 'size': 19, | ||
1344 | 606 | 'checksum': None, | ||
1345 | 607 | 'created_at': time1} | ||
1346 | 608 | |||
1347 | 609 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1348 | 610 | |||
1349 | 611 | extra_fixture = {'id': 4, | ||
1350 | 612 | 'status': 'active', | ||
1351 | 613 | 'is_public': True, | ||
1352 | 614 | 'disk_format': 'vhd', | ||
1353 | 615 | 'container_format': 'ovf', | ||
1354 | 616 | 'name': 'new name! #123', | ||
1355 | 617 | 'size': 20, | ||
1356 | 618 | 'checksum': None, | ||
1357 | 619 | 'created_at': time2} | ||
1358 | 620 | |||
1359 | 621 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1360 | 622 | |||
1361 | 623 | req = webob.Request.blank('/images?sort_key=created_at&sort_dir=asc') | ||
1362 | 624 | res = req.get_response(self.api) | ||
1363 | 625 | self.assertEquals(res.status_int, 200) | ||
1364 | 626 | res_dict = json.loads(res.body) | ||
1365 | 627 | |||
1366 | 628 | images = res_dict['images'] | ||
1367 | 629 | self.assertEquals(len(images), 3) | ||
1368 | 630 | self.assertEquals(int(images[0]['id']), 2) | ||
1369 | 631 | self.assertEquals(int(images[1]['id']), 4) | ||
1370 | 632 | self.assertEquals(int(images[2]['id']), 3) | ||
1371 | 633 | |||
1372 | 634 | def test_get_index_sort_updated_at_desc(self): | ||
1373 | 635 | """ | ||
1374 | 636 | Tests that the /images registry API returns list of | ||
1375 | 637 | public images sorted by updated_at in descending order. | ||
1376 | 638 | """ | ||
1377 | 639 | now = datetime.datetime.utcnow() | ||
1378 | 640 | time1 = now + datetime.timedelta(seconds=5) | ||
1379 | 641 | time2 = now | ||
1380 | 642 | |||
1381 | 643 | extra_fixture = {'id': 3, | ||
1382 | 644 | 'status': 'active', | ||
1383 | 645 | 'is_public': True, | ||
1384 | 646 | 'disk_format': 'vhd', | ||
1385 | 647 | 'container_format': 'ovf', | ||
1386 | 648 | 'name': 'new name! #123', | ||
1387 | 649 | 'size': 19, | ||
1388 | 650 | 'checksum': None, | ||
1389 | 651 | 'created_at': None, | ||
1390 | 652 | 'created_at': time1} | ||
1391 | 653 | |||
1392 | 654 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1393 | 655 | |||
1394 | 656 | extra_fixture = {'id': 4, | ||
1395 | 657 | 'status': 'active', | ||
1396 | 658 | 'is_public': True, | ||
1397 | 659 | 'disk_format': 'vhd', | ||
1398 | 660 | 'container_format': 'ovf', | ||
1399 | 661 | 'name': 'new name! #123', | ||
1400 | 662 | 'size': 20, | ||
1401 | 663 | 'checksum': None, | ||
1402 | 664 | 'created_at': None, | ||
1403 | 665 | 'updated_at': time2} | ||
1404 | 666 | |||
1405 | 667 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1406 | 668 | |||
1407 | 669 | req = webob.Request.blank('/images?sort_key=updated_at&sort_dir=desc') | ||
1408 | 670 | res = req.get_response(self.api) | ||
1409 | 671 | self.assertEquals(res.status_int, 200) | ||
1410 | 672 | res_dict = json.loads(res.body) | ||
1411 | 673 | |||
1412 | 674 | images = res_dict['images'] | ||
1413 | 675 | self.assertEquals(len(images), 3) | ||
1414 | 676 | self.assertEquals(int(images[0]['id']), 3) | ||
1415 | 677 | self.assertEquals(int(images[1]['id']), 4) | ||
1416 | 678 | self.assertEquals(int(images[2]['id']), 2) | ||
1417 | 679 | |||
1418 | 288 | def test_get_details(self): | 680 | def test_get_details(self): |
1419 | 289 | """Tests that the /images/detail registry API returns | 681 | """Tests that the /images/detail registry API returns |
1420 | 290 | a mapping containing a list of detailed image information | 682 | a mapping containing a list of detailed image information |
1421 | @@ -668,6 +1060,45 @@ | |||
1422 | 668 | for image in images: | 1060 | for image in images: |
1423 | 669 | self.assertEqual('v a', image['properties']['prop_123']) | 1061 | self.assertEqual('v a', image['properties']['prop_123']) |
1424 | 670 | 1062 | ||
1425 | 1063 | def test_get_details_sort_name_asc(self): | ||
1426 | 1064 | """ | ||
1427 | 1065 | Tests that the /images/details registry API returns list of | ||
1428 | 1066 | public images sorted alphabetically by name in | ||
1429 | 1067 | ascending order. | ||
1430 | 1068 | """ | ||
1431 | 1069 | extra_fixture = {'id': 3, | ||
1432 | 1070 | 'status': 'active', | ||
1433 | 1071 | 'is_public': True, | ||
1434 | 1072 | 'disk_format': 'vhd', | ||
1435 | 1073 | 'container_format': 'ovf', | ||
1436 | 1074 | 'name': 'asdf', | ||
1437 | 1075 | 'size': 19, | ||
1438 | 1076 | 'checksum': None} | ||
1439 | 1077 | |||
1440 | 1078 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1441 | 1079 | |||
1442 | 1080 | extra_fixture = {'id': 4, | ||
1443 | 1081 | 'status': 'active', | ||
1444 | 1082 | 'is_public': True, | ||
1445 | 1083 | 'disk_format': 'vhd', | ||
1446 | 1084 | 'container_format': 'ovf', | ||
1447 | 1085 | 'name': 'xyz', | ||
1448 | 1086 | 'size': 20, | ||
1449 | 1087 | 'checksum': None} | ||
1450 | 1088 | |||
1451 | 1089 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1452 | 1090 | |||
1453 | 1091 | req = webob.Request.blank('/images/detail?sort_key=name&sort_dir=asc') | ||
1454 | 1092 | res = req.get_response(self.api) | ||
1455 | 1093 | self.assertEquals(res.status_int, 200) | ||
1456 | 1094 | res_dict = json.loads(res.body) | ||
1457 | 1095 | |||
1458 | 1096 | images = res_dict['images'] | ||
1459 | 1097 | self.assertEquals(len(images), 3) | ||
1460 | 1098 | self.assertEquals(int(images[0]['id']), 3) | ||
1461 | 1099 | self.assertEquals(int(images[1]['id']), 2) | ||
1462 | 1100 | self.assertEquals(int(images[2]['id']), 4) | ||
1463 | 1101 | |||
1464 | 671 | def test_create_image(self): | 1102 | def test_create_image(self): |
1465 | 672 | """Tests that the /images POST registry API creates the image""" | 1103 | """Tests that the /images POST registry API creates the image""" |
1466 | 673 | fixture = {'name': 'fake public image', | 1104 | fixture = {'name': 'fake public image', |
1467 | @@ -1042,6 +1473,45 @@ | |||
1468 | 1042 | "res.headerlist = %r" % res.headerlist) | 1473 | "res.headerlist = %r" % res.headerlist) |
1469 | 1043 | self.assertTrue('/images/3' in res.headers['location']) | 1474 | self.assertTrue('/images/3' in res.headers['location']) |
1470 | 1044 | 1475 | ||
1471 | 1476 | def test_get_index_sort_name_asc(self): | ||
1472 | 1477 | """ | ||
1473 | 1478 | Tests that the /images registry API returns list of | ||
1474 | 1479 | public images sorted alphabetically by name in | ||
1475 | 1480 | ascending order. | ||
1476 | 1481 | """ | ||
1477 | 1482 | extra_fixture = {'id': 3, | ||
1478 | 1483 | 'status': 'active', | ||
1479 | 1484 | 'is_public': True, | ||
1480 | 1485 | 'disk_format': 'vhd', | ||
1481 | 1486 | 'container_format': 'ovf', | ||
1482 | 1487 | 'name': 'asdf', | ||
1483 | 1488 | 'size': 19, | ||
1484 | 1489 | 'checksum': None} | ||
1485 | 1490 | |||
1486 | 1491 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1487 | 1492 | |||
1488 | 1493 | extra_fixture = {'id': 4, | ||
1489 | 1494 | 'status': 'active', | ||
1490 | 1495 | 'is_public': True, | ||
1491 | 1496 | 'disk_format': 'vhd', | ||
1492 | 1497 | 'container_format': 'ovf', | ||
1493 | 1498 | 'name': 'xyz', | ||
1494 | 1499 | 'size': 20, | ||
1495 | 1500 | 'checksum': None} | ||
1496 | 1501 | |||
1497 | 1502 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1498 | 1503 | |||
1499 | 1504 | req = webob.Request.blank('/images?sort_key=name&sort_dir=asc') | ||
1500 | 1505 | res = req.get_response(self.api) | ||
1501 | 1506 | self.assertEquals(res.status_int, 200) | ||
1502 | 1507 | res_dict = json.loads(res.body) | ||
1503 | 1508 | |||
1504 | 1509 | images = res_dict['images'] | ||
1505 | 1510 | self.assertEquals(len(images), 3) | ||
1506 | 1511 | self.assertEquals(int(images[0]['id']), 3) | ||
1507 | 1512 | self.assertEquals(int(images[1]['id']), 2) | ||
1508 | 1513 | self.assertEquals(int(images[2]['id']), 4) | ||
1509 | 1514 | |||
1510 | 1045 | def test_image_is_checksummed(self): | 1515 | def test_image_is_checksummed(self): |
1511 | 1046 | """Test that the image contents are checksummed properly""" | 1516 | """Test that the image contents are checksummed properly""" |
1512 | 1047 | fixture_headers = {'x-image-meta-store': 'file', | 1517 | fixture_headers = {'x-image-meta-store': 'file', |
1513 | 1048 | 1518 | ||
1514 | === modified file 'tests/unit/test_clients.py' | |||
1515 | --- tests/unit/test_clients.py 2011-05-31 19:32:17 +0000 | |||
1516 | +++ tests/unit/test_clients.py 2011-06-28 13:52:02 +0000 | |||
1517 | @@ -15,6 +15,7 @@ | |||
1518 | 15 | # License for the specific language governing permissions and limitations | 15 | # License for the specific language governing permissions and limitations |
1519 | 16 | # under the License. | 16 | # under the License. |
1520 | 17 | 17 | ||
1521 | 18 | import datetime | ||
1522 | 18 | import json | 19 | import json |
1523 | 19 | import os | 20 | import os |
1524 | 20 | import stubout | 21 | import stubout |
1525 | @@ -37,7 +38,7 @@ | |||
1526 | 37 | def test_bad_address(self): | 38 | def test_bad_address(self): |
1527 | 38 | """Test ClientConnectionError raised""" | 39 | """Test ClientConnectionError raised""" |
1528 | 39 | c = client.Client("127.999.1.1") | 40 | c = client.Client("127.999.1.1") |
1530 | 40 | self.assertRaises(client.ClientConnectionError, | 41 | self.assertRaises(exception.ClientConnectionError, |
1531 | 41 | c.get_image, | 42 | c.get_image, |
1532 | 42 | 1) | 43 | 1) |
1533 | 43 | 44 | ||
1534 | @@ -70,6 +71,298 @@ | |||
1535 | 70 | for k, v in fixture.items(): | 71 | for k, v in fixture.items(): |
1536 | 71 | self.assertEquals(v, images[0][k]) | 72 | self.assertEquals(v, images[0][k]) |
1537 | 72 | 73 | ||
1538 | 74 | def test_get_index_sort_id_desc(self): | ||
1539 | 75 | """ | ||
1540 | 76 | Tests that the /images registry API returns list of | ||
1541 | 77 | public images sorted by id in descending order. | ||
1542 | 78 | """ | ||
1543 | 79 | extra_fixture = {'id': 3, | ||
1544 | 80 | 'status': 'active', | ||
1545 | 81 | 'is_public': True, | ||
1546 | 82 | 'disk_format': 'vhd', | ||
1547 | 83 | 'container_format': 'ovf', | ||
1548 | 84 | 'name': 'asdf', | ||
1549 | 85 | 'size': 19, | ||
1550 | 86 | 'checksum': None} | ||
1551 | 87 | |||
1552 | 88 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1553 | 89 | |||
1554 | 90 | extra_fixture = {'id': 4, | ||
1555 | 91 | 'status': 'active', | ||
1556 | 92 | 'is_public': True, | ||
1557 | 93 | 'disk_format': 'vhd', | ||
1558 | 94 | 'container_format': 'ovf', | ||
1559 | 95 | 'name': 'xyz', | ||
1560 | 96 | 'size': 20, | ||
1561 | 97 | 'checksum': None} | ||
1562 | 98 | |||
1563 | 99 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1564 | 100 | |||
1565 | 101 | images = self.client.get_images(sort_key='id', sort_dir='desc') | ||
1566 | 102 | |||
1567 | 103 | self.assertEquals(len(images), 3) | ||
1568 | 104 | self.assertEquals(int(images[0]['id']), 4) | ||
1569 | 105 | self.assertEquals(int(images[1]['id']), 3) | ||
1570 | 106 | self.assertEquals(int(images[2]['id']), 2) | ||
1571 | 107 | |||
1572 | 108 | def test_get_index_sort_name_asc(self): | ||
1573 | 109 | """ | ||
1574 | 110 | Tests that the /images registry API returns list of | ||
1575 | 111 | public images sorted alphabetically by name in | ||
1576 | 112 | ascending order. | ||
1577 | 113 | """ | ||
1578 | 114 | extra_fixture = {'id': 3, | ||
1579 | 115 | 'status': 'active', | ||
1580 | 116 | 'is_public': True, | ||
1581 | 117 | 'disk_format': 'vhd', | ||
1582 | 118 | 'container_format': 'ovf', | ||
1583 | 119 | 'name': 'asdf', | ||
1584 | 120 | 'size': 19, | ||
1585 | 121 | 'checksum': None} | ||
1586 | 122 | |||
1587 | 123 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1588 | 124 | |||
1589 | 125 | extra_fixture = {'id': 4, | ||
1590 | 126 | 'status': 'active', | ||
1591 | 127 | 'is_public': True, | ||
1592 | 128 | 'disk_format': 'vhd', | ||
1593 | 129 | 'container_format': 'ovf', | ||
1594 | 130 | 'name': 'xyz', | ||
1595 | 131 | 'size': 20, | ||
1596 | 132 | 'checksum': None} | ||
1597 | 133 | |||
1598 | 134 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1599 | 135 | |||
1600 | 136 | images = self.client.get_images(sort_key='name', sort_dir='asc') | ||
1601 | 137 | |||
1602 | 138 | self.assertEquals(len(images), 3) | ||
1603 | 139 | self.assertEquals(int(images[0]['id']), 3) | ||
1604 | 140 | self.assertEquals(int(images[1]['id']), 2) | ||
1605 | 141 | self.assertEquals(int(images[2]['id']), 4) | ||
1606 | 142 | |||
1607 | 143 | def test_get_index_sort_status_desc(self): | ||
1608 | 144 | """ | ||
1609 | 145 | Tests that the /images registry API returns list of | ||
1610 | 146 | public images sorted alphabetically by status in | ||
1611 | 147 | descending order. | ||
1612 | 148 | """ | ||
1613 | 149 | extra_fixture = {'id': 3, | ||
1614 | 150 | 'status': 'killed', | ||
1615 | 151 | 'is_public': True, | ||
1616 | 152 | 'disk_format': 'vhd', | ||
1617 | 153 | 'container_format': 'ovf', | ||
1618 | 154 | 'name': 'asdf', | ||
1619 | 155 | 'size': 19, | ||
1620 | 156 | 'checksum': None} | ||
1621 | 157 | |||
1622 | 158 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1623 | 159 | |||
1624 | 160 | extra_fixture = {'id': 4, | ||
1625 | 161 | 'status': 'active', | ||
1626 | 162 | 'is_public': True, | ||
1627 | 163 | 'disk_format': 'vhd', | ||
1628 | 164 | 'container_format': 'ovf', | ||
1629 | 165 | 'name': 'xyz', | ||
1630 | 166 | 'size': 20, | ||
1631 | 167 | 'checksum': None} | ||
1632 | 168 | |||
1633 | 169 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1634 | 170 | |||
1635 | 171 | images = self.client.get_images(sort_key='status', sort_dir='desc') | ||
1636 | 172 | |||
1637 | 173 | self.assertEquals(len(images), 3) | ||
1638 | 174 | self.assertEquals(int(images[0]['id']), 3) | ||
1639 | 175 | self.assertEquals(int(images[1]['id']), 4) | ||
1640 | 176 | self.assertEquals(int(images[2]['id']), 2) | ||
1641 | 177 | |||
1642 | 178 | def test_get_index_sort_disk_format_asc(self): | ||
1643 | 179 | """ | ||
1644 | 180 | Tests that the /images registry API returns list of | ||
1645 | 181 | public images sorted alphabetically by disk_format in | ||
1646 | 182 | ascending order. | ||
1647 | 183 | """ | ||
1648 | 184 | extra_fixture = {'id': 3, | ||
1649 | 185 | 'status': 'active', | ||
1650 | 186 | 'is_public': True, | ||
1651 | 187 | 'disk_format': 'ami', | ||
1652 | 188 | 'container_format': 'ami', | ||
1653 | 189 | 'name': 'asdf', | ||
1654 | 190 | 'size': 19, | ||
1655 | 191 | 'checksum': None} | ||
1656 | 192 | |||
1657 | 193 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1658 | 194 | |||
1659 | 195 | extra_fixture = {'id': 4, | ||
1660 | 196 | 'status': 'active', | ||
1661 | 197 | 'is_public': True, | ||
1662 | 198 | 'disk_format': 'vdi', | ||
1663 | 199 | 'container_format': 'ovf', | ||
1664 | 200 | 'name': 'xyz', | ||
1665 | 201 | 'size': 20, | ||
1666 | 202 | 'checksum': None} | ||
1667 | 203 | |||
1668 | 204 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1669 | 205 | |||
1670 | 206 | images = self.client.get_images(sort_key='disk_format', | ||
1671 | 207 | sort_dir='asc') | ||
1672 | 208 | |||
1673 | 209 | self.assertEquals(len(images), 3) | ||
1674 | 210 | self.assertEquals(int(images[0]['id']), 3) | ||
1675 | 211 | self.assertEquals(int(images[1]['id']), 4) | ||
1676 | 212 | self.assertEquals(int(images[2]['id']), 2) | ||
1677 | 213 | |||
1678 | 214 | def test_get_index_sort_container_format_desc(self): | ||
1679 | 215 | """ | ||
1680 | 216 | Tests that the /images registry API returns list of | ||
1681 | 217 | public images sorted alphabetically by container_format in | ||
1682 | 218 | descending order. | ||
1683 | 219 | """ | ||
1684 | 220 | extra_fixture = {'id': 3, | ||
1685 | 221 | 'status': 'active', | ||
1686 | 222 | 'is_public': True, | ||
1687 | 223 | 'disk_format': 'ami', | ||
1688 | 224 | 'container_format': 'ami', | ||
1689 | 225 | 'name': 'asdf', | ||
1690 | 226 | 'size': 19, | ||
1691 | 227 | 'checksum': None} | ||
1692 | 228 | |||
1693 | 229 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1694 | 230 | |||
1695 | 231 | extra_fixture = {'id': 4, | ||
1696 | 232 | 'status': 'active', | ||
1697 | 233 | 'is_public': True, | ||
1698 | 234 | 'disk_format': 'iso', | ||
1699 | 235 | 'container_format': 'bare', | ||
1700 | 236 | 'name': 'xyz', | ||
1701 | 237 | 'size': 20, | ||
1702 | 238 | 'checksum': None} | ||
1703 | 239 | |||
1704 | 240 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1705 | 241 | |||
1706 | 242 | images = self.client.get_images(sort_key='container_format', | ||
1707 | 243 | sort_dir='desc') | ||
1708 | 244 | |||
1709 | 245 | self.assertEquals(len(images), 3) | ||
1710 | 246 | self.assertEquals(int(images[0]['id']), 2) | ||
1711 | 247 | self.assertEquals(int(images[1]['id']), 4) | ||
1712 | 248 | self.assertEquals(int(images[2]['id']), 3) | ||
1713 | 249 | |||
1714 | 250 | def test_get_index_sort_size_asc(self): | ||
1715 | 251 | """ | ||
1716 | 252 | Tests that the /images registry API returns list of | ||
1717 | 253 | public images sorted by size in ascending order. | ||
1718 | 254 | """ | ||
1719 | 255 | extra_fixture = {'id': 3, | ||
1720 | 256 | 'status': 'active', | ||
1721 | 257 | 'is_public': True, | ||
1722 | 258 | 'disk_format': 'ami', | ||
1723 | 259 | 'container_format': 'ami', | ||
1724 | 260 | 'name': 'asdf', | ||
1725 | 261 | 'size': 100, | ||
1726 | 262 | 'checksum': None} | ||
1727 | 263 | |||
1728 | 264 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1729 | 265 | |||
1730 | 266 | extra_fixture = {'id': 4, | ||
1731 | 267 | 'status': 'active', | ||
1732 | 268 | 'is_public': True, | ||
1733 | 269 | 'disk_format': 'iso', | ||
1734 | 270 | 'container_format': 'bare', | ||
1735 | 271 | 'name': 'xyz', | ||
1736 | 272 | 'size': 2, | ||
1737 | 273 | 'checksum': None} | ||
1738 | 274 | |||
1739 | 275 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1740 | 276 | |||
1741 | 277 | images = self.client.get_images(sort_key='size', sort_dir='asc') | ||
1742 | 278 | |||
1743 | 279 | self.assertEquals(len(images), 3) | ||
1744 | 280 | self.assertEquals(int(images[0]['id']), 4) | ||
1745 | 281 | self.assertEquals(int(images[1]['id']), 2) | ||
1746 | 282 | self.assertEquals(int(images[2]['id']), 3) | ||
1747 | 283 | |||
1748 | 284 | def test_get_index_sort_created_at_asc(self): | ||
1749 | 285 | """ | ||
1750 | 286 | Tests that the /images registry API returns list of | ||
1751 | 287 | public images sorted by created_at in ascending order. | ||
1752 | 288 | """ | ||
1753 | 289 | now = datetime.datetime.utcnow() | ||
1754 | 290 | time1 = now + datetime.timedelta(seconds=5) | ||
1755 | 291 | time2 = now | ||
1756 | 292 | |||
1757 | 293 | extra_fixture = {'id': 3, | ||
1758 | 294 | 'status': 'active', | ||
1759 | 295 | 'is_public': True, | ||
1760 | 296 | 'disk_format': 'vhd', | ||
1761 | 297 | 'container_format': 'ovf', | ||
1762 | 298 | 'name': 'new name! #123', | ||
1763 | 299 | 'size': 19, | ||
1764 | 300 | 'checksum': None, | ||
1765 | 301 | 'created_at': time1} | ||
1766 | 302 | |||
1767 | 303 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1768 | 304 | |||
1769 | 305 | extra_fixture = {'id': 4, | ||
1770 | 306 | 'status': 'active', | ||
1771 | 307 | 'is_public': True, | ||
1772 | 308 | 'disk_format': 'vhd', | ||
1773 | 309 | 'container_format': 'ovf', | ||
1774 | 310 | 'name': 'new name! #123', | ||
1775 | 311 | 'size': 20, | ||
1776 | 312 | 'checksum': None, | ||
1777 | 313 | 'created_at': time2} | ||
1778 | 314 | |||
1779 | 315 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1780 | 316 | |||
1781 | 317 | images = self.client.get_images(sort_key='created_at', sort_dir='asc') | ||
1782 | 318 | |||
1783 | 319 | self.assertEquals(len(images), 3) | ||
1784 | 320 | self.assertEquals(int(images[0]['id']), 2) | ||
1785 | 321 | self.assertEquals(int(images[1]['id']), 4) | ||
1786 | 322 | self.assertEquals(int(images[2]['id']), 3) | ||
1787 | 323 | |||
1788 | 324 | def test_get_index_sort_updated_at_desc(self): | ||
1789 | 325 | """ | ||
1790 | 326 | Tests that the /images registry API returns list of | ||
1791 | 327 | public images sorted by updated_at in descending order. | ||
1792 | 328 | """ | ||
1793 | 329 | now = datetime.datetime.utcnow() | ||
1794 | 330 | time1 = now + datetime.timedelta(seconds=5) | ||
1795 | 331 | time2 = now | ||
1796 | 332 | |||
1797 | 333 | extra_fixture = {'id': 3, | ||
1798 | 334 | 'status': 'active', | ||
1799 | 335 | 'is_public': True, | ||
1800 | 336 | 'disk_format': 'vhd', | ||
1801 | 337 | 'container_format': 'ovf', | ||
1802 | 338 | 'name': 'new name! #123', | ||
1803 | 339 | 'size': 19, | ||
1804 | 340 | 'checksum': None, | ||
1805 | 341 | 'created_at': None, | ||
1806 | 342 | 'created_at': time1} | ||
1807 | 343 | |||
1808 | 344 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1809 | 345 | |||
1810 | 346 | extra_fixture = {'id': 4, | ||
1811 | 347 | 'status': 'active', | ||
1812 | 348 | 'is_public': True, | ||
1813 | 349 | 'disk_format': 'vhd', | ||
1814 | 350 | 'container_format': 'ovf', | ||
1815 | 351 | 'name': 'new name! #123', | ||
1816 | 352 | 'size': 20, | ||
1817 | 353 | 'checksum': None, | ||
1818 | 354 | 'created_at': None, | ||
1819 | 355 | 'updated_at': time2} | ||
1820 | 356 | |||
1821 | 357 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1822 | 358 | |||
1823 | 359 | images = self.client.get_images(sort_key='updated_at', sort_dir='desc') | ||
1824 | 360 | |||
1825 | 361 | self.assertEquals(len(images), 3) | ||
1826 | 362 | self.assertEquals(int(images[0]['id']), 3) | ||
1827 | 363 | self.assertEquals(int(images[1]['id']), 4) | ||
1828 | 364 | self.assertEquals(int(images[2]['id']), 2) | ||
1829 | 365 | |||
1830 | 73 | def test_get_image_index_marker(self): | 366 | def test_get_image_index_marker(self): |
1831 | 74 | """Test correct set of images returned with marker param.""" | 367 | """Test correct set of images returned with marker param.""" |
1832 | 75 | extra_fixture = {'id': 3, | 368 | extra_fixture = {'id': 3, |
1833 | @@ -170,7 +463,7 @@ | |||
1834 | 170 | 463 | ||
1835 | 171 | glance.registry.db.api.image_create(None, extra_fixture) | 464 | glance.registry.db.api.image_create(None, extra_fixture) |
1836 | 172 | 465 | ||
1838 | 173 | images = self.client.get_images({'name': 'new name! #123'}) | 466 | images = self.client.get_images(filters={'name': 'new name! #123'}) |
1839 | 174 | self.assertEquals(len(images), 1) | 467 | self.assertEquals(len(images), 1) |
1840 | 175 | 468 | ||
1841 | 176 | for image in images: | 469 | for image in images: |
1842 | @@ -236,7 +529,8 @@ | |||
1843 | 236 | 529 | ||
1844 | 237 | glance.registry.db.api.image_create(None, extra_fixture) | 530 | glance.registry.db.api.image_create(None, extra_fixture) |
1845 | 238 | 531 | ||
1847 | 239 | images = self.client.get_images_detailed({'name': 'new name! #123'}) | 532 | filters = {'name': 'new name! #123'} |
1848 | 533 | images = self.client.get_images_detailed(filters=filters) | ||
1849 | 240 | self.assertEquals(len(images), 1) | 534 | self.assertEquals(len(images), 1) |
1850 | 241 | 535 | ||
1851 | 242 | for image in images: | 536 | for image in images: |
1852 | @@ -255,7 +549,7 @@ | |||
1853 | 255 | 549 | ||
1854 | 256 | glance.registry.db.api.image_create(None, extra_fixture) | 550 | glance.registry.db.api.image_create(None, extra_fixture) |
1855 | 257 | 551 | ||
1857 | 258 | images = self.client.get_images_detailed({'status': 'saving'}) | 552 | images = self.client.get_images_detailed(filters={'status': 'saving'}) |
1858 | 259 | self.assertEquals(len(images), 1) | 553 | self.assertEquals(len(images), 1) |
1859 | 260 | 554 | ||
1860 | 261 | for image in images: | 555 | for image in images: |
1861 | @@ -274,7 +568,8 @@ | |||
1862 | 274 | 568 | ||
1863 | 275 | glance.registry.db.api.image_create(None, extra_fixture) | 569 | glance.registry.db.api.image_create(None, extra_fixture) |
1864 | 276 | 570 | ||
1866 | 277 | images = self.client.get_images_detailed({'container_format': 'ovf'}) | 571 | filters = {'container_format': 'ovf'} |
1867 | 572 | images = self.client.get_images_detailed(filters=filters) | ||
1868 | 278 | self.assertEquals(len(images), 2) | 573 | self.assertEquals(len(images), 2) |
1869 | 279 | 574 | ||
1870 | 280 | for image in images: | 575 | for image in images: |
1871 | @@ -293,7 +588,8 @@ | |||
1872 | 293 | 588 | ||
1873 | 294 | glance.registry.db.api.image_create(None, extra_fixture) | 589 | glance.registry.db.api.image_create(None, extra_fixture) |
1874 | 295 | 590 | ||
1876 | 296 | images = self.client.get_images_detailed({'disk_format': 'vhd'}) | 591 | filters = {'disk_format': 'vhd'} |
1877 | 592 | images = self.client.get_images_detailed(filters=filters) | ||
1878 | 297 | self.assertEquals(len(images), 2) | 593 | self.assertEquals(len(images), 2) |
1879 | 298 | 594 | ||
1880 | 299 | for image in images: | 595 | for image in images: |
1881 | @@ -312,7 +608,7 @@ | |||
1882 | 312 | 608 | ||
1883 | 313 | glance.registry.db.api.image_create(None, extra_fixture) | 609 | glance.registry.db.api.image_create(None, extra_fixture) |
1884 | 314 | 610 | ||
1886 | 315 | images = self.client.get_images_detailed({'size_max': 20}) | 611 | images = self.client.get_images_detailed(filters={'size_max': 20}) |
1887 | 316 | self.assertEquals(len(images), 1) | 612 | self.assertEquals(len(images), 1) |
1888 | 317 | 613 | ||
1889 | 318 | for image in images: | 614 | for image in images: |
1890 | @@ -331,7 +627,7 @@ | |||
1891 | 331 | 627 | ||
1892 | 332 | glance.registry.db.api.image_create(None, extra_fixture) | 628 | glance.registry.db.api.image_create(None, extra_fixture) |
1893 | 333 | 629 | ||
1895 | 334 | images = self.client.get_images_detailed({'size_min': 20}) | 630 | images = self.client.get_images_detailed(filters={'size_min': 20}) |
1896 | 335 | self.assertEquals(len(images), 1) | 631 | self.assertEquals(len(images), 1) |
1897 | 336 | 632 | ||
1898 | 337 | for image in images: | 633 | for image in images: |
1899 | @@ -351,12 +647,49 @@ | |||
1900 | 351 | 647 | ||
1901 | 352 | glance.registry.db.api.image_create(None, extra_fixture) | 648 | glance.registry.db.api.image_create(None, extra_fixture) |
1902 | 353 | 649 | ||
1904 | 354 | images = self.client.get_images_detailed({'property-p a': 'v a'}) | 650 | filters = {'property-p a': 'v a'} |
1905 | 651 | images = self.client.get_images_detailed(filters=filters) | ||
1906 | 355 | self.assertEquals(len(images), 1) | 652 | self.assertEquals(len(images), 1) |
1907 | 356 | 653 | ||
1908 | 357 | for image in images: | 654 | for image in images: |
1909 | 358 | self.assertEquals('v a', image['properties']['p a']) | 655 | self.assertEquals('v a', image['properties']['p a']) |
1910 | 359 | 656 | ||
1911 | 657 | def test_get_image_details_sort_disk_format_asc(self): | ||
1912 | 658 | """ | ||
1913 | 659 | Tests that a detailed call returns list of | ||
1914 | 660 | public images sorted alphabetically by disk_format in | ||
1915 | 661 | ascending order. | ||
1916 | 662 | """ | ||
1917 | 663 | extra_fixture = {'id': 3, | ||
1918 | 664 | 'status': 'active', | ||
1919 | 665 | 'is_public': True, | ||
1920 | 666 | 'disk_format': 'ami', | ||
1921 | 667 | 'container_format': 'ami', | ||
1922 | 668 | 'name': 'asdf', | ||
1923 | 669 | 'size': 19, | ||
1924 | 670 | 'checksum': None} | ||
1925 | 671 | |||
1926 | 672 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1927 | 673 | |||
1928 | 674 | extra_fixture = {'id': 4, | ||
1929 | 675 | 'status': 'active', | ||
1930 | 676 | 'is_public': True, | ||
1931 | 677 | 'disk_format': 'vdi', | ||
1932 | 678 | 'container_format': 'ovf', | ||
1933 | 679 | 'name': 'xyz', | ||
1934 | 680 | 'size': 20, | ||
1935 | 681 | 'checksum': None} | ||
1936 | 682 | |||
1937 | 683 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1938 | 684 | |||
1939 | 685 | images = self.client.get_images_detailed(sort_key='disk_format', | ||
1940 | 686 | sort_dir='asc') | ||
1941 | 687 | |||
1942 | 688 | self.assertEquals(len(images), 3) | ||
1943 | 689 | self.assertEquals(int(images[0]['id']), 3) | ||
1944 | 690 | self.assertEquals(int(images[1]['id']), 4) | ||
1945 | 691 | self.assertEquals(int(images[2]['id']), 2) | ||
1946 | 692 | |||
1947 | 360 | def test_get_image(self): | 693 | def test_get_image(self): |
1948 | 361 | """Tests that the detailed info about an image returned""" | 694 | """Tests that the detailed info about an image returned""" |
1949 | 362 | fixture = {'id': 1, | 695 | fixture = {'id': 1, |
1950 | @@ -562,6 +895,42 @@ | |||
1951 | 562 | self.client.get_image, | 895 | self.client.get_image, |
1952 | 563 | 3) | 896 | 3) |
1953 | 564 | 897 | ||
1954 | 898 | def test_get_image_index_sort_container_format_desc(self): | ||
1955 | 899 | """ | ||
1956 | 900 | Tests that the client returns list of public images | ||
1957 | 901 | sorted alphabetically by container_format in | ||
1958 | 902 | descending order. | ||
1959 | 903 | """ | ||
1960 | 904 | extra_fixture = {'id': 3, | ||
1961 | 905 | 'status': 'active', | ||
1962 | 906 | 'is_public': True, | ||
1963 | 907 | 'disk_format': 'ami', | ||
1964 | 908 | 'container_format': 'ami', | ||
1965 | 909 | 'name': 'asdf', | ||
1966 | 910 | 'size': 19, | ||
1967 | 911 | 'checksum': None} | ||
1968 | 912 | |||
1969 | 913 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1970 | 914 | |||
1971 | 915 | extra_fixture = {'id': 4, | ||
1972 | 916 | 'status': 'active', | ||
1973 | 917 | 'is_public': True, | ||
1974 | 918 | 'disk_format': 'iso', | ||
1975 | 919 | 'container_format': 'bare', | ||
1976 | 920 | 'name': 'xyz', | ||
1977 | 921 | 'size': 20, | ||
1978 | 922 | 'checksum': None} | ||
1979 | 923 | |||
1980 | 924 | glance.registry.db.api.image_create(None, extra_fixture) | ||
1981 | 925 | |||
1982 | 926 | images = self.client.get_images(sort_key='container_format', | ||
1983 | 927 | sort_dir='desc') | ||
1984 | 928 | |||
1985 | 929 | self.assertEquals(len(images), 3) | ||
1986 | 930 | self.assertEquals(int(images[0]['id']), 2) | ||
1987 | 931 | self.assertEquals(int(images[1]['id']), 4) | ||
1988 | 932 | self.assertEquals(int(images[2]['id']), 3) | ||
1989 | 933 | |||
1990 | 565 | def test_get_image_index(self): | 934 | def test_get_image_index(self): |
1991 | 566 | """Test correct set of public image returned""" | 935 | """Test correct set of public image returned""" |
1992 | 567 | fixture = {'id': 2, | 936 | fixture = {'id': 2, |
1993 | @@ -671,7 +1040,8 @@ | |||
1994 | 671 | 1040 | ||
1995 | 672 | glance.registry.db.api.image_create(None, extra_fixture) | 1041 | glance.registry.db.api.image_create(None, extra_fixture) |
1996 | 673 | 1042 | ||
1998 | 674 | images = self.client.get_images({'name': 'new name! #123'}) | 1043 | filters = {'name': 'new name! #123'} |
1999 | 1044 | images = self.client.get_images(filters=filters) | ||
2000 | 675 | 1045 | ||
2001 | 676 | self.assertEquals(len(images), 1) | 1046 | self.assertEquals(len(images), 1) |
2002 | 677 | self.assertEquals('new name! #123', images[0]['name']) | 1047 | self.assertEquals('new name! #123', images[0]['name']) |
2003 | @@ -690,7 +1060,8 @@ | |||
2004 | 690 | 1060 | ||
2005 | 691 | glance.registry.db.api.image_create(None, extra_fixture) | 1061 | glance.registry.db.api.image_create(None, extra_fixture) |
2006 | 692 | 1062 | ||
2008 | 693 | images = self.client.get_images({'property-p a': 'v a'}) | 1063 | filters = {'property-p a': 'v a'} |
2009 | 1064 | images = self.client.get_images(filters=filters) | ||
2010 | 694 | 1065 | ||
2011 | 695 | self.assertEquals(len(images), 1) | 1066 | self.assertEquals(len(images), 1) |
2012 | 696 | self.assertEquals(3, images[0]['id']) | 1067 | self.assertEquals(3, images[0]['id']) |
2013 | @@ -765,7 +1136,8 @@ | |||
2014 | 765 | 1136 | ||
2015 | 766 | glance.registry.db.api.image_create(None, extra_fixture) | 1137 | glance.registry.db.api.image_create(None, extra_fixture) |
2016 | 767 | 1138 | ||
2018 | 768 | images = self.client.get_images_detailed({'name': 'new name! #123'}) | 1139 | filters = {'name': 'new name! #123'} |
2019 | 1140 | images = self.client.get_images_detailed(filters=filters) | ||
2020 | 769 | self.assertEquals(len(images), 1) | 1141 | self.assertEquals(len(images), 1) |
2021 | 770 | 1142 | ||
2022 | 771 | for image in images: | 1143 | for image in images: |
2023 | @@ -785,7 +1157,8 @@ | |||
2024 | 785 | 1157 | ||
2025 | 786 | glance.registry.db.api.image_create(None, extra_fixture) | 1158 | glance.registry.db.api.image_create(None, extra_fixture) |
2026 | 787 | 1159 | ||
2028 | 788 | images = self.client.get_images_detailed({'property-p a': 'v a'}) | 1160 | filters = {'property-p a': 'v a'} |
2029 | 1161 | images = self.client.get_images_detailed(filters=filters) | ||
2030 | 789 | self.assertEquals(len(images), 1) | 1162 | self.assertEquals(len(images), 1) |
2031 | 790 | 1163 | ||
2032 | 791 | for image in images: | 1164 | for image in images: |
Hi Brian!
Nice work, yet again!
Small suggestions:
For the SUPPORTED_PARAMS, and SUPPORTED_ GET_PARAMS (in glance.client), could you make a glance. api.v1. images module-level function that returns these supported parameters? Either that, or make the constant at that module-level and import them into glance.client. That will ensure that future modifications to that constant do not have to make the modifications in two places. You did this for the SUPPORTED_SORT_KEYS constant in the registry, so might as well do it for SUPPORTED_PARAMS, too. :)
278 + _sort_dir = sort_dir or 'desc'
279 + sort_dir_func = {
280 + 'asc': asc,
281 + 'desc': desc,
282 + }[_sort_dir]
Instead of having the sort_dir param default to None, how about having it default to 'desc', in which case, the above can be simplified:
sort_dir_func = {
'asc': asc,
'desc': desc
}[sort_dir]
Same can be said for this:
284 + _sort_key = sort_key or 'created_at' models. Image, _sort_key)
285 + sort_key_attr = getattr(
Just make the sort_key parameter default to 'created_at', and shorten to:
sort_key_attr = getattr( models. Image, sort_key)
For this repeated block of code:
174 + params = kwargs. get('filters' , {}) update( self._extract_ get_params( kwargs) )
175 + params.
Go ahead and put the first line in the _extract_ get_params( ) method and have _extract_ get_params( ) return the entire parameter dictionary. DRY...
Cheers!
jay