Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -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 json |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 17 | |
| 18 | import jsonschema |
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 | from tempest_lib import exceptions as lib_exc |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 21 | |
| 22 | from tempest.common import glance_http |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 23 | from tempest.common import service_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 24 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 25 | |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 26 | class ImageClientV2JSON(service_client.ServiceClient): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 27 | |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 28 | def __init__(self, auth_provider, catalog_type, region, endpoint_type=None, |
| 29 | build_interval=None, build_timeout=None, |
| 30 | disable_ssl_certificate_validation=None, ca_certs=None, |
David Kranz | 85b660b | 2015-03-23 10:26:52 -0400 | [diff] [blame] | 31 | trace_requests=None): |
Ken'ichi Ohmichi | 0690ea4 | 2015-01-02 07:03:51 +0000 | [diff] [blame] | 32 | super(ImageClientV2JSON, self).__init__( |
| 33 | auth_provider, |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 34 | catalog_type, |
| 35 | region, |
| 36 | endpoint_type=endpoint_type, |
| 37 | build_interval=build_interval, |
| 38 | build_timeout=build_timeout, |
| 39 | disable_ssl_certificate_validation=( |
| 40 | disable_ssl_certificate_validation), |
| 41 | ca_certs=ca_certs, |
David Kranz | 85b660b | 2015-03-23 10:26:52 -0400 | [diff] [blame] | 42 | trace_requests=trace_requests) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 43 | self._http = None |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 44 | self.dscv = disable_ssl_certificate_validation |
| 45 | self.ca_certs = ca_certs |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 46 | |
| 47 | def _get_http(self): |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 48 | return glance_http.HTTPClient(auth_provider=self.auth_provider, |
| 49 | filters=self.filters, |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 50 | insecure=self.dscv, |
| 51 | ca_certs=self.ca_certs) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 52 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 53 | def _validate_schema(self, body, type='image'): |
raiesmh08 | a1ce354 | 2014-03-04 11:58:29 +0530 | [diff] [blame] | 54 | if type in ['image', 'images']: |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 55 | schema = self.get_schema(type) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 56 | else: |
| 57 | raise ValueError("%s is not a valid schema type" % type) |
| 58 | |
| 59 | jsonschema.validate(body, schema) |
| 60 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 61 | @property |
| 62 | def http(self): |
| 63 | if self._http is None: |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 64 | self._http = self._get_http() |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 65 | return self._http |
| 66 | |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 67 | def update_image(self, image_id, patch): |
| 68 | data = json.dumps(patch) |
| 69 | self._validate_schema(data) |
| 70 | |
| 71 | headers = {"Content-Type": "application/openstack-images-v2.0" |
| 72 | "-json-patch"} |
| 73 | resp, body = self.patch('v2/images/%s' % image_id, data, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 74 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 75 | return service_client.ResponseBody(resp, self._parse_resp(body)) |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 76 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 77 | def create_image(self, name, container_format, disk_format, **kwargs): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 78 | params = { |
| 79 | "name": name, |
| 80 | "container_format": container_format, |
| 81 | "disk_format": disk_format, |
| 82 | } |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 83 | |
Sean Dague | c6ec476 | 2014-05-29 08:54:21 -0400 | [diff] [blame] | 84 | for option in kwargs: |
| 85 | value = kwargs.get(option) |
| 86 | if isinstance(value, dict) or isinstance(value, tuple): |
| 87 | params.update(value) |
| 88 | else: |
| 89 | params[option] = value |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 90 | |
| 91 | data = json.dumps(params) |
| 92 | self._validate_schema(data) |
| 93 | |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 94 | resp, body = self.post('v2/images', data) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 95 | self.expected_success(201, resp.status) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 96 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 97 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 98 | |
bkopilov | 81aaae7 | 2015-05-15 23:46:25 +0300 | [diff] [blame^] | 99 | def deactivate_image(self, image_id): |
| 100 | url = 'v2/images/%s/actions/deactivate' % image_id |
| 101 | resp, body = self.post(url, None) |
| 102 | self.expected_success(204, resp.status) |
| 103 | return service_client.ResponseBody(resp, body) |
| 104 | |
| 105 | def reactivate_image(self, image_id): |
| 106 | url = 'v2/images/%s/actions/reactivate' % image_id |
| 107 | resp, body = self.post(url, None) |
| 108 | self.expected_success(204, resp.status) |
| 109 | return service_client.ResponseBody(resp, body) |
| 110 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 111 | def delete_image(self, image_id): |
| 112 | url = 'v2/images/%s' % image_id |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 113 | resp, _ = self.delete(url) |
| 114 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 115 | return service_client.ResponseBody(resp) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 116 | |
Ken'ichi Ohmichi | e3acc12 | 2015-05-22 00:32:54 +0000 | [diff] [blame] | 117 | def list_images(self, params=None): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 118 | url = 'v2/images' |
| 119 | |
| 120 | if params: |
| 121 | url += '?%s' % urllib.urlencode(params) |
| 122 | |
| 123 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 124 | self.expected_success(200, resp.status) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 125 | body = json.loads(body) |
| 126 | self._validate_schema(body, type='images') |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 127 | return service_client.ResponseBodyList(resp, body['images']) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 128 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 129 | def show_image(self, image_id): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 130 | url = 'v2/images/%s' % image_id |
| 131 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 132 | self.expected_success(200, resp.status) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 133 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 134 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 135 | |
| 136 | def is_resource_deleted(self, id): |
| 137 | try: |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 138 | self.show_image(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 139 | except lib_exc.NotFound: |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 140 | return True |
| 141 | return False |
| 142 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 143 | @property |
| 144 | def resource_type(self): |
| 145 | """Returns the primary type of resource this client works with.""" |
| 146 | return 'image' |
| 147 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 148 | def store_image(self, image_id, data): |
| 149 | url = 'v2/images/%s/file' % image_id |
| 150 | headers = {'Content-Type': 'application/octet-stream'} |
| 151 | resp, body = self.http.raw_request('PUT', url, headers=headers, |
| 152 | body=data) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 153 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 154 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 155 | |
| 156 | def get_image_file(self, image_id): |
| 157 | url = 'v2/images/%s/file' % image_id |
| 158 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 159 | self.expected_success(200, resp.status) |
David Kranz | d7e97b4 | 2015-02-16 09:37:31 -0500 | [diff] [blame] | 160 | return service_client.ResponseBodyData(resp, body) |
Anju Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 161 | |
| 162 | def add_image_tag(self, image_id, tag): |
| 163 | url = 'v2/images/%s/tags/%s' % (image_id, tag) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 164 | resp, body = self.put(url, body=None) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 165 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 166 | return service_client.ResponseBody(resp, body) |
Anju Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 167 | |
| 168 | def delete_image_tag(self, image_id, tag): |
| 169 | url = 'v2/images/%s/tags/%s' % (image_id, tag) |
| 170 | resp, _ = self.delete(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 171 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 172 | return service_client.ResponseBody(resp) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 173 | |
Ken'ichi Ohmichi | f0df53c | 2015-05-22 01:16:50 +0000 | [diff] [blame] | 174 | def list_image_members(self, image_id): |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 175 | url = 'v2/images/%s/members' % image_id |
| 176 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 177 | self.expected_success(200, resp.status) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 178 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 179 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 180 | |
| 181 | def add_member(self, image_id, member_id): |
| 182 | url = 'v2/images/%s/members' % image_id |
| 183 | data = json.dumps({'member': member_id}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 184 | resp, body = self.post(url, data) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 185 | self.expected_success(200, resp.status) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 186 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 187 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 188 | |
| 189 | def update_member_status(self, image_id, member_id, status): |
| 190 | """Valid status are: ``pending``, ``accepted``, ``rejected``.""" |
| 191 | url = 'v2/images/%s/members/%s' % (image_id, member_id) |
| 192 | data = json.dumps({'status': status}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 193 | resp, body = self.put(url, data) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 194 | self.expected_success(200, resp.status) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 195 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 196 | return service_client.ResponseBody(resp, body) |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 197 | |
| 198 | def get_member(self, image_id, member_id): |
| 199 | url = 'v2/images/%s/members/%s' % (image_id, member_id) |
| 200 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 201 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 202 | return service_client.ResponseBody(resp, json.loads(body)) |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 203 | |
| 204 | def remove_member(self, image_id, member_id): |
| 205 | url = 'v2/images/%s/members/%s' % (image_id, member_id) |
| 206 | resp, _ = self.delete(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 207 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 208 | return service_client.ResponseBody(resp) |
raiesmh08 | a1ce354 | 2014-03-04 11:58:29 +0530 | [diff] [blame] | 209 | |
| 210 | def get_schema(self, schema): |
| 211 | url = 'v2/schemas/%s' % schema |
| 212 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 213 | self.expected_success(200, resp.status) |
raiesmh08 | a1ce354 | 2014-03-04 11:58:29 +0530 | [diff] [blame] | 214 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 215 | return service_client.ResponseBody(resp, body) |