Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import copy |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 17 | import functools |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 18 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 19 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 20 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 21 | import six |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 22 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 23 | |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 24 | from tempest.lib.common import rest_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 25 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 26 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 27 | LOG = logging.getLogger(__name__) |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 28 | CHUNKSIZE = 1024 * 64 # 64kB |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 29 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 30 | |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 31 | class ImagesClient(rest_client.RestClient): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 32 | api_version = "v1" |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 33 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 34 | def _image_meta_to_headers(self, fields): |
| 35 | headers = {} |
| 36 | fields_copy = copy.deepcopy(fields) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 37 | copy_from = fields_copy.pop('copy_from', None) |
| 38 | if copy_from is not None: |
| 39 | headers['x-glance-api-copy-from'] = copy_from |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 40 | for key, value in six.iteritems(fields_copy.pop('properties', {})): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 41 | headers['x-image-meta-property-%s' % key] = str(value) |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 42 | for key, value in six.iteritems(fields_copy.pop('api', {})): |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 43 | headers['x-glance-api-property-%s' % key] = str(value) |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 44 | for key, value in six.iteritems(fields_copy): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 45 | headers['x-image-meta-%s' % key] = str(value) |
| 46 | return headers |
| 47 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 48 | def _create_with_data(self, headers, data): |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 49 | # We are going to do chunked transfert, so split the input data |
| 50 | # info fixed-sized chunks. |
| 51 | headers['Content-Type'] = 'application/octet-stream' |
| 52 | data = iter(functools.partial(data.read, CHUNKSIZE), b'') |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 53 | resp, body = self.request('POST', 'images', |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 54 | headers=headers, body=data, chunked=True) |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 55 | self._error_checker('POST', 'images', headers, data, resp, |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 56 | body) |
| 57 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 58 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 59 | |
| 60 | def _update_with_data(self, image_id, headers, data): |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 61 | # We are going to do chunked transfert, so split the input data |
| 62 | # info fixed-sized chunks. |
| 63 | headers['Content-Type'] = 'application/octet-stream' |
| 64 | data = iter(functools.partial(data.read, CHUNKSIZE), b'') |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 65 | url = 'images/%s' % image_id |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 66 | resp, body = self.request('PUT', url, headers=headers, |
| 67 | body=data, chunked=True) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 68 | self._error_checker('PUT', url, headers, data, |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 69 | resp, body) |
| 70 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 71 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 72 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 73 | @property |
| 74 | def http(self): |
| 75 | if self._http is None: |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 76 | self._http = self._get_http() |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 77 | return self._http |
| 78 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 79 | def create_image(self, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 80 | headers = {} |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 81 | data = kwargs.pop('data', None) |
| 82 | headers.update(self._image_meta_to_headers(kwargs)) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 83 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 84 | if data is not None: |
| 85 | return self._create_with_data(headers, data) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 86 | |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 87 | resp, body = self.post('images', None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 88 | self.expected_success(201, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 89 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 90 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 91 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 92 | def update_image(self, image_id, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 93 | headers = {} |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 94 | data = kwargs.pop('data', None) |
| 95 | headers.update(self._image_meta_to_headers(kwargs)) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 96 | |
| 97 | if data is not None: |
| 98 | return self._update_with_data(image_id, headers, data) |
| 99 | |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 100 | url = 'images/%s' % image_id |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 101 | resp, body = self.put(url, None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 102 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 103 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 104 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 105 | |
| 106 | def delete_image(self, image_id): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 107 | url = 'images/%s' % image_id |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 108 | resp, body = self.delete(url) |
| 109 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 110 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 111 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 112 | def list_images(self, detail=False, **kwargs): |
| 113 | """Return a list of all images filtered by input parameters. |
| 114 | |
| 115 | Available params: see http://developer.openstack.org/ |
| 116 | api-ref-image-v1.html#listImage-v1 |
| 117 | |
| 118 | Most parameters except the following are passed to the API without |
| 119 | any changes. |
| 120 | :param changes_since: The name is changed to changes-since |
| 121 | """ |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 122 | url = 'images' |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 123 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 124 | if detail: |
| 125 | url += '/detail' |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 126 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 127 | properties = kwargs.pop('properties', {}) |
| 128 | for key, value in six.iteritems(properties): |
| 129 | kwargs['property-%s' % key] = value |
ivan-zhu | ccc8946 | 2013-10-31 16:53:12 +0800 | [diff] [blame] | 130 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 131 | if kwargs.get('changes_since'): |
| 132 | kwargs['changes-since'] = kwargs.pop('changes_since') |
ivan-zhu | d1bbe5d | 2013-12-29 18:32:46 +0800 | [diff] [blame] | 133 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 134 | if len(kwargs) > 0: |
| 135 | url += '?%s' % urllib.urlencode(kwargs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 136 | |
| 137 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 138 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 139 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 140 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 141 | |
Yaroslav Lobankov | 9d28a0f | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 142 | def check_image(self, image_id): |
| 143 | """Check image metadata.""" |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 144 | url = 'images/%s' % image_id |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame^] | 145 | resp, body = self.head(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 146 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 147 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 148 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 149 | def show_image(self, image_id): |
Yaroslav Lobankov | 9d28a0f | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 150 | """Get image details plus the image itself.""" |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 151 | url = 'images/%s' % image_id |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 152 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 153 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 154 | return rest_client.ResponseBodyData(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 155 | |
| 156 | def is_resource_deleted(self, id): |
| 157 | try: |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame^] | 158 | resp = self.check_image(id) |
| 159 | if resp.response["x-image-meta-status"] == 'deleted': |
Joshua White | 95dcef2 | 2016-04-11 06:17:42 -0700 | [diff] [blame] | 160 | return True |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 161 | except lib_exc.NotFound: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 162 | return True |
| 163 | return False |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 164 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 165 | @property |
| 166 | def resource_type(self): |
| 167 | """Returns the primary type of resource this client works with.""" |
| 168 | return 'image_meta' |