blob: b825519a3a200815b9e01df4b720e8bae35f86af [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
42 def get_images_schema(self):
43 url = 'v2/schemas/images'
44 resp, body = self.get(url)
45 body = json.loads(body)
46 return resp, body
47
48 def get_image_schema(self):
49 url = 'v2/schemas/image'
50 resp, body = self.get(url)
51 body = json.loads(body)
52 return resp, body
53
54 def _validate_schema(self, body, type='image'):
55 if type == 'image':
56 resp, schema = self.get_image_schema()
57 elif type == 'images':
58 resp, schema = self.get_images_schema()
59 else:
60 raise ValueError("%s is not a valid schema type" % type)
61
62 jsonschema.validate(body, schema)
63
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000064 @property
65 def http(self):
66 if self._http is None:
67 if CONF.service_available.glance:
68 self._http = self._get_http()
69 return self._http
70
Matthew Treinishce3ef922013-03-11 14:02:46 -040071 def create_image(self, name, container_format, disk_format, **kwargs):
Matthew Treinisha62347f2013-03-01 16:37:30 -050072 params = {
73 "name": name,
74 "container_format": container_format,
75 "disk_format": disk_format,
76 }
Matthew Treinishce3ef922013-03-11 14:02:46 -040077
78 for option in ['visibility']:
79 if option in kwargs:
80 value = kwargs.get(option)
81 if isinstance(value, dict) or isinstance(value, tuple):
82 params.update(value)
83 else:
84 params[option] = value
Matthew Treinisha62347f2013-03-01 16:37:30 -050085
86 data = json.dumps(params)
87 self._validate_schema(data)
88
89 resp, body = self.post('v2/images', data, self.headers)
90 body = json.loads(body)
91 return resp, body
92
93 def delete_image(self, image_id):
94 url = 'v2/images/%s' % image_id
95 self.delete(url)
96
97 def image_list(self, params=None):
98 url = 'v2/images'
99
100 if params:
101 url += '?%s' % urllib.urlencode(params)
102
103 resp, body = self.get(url)
104 body = json.loads(body)
105 self._validate_schema(body, type='images')
106 return resp, body['images']
107
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +0530108 def get_image(self, image_id):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500109 url = 'v2/images/%s' % image_id
110 resp, body = self.get(url)
111 body = json.loads(body)
112 return resp, body
113
114 def is_resource_deleted(self, id):
115 try:
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +0530116 self.get_image(id)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500117 except exceptions.NotFound:
118 return True
119 return False
120
121 def store_image(self, image_id, data):
122 url = 'v2/images/%s/file' % image_id
123 headers = {'Content-Type': 'application/octet-stream'}
124 resp, body = self.http.raw_request('PUT', url, headers=headers,
125 body=data)
126 return resp, body
127
128 def get_image_file(self, image_id):
129 url = 'v2/images/%s/file' % image_id
130 resp, body = self.get(url)
131 return resp, body
Anju Tiwaric4952982013-10-20 07:10:02 +0530132
133 def add_image_tag(self, image_id, tag):
134 url = 'v2/images/%s/tags/%s' % (image_id, tag)
135 resp, body = self.put(url, body=None, headers=self.headers)
136 return resp, body
137
138 def delete_image_tag(self, image_id, tag):
139 url = 'v2/images/%s/tags/%s' % (image_id, tag)
140 resp, _ = self.delete(url)
141 return resp
Attila Fazekas689e2652013-10-07 18:06:57 +0200142
143 def get_image_membership(self, image_id):
144 url = 'v2/images/%s/members' % image_id
145 resp, body = self.get(url)
146 body = json.loads(body)
147 self.expected_success(200, resp)
148 return resp, body
149
150 def add_member(self, image_id, member_id):
151 url = 'v2/images/%s/members' % image_id
152 data = json.dumps({'member': member_id})
153 resp, body = self.post(url, data, self.headers)
154 body = json.loads(body)
155 self.expected_success(200, resp)
156 return resp, body
157
158 def update_member_status(self, image_id, member_id, status):
159 """Valid status are: ``pending``, ``accepted``, ``rejected``."""
160 url = 'v2/images/%s/members/%s' % (image_id, member_id)
161 data = json.dumps({'status': status})
162 resp, body = self.put(url, data, self.headers)
163 body = json.loads(body)
164 self.expected_success(200, resp)
165 return resp, body