|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Image v1 API Library""" |
| 15 | + |
| 16 | +from openstackclient.api import api |
| 17 | + |
| 18 | + |
| 19 | +class APIv1(api.BaseAPI): |
| 20 | + """Image v1 API""" |
| 21 | + |
| 22 | + def __init__(self, endpoint=None, **kwargs): |
| 23 | + super(APIv1, self).__init__(endpoint=endpoint, **kwargs) |
| 24 | + |
| 25 | + # Hack this until discovery is up |
| 26 | + self.endpoint = '/'.join([self.endpoint.rstrip('/'), 'v1']) |
| 27 | + |
| 28 | + def image_list( |
| 29 | + self, |
| 30 | + detailed=False, |
| 31 | + public=False, |
| 32 | + private=False, |
| 33 | + **filter |
| 34 | + ): |
| 35 | + """Get available images |
| 36 | +
|
| 37 | + :param detailed: |
| 38 | + Retrieve detailed response from server if True |
| 39 | + :param public: |
| 40 | + Return public images if True |
| 41 | + :param private: |
| 42 | + Return private images if True |
| 43 | +
|
| 44 | + If public and private are both True or both False then all images are |
| 45 | + returned. Both arguments False is equivalent to no filter and all |
| 46 | + images are returned. Both arguments True is a filter that includes |
| 47 | + both public and private images which is the same set as all images. |
| 48 | +
|
| 49 | + http://docs.openstack.org/api/openstack-image-service/1.1/content/requesting-a-list-of-public-vm-images.html |
| 50 | + http://docs.openstack.org/api/openstack-image-service/1.1/content/requesting-detailed-metadata-on-public-vm-images.html |
| 51 | + http://docs.openstack.org/api/openstack-image-service/1.1/content/filtering-images-returned-via-get-images-and-get-imagesdetail.html |
| 52 | +
|
| 53 | + TODO(dtroyer): Implement filtering |
| 54 | + """ |
| 55 | + |
| 56 | + url = "/images" |
| 57 | + if detailed or public or private: |
| 58 | + # Because we can't all use /details |
| 59 | + url += "/detail" |
| 60 | + |
| 61 | + image_list = self.list(url, **filter)['images'] |
| 62 | + |
| 63 | + if public != private: |
| 64 | + # One is True and one is False, so public represents the filter |
| 65 | + # state in either case |
| 66 | + image_list = [i for i in image_list if i['is_public'] == public] |
| 67 | + |
| 68 | + return image_list |
0 commit comments