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 | |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 16 | import functools |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 17 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 19 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 20 | |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 23 | |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 24 | CHUNKSIZE = 1024 * 64 # 64kB |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 25 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 26 | |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 27 | class ImagesClient(rest_client.RestClient): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 28 | api_version = "v1" |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 29 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 30 | def _create_with_data(self, headers, data): |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 31 | # We are going to do chunked transfert, so split the input data |
| 32 | # info fixed-sized chunks. |
| 33 | headers['Content-Type'] = 'application/octet-stream' |
| 34 | data = iter(functools.partial(data.read, CHUNKSIZE), b'') |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 35 | resp, body = self.request('POST', 'images', |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 36 | headers=headers, body=data, chunked=True) |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 37 | self._error_checker('POST', 'images', headers, data, resp, |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 38 | body) |
| 39 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 40 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 41 | |
| 42 | def _update_with_data(self, image_id, headers, data): |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 43 | # We are going to do chunked transfert, so split the input data |
| 44 | # info fixed-sized chunks. |
| 45 | headers['Content-Type'] = 'application/octet-stream' |
| 46 | data = iter(functools.partial(data.read, CHUNKSIZE), b'') |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 47 | url = 'images/%s' % image_id |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 48 | resp, body = self.request('PUT', url, headers=headers, |
| 49 | body=data, chunked=True) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 50 | self._error_checker('PUT', url, headers, data, |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 51 | resp, body) |
| 52 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 53 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 54 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 55 | @property |
| 56 | def http(self): |
| 57 | if self._http is None: |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 58 | self._http = self._get_http() |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 59 | return self._http |
| 60 | |
Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 61 | def create_image(self, data=None, headers=None): |
Ken'ichi Ohmichi | d901638 | 2016-06-13 15:57:42 -0700 | [diff] [blame] | 62 | """Create an image. |
| 63 | |
zhufl | 3a36f05 | 2016-09-07 11:08:19 +0800 | [diff] [blame^] | 64 | For a full list of available parameters, please refer to the official |
| 65 | API reference: |
| 66 | http://developer.openstack.org/api-ref-image-v1.html#createImage-v1 |
Ken'ichi Ohmichi | d901638 | 2016-06-13 15:57:42 -0700 | [diff] [blame] | 67 | """ |
Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 68 | if headers is None: |
| 69 | headers = {} |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 70 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 71 | if data is not None: |
| 72 | return self._create_with_data(headers, data) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 73 | |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 74 | resp, body = self.post('images', None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 75 | self.expected_success(201, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 76 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 77 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 78 | |
Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 79 | def update_image(self, image_id, data=None, headers=None): |
Ken'ichi Ohmichi | d901638 | 2016-06-13 15:57:42 -0700 | [diff] [blame] | 80 | """Update an image. |
| 81 | |
zhufl | 3a36f05 | 2016-09-07 11:08:19 +0800 | [diff] [blame^] | 82 | For a full list of available parameters, please refer to the official |
| 83 | API reference: |
| 84 | http://developer.openstack.org/api-ref-image-v1.html#updateImage-v1 |
Ken'ichi Ohmichi | d901638 | 2016-06-13 15:57:42 -0700 | [diff] [blame] | 85 | """ |
Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 86 | if headers is None: |
| 87 | headers = {} |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 88 | |
| 89 | if data is not None: |
| 90 | return self._update_with_data(image_id, headers, data) |
| 91 | |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 92 | url = 'images/%s' % image_id |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 93 | resp, body = self.put(url, None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 94 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 95 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 96 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 97 | |
| 98 | def delete_image(self, image_id): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 99 | url = 'images/%s' % image_id |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 100 | resp, body = self.delete(url) |
| 101 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 102 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 103 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 104 | def list_images(self, detail=False, **kwargs): |
| 105 | """Return a list of all images filtered by input parameters. |
| 106 | |
zhufl | 3a36f05 | 2016-09-07 11:08:19 +0800 | [diff] [blame^] | 107 | For a full list of available parameters, please refer to the official |
| 108 | API reference: |
| 109 | http://developer.openstack.org/api-ref/image/v1/#list-images |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 110 | |
| 111 | Most parameters except the following are passed to the API without |
| 112 | any changes. |
| 113 | :param changes_since: The name is changed to changes-since |
| 114 | """ |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 115 | url = 'images' |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 116 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 117 | if detail: |
| 118 | url += '/detail' |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 119 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 120 | if kwargs.get('changes_since'): |
| 121 | kwargs['changes-since'] = kwargs.pop('changes_since') |
ivan-zhu | d1bbe5d | 2013-12-29 18:32:46 +0800 | [diff] [blame] | 122 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 123 | if len(kwargs) > 0: |
| 124 | url += '?%s' % urllib.urlencode(kwargs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 125 | |
| 126 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 127 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 128 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 129 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 130 | |
Yaroslav Lobankov | 9d28a0f | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 131 | def check_image(self, image_id): |
| 132 | """Check image metadata.""" |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 133 | url = 'images/%s' % image_id |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 134 | resp, body = self.head(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 135 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 136 | return rest_client.ResponseBody(resp, body) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 137 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 138 | def show_image(self, image_id): |
Yaroslav Lobankov | 9d28a0f | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 139 | """Get image details plus the image itself.""" |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 140 | url = 'images/%s' % image_id |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 141 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 142 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 143 | return rest_client.ResponseBodyData(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 144 | |
| 145 | def is_resource_deleted(self, id): |
| 146 | try: |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 147 | resp = self.check_image(id) |
| 148 | if resp.response["x-image-meta-status"] == 'deleted': |
Joshua White | 95dcef2 | 2016-04-11 06:17:42 -0700 | [diff] [blame] | 149 | return True |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 150 | except lib_exc.NotFound: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 151 | return True |
| 152 | return False |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 153 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 154 | @property |
| 155 | def resource_type(self): |
| 156 | """Returns the primary type of resource this client works with.""" |
| 157 | return 'image_meta' |