blob: 342a09cbec41f14d65c28df4ed8f9475d337135e [file] [log] [blame]
Matthew Treinisha62347f2013-03-01 16:37:30 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04003# Copyright 2013 IBM Corp.
Matthew Treinisha62347f2013-03-01 16:37:30 -05004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import json
19import urllib
20
21import jsonschema
22
23from tempest.common import glance_http
24from tempest.common import rest_client
25from tempest import exceptions
26
27
28class ImageClientV2JSON(rest_client.RestClient):
29
30 def __init__(self, config, username, password, auth_url, tenant_name=None):
31 super(ImageClientV2JSON, self).__init__(config, username, password,
32 auth_url, tenant_name)
33 self.service = self.config.images.catalog_type
34 self.http = self._get_http()
35
36 def _get_http(self):
37 token, endpoint = self.keystone_auth(self.user, self.password,
38 self.auth_url, self.service,
39 self.tenant_name)
40 dscv = self.config.identity.disable_ssl_certificate_validation
41 return glance_http.HTTPClient(endpoint=endpoint, token=token,
42 insecure=dscv)
43
44 def get_images_schema(self):
45 url = 'v2/schemas/images'
46 resp, body = self.get(url)
47 body = json.loads(body)
48 return resp, body
49
50 def get_image_schema(self):
51 url = 'v2/schemas/image'
52 resp, body = self.get(url)
53 body = json.loads(body)
54 return resp, body
55
56 def _validate_schema(self, body, type='image'):
57 if type == 'image':
58 resp, schema = self.get_image_schema()
59 elif type == 'images':
60 resp, schema = self.get_images_schema()
61 else:
62 raise ValueError("%s is not a valid schema type" % type)
63
64 jsonschema.validate(body, schema)
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
73 for option in ['visibility']:
74 if option in kwargs:
75 value = kwargs.get(option)
76 if isinstance(value, dict) or isinstance(value, tuple):
77 params.update(value)
78 else:
79 params[option] = value
Matthew Treinisha62347f2013-03-01 16:37:30 -050080
81 data = json.dumps(params)
82 self._validate_schema(data)
83
84 resp, body = self.post('v2/images', data, self.headers)
85 body = json.loads(body)
86 return resp, body
87
88 def delete_image(self, image_id):
89 url = 'v2/images/%s' % image_id
90 self.delete(url)
91
92 def image_list(self, params=None):
93 url = 'v2/images'
94
95 if params:
96 url += '?%s' % urllib.urlencode(params)
97
98 resp, body = self.get(url)
99 body = json.loads(body)
100 self._validate_schema(body, type='images')
101 return resp, body['images']
102
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +0530103 def get_image(self, image_id):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500104 url = 'v2/images/%s' % image_id
105 resp, body = self.get(url)
106 body = json.loads(body)
107 return resp, body
108
109 def is_resource_deleted(self, id):
110 try:
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +0530111 self.get_image(id)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500112 except exceptions.NotFound:
113 return True
114 return False
115
116 def store_image(self, image_id, data):
117 url = 'v2/images/%s/file' % image_id
118 headers = {'Content-Type': 'application/octet-stream'}
119 resp, body = self.http.raw_request('PUT', url, headers=headers,
120 body=data)
121 return resp, body
122
123 def get_image_file(self, image_id):
124 url = 'v2/images/%s/file' % image_id
125 resp, body = self.get(url)
126 return resp, body
Anju Tiwaric4952982013-10-20 07:10:02 +0530127
128 def add_image_tag(self, image_id, tag):
129 url = 'v2/images/%s/tags/%s' % (image_id, tag)
130 resp, body = self.put(url, body=None, headers=self.headers)
131 return resp, body
132
133 def delete_image_tag(self, image_id, tag):
134 url = 'v2/images/%s/tags/%s' % (image_id, tag)
135 resp, _ = self.delete(url)
136 return resp