blob: 201869e148eabc1559af33afddb0ec2c96d92005 [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
17import urllib
18
19import jsonschema
20
21from tempest.common import glance_http
22from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000023from tempest import config
Matthew Treinisha62347f2013-03-01 16:37:30 -050024from tempest import exceptions
25
Matthew Treinish684d8992014-01-30 16:27:40 +000026CONF = config.CONF
27
Matthew Treinisha62347f2013-03-01 16:37:30 -050028
29class ImageClientV2JSON(rest_client.RestClient):
30
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(ImageClientV2JSON, self).__init__(auth_provider)
Matt Riedemannd3efe902014-02-10 06:46:38 -080033 self.service = CONF.image.catalog_type
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000034 self._http = None
Matthew Treinisha62347f2013-03-01 16:37:30 -050035
36 def _get_http(self):
Matthew Treinish684d8992014-01-30 16:27:40 +000037 dscv = CONF.identity.disable_ssl_certificate_validation
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000038 return glance_http.HTTPClient(auth_provider=self.auth_provider,
39 filters=self.filters,
Matthew Treinisha62347f2013-03-01 16:37:30 -050040 insecure=dscv)
41
Matthew Treinisha62347f2013-03-01 16:37:30 -050042 def _validate_schema(self, body, type='image'):
raiesmh08a1ce3542014-03-04 11:58:29 +053043 if type in ['image', 'images']:
44 resp, schema = self.get_schema(type)
Matthew Treinisha62347f2013-03-01 16:37:30 -050045 else:
46 raise ValueError("%s is not a valid schema type" % type)
47
48 jsonschema.validate(body, schema)
49
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000050 @property
51 def http(self):
52 if self._http is None:
53 if CONF.service_available.glance:
54 self._http = self._get_http()
55 return self._http
56
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040057 def update_image(self, image_id, patch):
58 data = json.dumps(patch)
59 self._validate_schema(data)
60
61 headers = {"Content-Type": "application/openstack-images-v2.0"
62 "-json-patch"}
63 resp, body = self.patch('v2/images/%s' % image_id, data, headers)
64 return resp, self._parse_resp(body)
65
Matthew Treinishce3ef922013-03-11 14:02:46 -040066 def create_image(self, name, container_format, disk_format, **kwargs):
Matthew Treinisha62347f2013-03-01 16:37:30 -050067 params = {
68 "name": name,
69 "container_format": container_format,
70 "disk_format": disk_format,
71 }
Matthew Treinishce3ef922013-03-11 14:02:46 -040072
Sean Daguec6ec4762014-05-29 08:54:21 -040073 for option in kwargs:
74 value = kwargs.get(option)
75 if isinstance(value, dict) or isinstance(value, tuple):
76 params.update(value)
77 else:
78 params[option] = value
Matthew Treinisha62347f2013-03-01 16:37:30 -050079
80 data = json.dumps(params)
81 self._validate_schema(data)
82
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020083 resp, body = self.post('v2/images', data)
Matthew Treinisha62347f2013-03-01 16:37:30 -050084 body = json.loads(body)
85 return resp, body
86
87 def delete_image(self, image_id):
88 url = 'v2/images/%s' % image_id
89 self.delete(url)
90
91 def image_list(self, params=None):
92 url = 'v2/images'
93
94 if params:
95 url += '?%s' % urllib.urlencode(params)
96
97 resp, body = self.get(url)
98 body = json.loads(body)
99 self._validate_schema(body, type='images')
100 return resp, body['images']
101
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +0530102 def get_image(self, image_id):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500103 url = 'v2/images/%s' % image_id
104 resp, body = self.get(url)
105 body = json.loads(body)
106 return resp, body
107
108 def is_resource_deleted(self, id):
109 try:
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +0530110 self.get_image(id)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500111 except exceptions.NotFound:
112 return True
113 return False
114
115 def store_image(self, image_id, data):
116 url = 'v2/images/%s/file' % image_id
117 headers = {'Content-Type': 'application/octet-stream'}
118 resp, body = self.http.raw_request('PUT', url, headers=headers,
119 body=data)
120 return resp, body
121
122 def get_image_file(self, image_id):
123 url = 'v2/images/%s/file' % image_id
124 resp, body = self.get(url)
125 return resp, body
Anju Tiwaric4952982013-10-20 07:10:02 +0530126
127 def add_image_tag(self, image_id, tag):
128 url = 'v2/images/%s/tags/%s' % (image_id, tag)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200129 resp, body = self.put(url, body=None)
Anju Tiwaric4952982013-10-20 07:10:02 +0530130 return resp, body
131
132 def delete_image_tag(self, image_id, tag):
133 url = 'v2/images/%s/tags/%s' % (image_id, tag)
134 resp, _ = self.delete(url)
135 return resp
Attila Fazekas689e2652013-10-07 18:06:57 +0200136
137 def get_image_membership(self, image_id):
138 url = 'v2/images/%s/members' % image_id
139 resp, body = self.get(url)
140 body = json.loads(body)
141 self.expected_success(200, resp)
142 return resp, body
143
144 def add_member(self, image_id, member_id):
145 url = 'v2/images/%s/members' % image_id
146 data = json.dumps({'member': member_id})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200147 resp, body = self.post(url, data)
Attila Fazekas689e2652013-10-07 18:06:57 +0200148 body = json.loads(body)
149 self.expected_success(200, resp)
150 return resp, body
151
152 def update_member_status(self, image_id, member_id, status):
153 """Valid status are: ``pending``, ``accepted``, ``rejected``."""
154 url = 'v2/images/%s/members/%s' % (image_id, member_id)
155 data = json.dumps({'status': status})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200156 resp, body = self.put(url, data)
Attila Fazekas689e2652013-10-07 18:06:57 +0200157 body = json.loads(body)
158 self.expected_success(200, resp)
159 return resp, body
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400160
161 def get_member(self, image_id, member_id):
162 url = 'v2/images/%s/members/%s' % (image_id, member_id)
163 resp, body = self.get(url)
164 self.expected_success(200, resp)
165 return resp, json.loads(body)
166
167 def remove_member(self, image_id, member_id):
168 url = 'v2/images/%s/members/%s' % (image_id, member_id)
169 resp, _ = self.delete(url)
170 self.expected_success(204, resp)
171 return resp
raiesmh08a1ce3542014-03-04 11:58:29 +0530172
173 def get_schema(self, schema):
174 url = 'v2/schemas/%s' % schema
175 resp, body = self.get(url)
176 body = json.loads(body)
177 return resp, body