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 |
| 17 | import urllib |
| 18 | |
| 19 | import jsonschema |
| 20 | |
| 21 | from tempest.common import glance_http |
| 22 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | from tempest import config |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 24 | from tempest import exceptions |
| 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | CONF = config.CONF |
| 27 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 28 | |
| 29 | class ImageClientV2JSON(rest_client.RestClient): |
| 30 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 31 | def __init__(self, auth_provider): |
| 32 | super(ImageClientV2JSON, self).__init__(auth_provider) |
Matt Riedemann | d3efe90 | 2014-02-10 06:46:38 -0800 | [diff] [blame] | 33 | self.service = CONF.image.catalog_type |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 34 | self._http = None |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 35 | |
| 36 | def _get_http(self): |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 37 | dscv = CONF.identity.disable_ssl_certificate_validation |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 38 | return glance_http.HTTPClient(auth_provider=self.auth_provider, |
| 39 | filters=self.filters, |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 40 | insecure=dscv) |
| 41 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 42 | def _validate_schema(self, body, type='image'): |
raiesmh08 | a1ce354 | 2014-03-04 11:58:29 +0530 | [diff] [blame] | 43 | if type in ['image', 'images']: |
| 44 | resp, schema = self.get_schema(type) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 45 | else: |
| 46 | raise ValueError("%s is not a valid schema type" % type) |
| 47 | |
| 48 | jsonschema.validate(body, schema) |
| 49 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 50 | @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 Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 57 | 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 Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 66 | def create_image(self, name, container_format, disk_format, **kwargs): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 67 | params = { |
| 68 | "name": name, |
| 69 | "container_format": container_format, |
| 70 | "disk_format": disk_format, |
| 71 | } |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 72 | |
Sean Dague | c6ec476 | 2014-05-29 08:54:21 -0400 | [diff] [blame^] | 73 | 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 Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 79 | |
| 80 | data = json.dumps(params) |
| 81 | self._validate_schema(data) |
| 82 | |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 83 | resp, body = self.post('v2/images', data) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 84 | 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 S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 102 | def get_image(self, image_id): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 103 | 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 S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 110 | self.get_image(id) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 111 | 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 Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 126 | |
| 127 | def add_image_tag(self, image_id, tag): |
| 128 | url = 'v2/images/%s/tags/%s' % (image_id, tag) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 129 | resp, body = self.put(url, body=None) |
Anju Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 130 | 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 Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 136 | |
| 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 147 | resp, body = self.post(url, data) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 148 | 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 156 | resp, body = self.put(url, data) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 157 | body = json.loads(body) |
| 158 | self.expected_success(200, resp) |
| 159 | return resp, body |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 160 | |
| 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 |
raiesmh08 | a1ce354 | 2014-03-04 11:58:29 +0530 | [diff] [blame] | 172 | |
| 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 |