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 | |
| 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 Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 64 | @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 Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 71 | def create_image(self, name, container_format, disk_format, **kwargs): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 72 | params = { |
| 73 | "name": name, |
| 74 | "container_format": container_format, |
| 75 | "disk_format": disk_format, |
| 76 | } |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 77 | |
| 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 Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 85 | |
| 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 S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 108 | def get_image(self, image_id): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 109 | 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 S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 116 | self.get_image(id) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 117 | 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 Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 132 | |
| 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 Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 142 | |
| 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 |