blob: 383c72bd5e8ce22eb77d0b7973a16a321af1d1cd [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2013 IBM Corp.
Matthew Treinisha62347f2013-03-01 16:37:30 -05002# 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
16import json
Matthew Treinisha62347f2013-03-01 16:37:30 -050017
18import jsonschema
Matthew Treinish89128142015-04-23 10:44:30 -040019from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090020from tempest_lib import exceptions as lib_exc
Matthew Treinisha62347f2013-03-01 16:37:30 -050021
22from tempest.common import glance_http
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000023from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000024
Matthew Treinisha62347f2013-03-01 16:37:30 -050025
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000026class ImageClientV2JSON(service_client.ServiceClient):
Matthew Treinisha62347f2013-03-01 16:37:30 -050027
Masayuki Igawabc7e1892015-03-03 11:46:48 +090028 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 Kranz85b660b2015-03-23 10:26:52 -040031 trace_requests=None):
Ken'ichi Ohmichi0690ea42015-01-02 07:03:51 +000032 super(ImageClientV2JSON, self).__init__(
33 auth_provider,
Masayuki Igawabc7e1892015-03-03 11:46:48 +090034 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 Kranz85b660b2015-03-23 10:26:52 -040042 trace_requests=trace_requests)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000043 self._http = None
Masayuki Igawabc7e1892015-03-03 11:46:48 +090044 self.dscv = disable_ssl_certificate_validation
45 self.ca_certs = ca_certs
Matthew Treinisha62347f2013-03-01 16:37:30 -050046
47 def _get_http(self):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000048 return glance_http.HTTPClient(auth_provider=self.auth_provider,
49 filters=self.filters,
Masayuki Igawabc7e1892015-03-03 11:46:48 +090050 insecure=self.dscv,
51 ca_certs=self.ca_certs)
Matthew Treinisha62347f2013-03-01 16:37:30 -050052
Matthew Treinisha62347f2013-03-01 16:37:30 -050053 def _validate_schema(self, body, type='image'):
raiesmh08a1ce3542014-03-04 11:58:29 +053054 if type in ['image', 'images']:
Ken'ichi Ohmichi66494e92015-06-08 04:28:10 +000055 schema = self.show_schema(type)
Matthew Treinisha62347f2013-03-01 16:37:30 -050056 else:
57 raise ValueError("%s is not a valid schema type" % type)
58
59 jsonschema.validate(body, schema)
60
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000061 @property
62 def http(self):
63 if self._http is None:
Masayuki Igawabc7e1892015-03-03 11:46:48 +090064 self._http = self._get_http()
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000065 return self._http
66
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040067 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 Kranz9c3b3b62014-06-19 16:05:53 -040074 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000075 return service_client.ResponseBody(resp, self._parse_resp(body))
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040076
Matthew Treinishce3ef922013-03-11 14:02:46 -040077 def create_image(self, name, container_format, disk_format, **kwargs):
Matthew Treinisha62347f2013-03-01 16:37:30 -050078 params = {
79 "name": name,
80 "container_format": container_format,
81 "disk_format": disk_format,
82 }
Matthew Treinishce3ef922013-03-11 14:02:46 -040083
Sean Daguec6ec4762014-05-29 08:54:21 -040084 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 Treinisha62347f2013-03-01 16:37:30 -050090
91 data = json.dumps(params)
92 self._validate_schema(data)
93
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020094 resp, body = self.post('v2/images', data)
David Kranz9c3b3b62014-06-19 16:05:53 -040095 self.expected_success(201, resp.status)
Matthew Treinisha62347f2013-03-01 16:37:30 -050096 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000097 return service_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050098
bkopilov81aaae72015-05-15 23:46:25 +030099 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 Treinisha62347f2013-03-01 16:37:30 -0500111 def delete_image(self, image_id):
112 url = 'v2/images/%s' % image_id
David Kranz9c3b3b62014-06-19 16:05:53 -0400113 resp, _ = self.delete(url)
114 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000115 return service_client.ResponseBody(resp)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500116
Ken'ichi Ohmichie3acc122015-05-22 00:32:54 +0000117 def list_images(self, params=None):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500118 url = 'v2/images'
119
120 if params:
121 url += '?%s' % urllib.urlencode(params)
122
123 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400124 self.expected_success(200, resp.status)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500125 body = json.loads(body)
126 self._validate_schema(body, type='images')
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000127 return service_client.ResponseBodyList(resp, body['images'])
Matthew Treinisha62347f2013-03-01 16:37:30 -0500128
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000129 def show_image(self, image_id):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500130 url = 'v2/images/%s' % image_id
131 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400132 self.expected_success(200, resp.status)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500133 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000134 return service_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500135
136 def is_resource_deleted(self, id):
137 try:
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000138 self.show_image(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900139 except lib_exc.NotFound:
Matthew Treinisha62347f2013-03-01 16:37:30 -0500140 return True
141 return False
142
Matt Riedemannd2b96512014-10-13 10:18:16 -0700143 @property
144 def resource_type(self):
145 """Returns the primary type of resource this client works with."""
146 return 'image'
147
Ken'ichi Ohmichi66494e92015-06-08 04:28:10 +0000148 def store_image_file(self, image_id, data):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500149 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 Kranz9c3b3b62014-06-19 16:05:53 -0400153 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000154 return service_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500155
Ken'ichi Ohmichi66494e92015-06-08 04:28:10 +0000156 def load_image_file(self, image_id):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500157 url = 'v2/images/%s/file' % image_id
158 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400159 self.expected_success(200, resp.status)
David Kranzd7e97b42015-02-16 09:37:31 -0500160 return service_client.ResponseBodyData(resp, body)
Anju Tiwaric4952982013-10-20 07:10:02 +0530161
162 def add_image_tag(self, image_id, tag):
163 url = 'v2/images/%s/tags/%s' % (image_id, tag)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200164 resp, body = self.put(url, body=None)
David Kranz9c3b3b62014-06-19 16:05:53 -0400165 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000166 return service_client.ResponseBody(resp, body)
Anju Tiwaric4952982013-10-20 07:10:02 +0530167
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 Kranz9c3b3b62014-06-19 16:05:53 -0400171 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000172 return service_client.ResponseBody(resp)
Attila Fazekas689e2652013-10-07 18:06:57 +0200173
Ken'ichi Ohmichif0df53c2015-05-22 01:16:50 +0000174 def list_image_members(self, image_id):
Attila Fazekas689e2652013-10-07 18:06:57 +0200175 url = 'v2/images/%s/members' % image_id
176 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400177 self.expected_success(200, resp.status)
Attila Fazekas689e2652013-10-07 18:06:57 +0200178 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000179 return service_client.ResponseBody(resp, body)
Attila Fazekas689e2652013-10-07 18:06:57 +0200180
Ken'ichi Ohmichic3728c62015-06-08 04:01:45 +0000181 def add_image_member(self, image_id, member_id):
Attila Fazekas689e2652013-10-07 18:06:57 +0200182 url = 'v2/images/%s/members' % image_id
183 data = json.dumps({'member': member_id})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200184 resp, body = self.post(url, data)
David Kranz9c3b3b62014-06-19 16:05:53 -0400185 self.expected_success(200, resp.status)
Attila Fazekas689e2652013-10-07 18:06:57 +0200186 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000187 return service_client.ResponseBody(resp, body)
Attila Fazekas689e2652013-10-07 18:06:57 +0200188
Ken'ichi Ohmichic3728c62015-06-08 04:01:45 +0000189 def update_image_member(self, image_id, member_id, body):
Attila Fazekas689e2652013-10-07 18:06:57 +0200190 url = 'v2/images/%s/members/%s' % (image_id, member_id)
Ken'ichi Ohmichic3728c62015-06-08 04:01:45 +0000191 data = json.dumps(body)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200192 resp, body = self.put(url, data)
David Kranz9c3b3b62014-06-19 16:05:53 -0400193 self.expected_success(200, resp.status)
Attila Fazekas689e2652013-10-07 18:06:57 +0200194 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000195 return service_client.ResponseBody(resp, body)
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400196
Ken'ichi Ohmichic3728c62015-06-08 04:01:45 +0000197 def show_image_member(self, image_id, member_id):
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400198 url = 'v2/images/%s/members/%s' % (image_id, member_id)
199 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400200 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000201 return service_client.ResponseBody(resp, json.loads(body))
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400202
Ken'ichi Ohmichic3728c62015-06-08 04:01:45 +0000203 def remove_image_member(self, image_id, member_id):
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400204 url = 'v2/images/%s/members/%s' % (image_id, member_id)
205 resp, _ = self.delete(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400206 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000207 return service_client.ResponseBody(resp)
raiesmh08a1ce3542014-03-04 11:58:29 +0530208
Ken'ichi Ohmichi66494e92015-06-08 04:28:10 +0000209 def show_schema(self, schema):
raiesmh08a1ce3542014-03-04 11:58:29 +0530210 url = 'v2/schemas/%s' % schema
211 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400212 self.expected_success(200, resp.status)
raiesmh08a1ce3542014-03-04 11:58:29 +0530213 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000214 return service_client.ResponseBody(resp, body)