Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 13 | |
| 14 | from oslo_serialization import jsonutils as json |
John L. Villalovos | d88fe9f | 2018-02-01 16:24:43 -0800 | [diff] [blame] | 15 | import six |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 16 | from six.moves import http_client |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 17 | from six.moves.urllib import parse as urllib |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 18 | from tempest.lib.common import api_version_utils |
Lenny Verkhovsky | 8862504 | 2016-03-08 17:44:00 +0200 | [diff] [blame] | 19 | from tempest.lib.common import rest_client |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 20 | |
Vasyl Saienko | 4ddbeec | 2017-01-20 16:26:04 +0000 | [diff] [blame] | 21 | # NOTE(vsaienko): concurrent tests work because they are launched in |
| 22 | # separate processes so global variables are not shared among them. |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 23 | BAREMETAL_MICROVERSION = None |
| 24 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 25 | |
Vasyl Saienko | 4ddbeec | 2017-01-20 16:26:04 +0000 | [diff] [blame] | 26 | def set_baremetal_api_microversion(baremetal_microversion): |
| 27 | global BAREMETAL_MICROVERSION |
| 28 | BAREMETAL_MICROVERSION = baremetal_microversion |
| 29 | |
| 30 | |
| 31 | def reset_baremetal_api_microversion(): |
| 32 | global BAREMETAL_MICROVERSION |
| 33 | BAREMETAL_MICROVERSION = None |
| 34 | |
| 35 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 36 | def handle_errors(f): |
| 37 | """A decorator that allows to ignore certain types of errors.""" |
| 38 | |
John L. Villalovos | d88fe9f | 2018-02-01 16:24:43 -0800 | [diff] [blame] | 39 | @six.wraps(f) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 40 | def wrapper(*args, **kwargs): |
| 41 | param_name = 'ignore_errors' |
| 42 | ignored_errors = kwargs.get(param_name, tuple()) |
| 43 | |
| 44 | if param_name in kwargs: |
| 45 | del kwargs[param_name] |
| 46 | |
| 47 | try: |
| 48 | return f(*args, **kwargs) |
| 49 | except ignored_errors: |
| 50 | # Silently ignore errors |
| 51 | pass |
| 52 | |
| 53 | return wrapper |
| 54 | |
| 55 | |
| 56 | class BaremetalClient(rest_client.RestClient): |
| 57 | """Base Tempest REST client for Ironic API.""" |
| 58 | |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 59 | api_microversion_header_name = 'X-OpenStack-Ironic-API-Version' |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 60 | uri_prefix = '' |
| 61 | |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 62 | def get_headers(self): |
| 63 | headers = super(BaremetalClient, self).get_headers() |
| 64 | if BAREMETAL_MICROVERSION: |
| 65 | headers[self.api_microversion_header_name] = BAREMETAL_MICROVERSION |
| 66 | return headers |
| 67 | |
Vasyl Saienko | f20979c | 2016-05-27 11:25:01 +0300 | [diff] [blame] | 68 | def request(self, *args, **kwargs): |
| 69 | resp, resp_body = super(BaremetalClient, self).request(*args, **kwargs) |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 70 | if (BAREMETAL_MICROVERSION and |
| 71 | BAREMETAL_MICROVERSION != api_version_utils.LATEST_MICROVERSION): |
| 72 | api_version_utils.assert_version_header_matches_request( |
| 73 | self.api_microversion_header_name, |
| 74 | BAREMETAL_MICROVERSION, |
| 75 | resp) |
| 76 | return resp, resp_body |
| 77 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 78 | def serialize(self, object_dict): |
| 79 | """Serialize an Ironic object.""" |
| 80 | |
| 81 | return json.dumps(object_dict) |
| 82 | |
| 83 | def deserialize(self, object_str): |
| 84 | """Deserialize an Ironic object.""" |
| 85 | |
| 86 | return json.loads(object_str) |
| 87 | |
| 88 | def _get_uri(self, resource_name, uuid=None, permanent=False): |
| 89 | """Get URI for a specific resource or object. |
| 90 | |
| 91 | :param resource_name: The name of the REST resource, e.g., 'nodes'. |
| 92 | :param uuid: The unique identifier of an object in UUID format. |
| 93 | :returns: Relative URI for the resource or object. |
| 94 | |
| 95 | """ |
| 96 | prefix = self.uri_prefix if not permanent else '' |
| 97 | |
| 98 | return '{pref}/{res}{uuid}'.format(pref=prefix, |
| 99 | res=resource_name, |
| 100 | uuid='/%s' % uuid if uuid else '') |
| 101 | |
| 102 | def _make_patch(self, allowed_attributes, **kwargs): |
| 103 | """Create a JSON patch according to RFC 6902. |
| 104 | |
| 105 | :param allowed_attributes: An iterable object that contains a set of |
| 106 | allowed attributes for an object. |
| 107 | :param **kwargs: Attributes and new values for them. |
| 108 | :returns: A JSON path that sets values of the specified attributes to |
| 109 | the new ones. |
| 110 | |
| 111 | """ |
| 112 | def get_change(kwargs, path='/'): |
Luong Anh Tuan | e22fbe1 | 2016-09-12 16:32:14 +0700 | [diff] [blame] | 113 | for name, value in kwargs.items(): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 114 | if isinstance(value, dict): |
| 115 | for ch in get_change(value, path + '%s/' % name): |
| 116 | yield ch |
| 117 | else: |
| 118 | if value is None: |
| 119 | yield {'path': path + name, |
| 120 | 'op': 'remove'} |
| 121 | else: |
| 122 | yield {'path': path + name, |
| 123 | 'value': value, |
| 124 | 'op': 'replace'} |
| 125 | |
| 126 | patch = [ch for ch in get_change(kwargs) |
| 127 | if ch['path'].lstrip('/') in allowed_attributes] |
| 128 | |
| 129 | return patch |
| 130 | |
Sam Betts | 462e9e6 | 2016-11-30 18:43:35 +0000 | [diff] [blame] | 131 | def _list_request(self, resource, permanent=False, headers=None, |
| 132 | extra_headers=False, **kwargs): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 133 | """Get the list of objects of the specified type. |
| 134 | |
| 135 | :param resource: The name of the REST resource, e.g., 'nodes'. |
Sam Betts | 462e9e6 | 2016-11-30 18:43:35 +0000 | [diff] [blame] | 136 | :param headers: List of headers to use in request. |
| 137 | :param extra_headers: Specify whether to use headers. |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 138 | :param **kwargs: Parameters for the request. |
| 139 | :returns: A tuple with the server response and deserialized JSON list |
| 140 | of objects |
| 141 | |
| 142 | """ |
| 143 | uri = self._get_uri(resource, permanent=permanent) |
| 144 | if kwargs: |
| 145 | uri += "?%s" % urllib.urlencode(kwargs) |
| 146 | |
Sam Betts | 462e9e6 | 2016-11-30 18:43:35 +0000 | [diff] [blame] | 147 | resp, body = self.get(uri, headers=headers, |
| 148 | extra_headers=extra_headers) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 149 | self.expected_success(http_client.OK, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 150 | |
| 151 | return resp, self.deserialize(body) |
| 152 | |
SofiiaAndriichenko | 569f0a4 | 2016-12-08 05:02:17 -0500 | [diff] [blame] | 153 | def _show_request(self, |
| 154 | resource, |
| 155 | uuid=None, |
| 156 | permanent=False, |
Mark Goddard | 56399cc | 2018-02-16 13:37:25 +0000 | [diff] [blame^] | 157 | headers=None, |
| 158 | extra_headers=False, |
SofiiaAndriichenko | 569f0a4 | 2016-12-08 05:02:17 -0500 | [diff] [blame] | 159 | **kwargs): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 160 | """Gets a specific object of the specified type. |
| 161 | |
| 162 | :param uuid: Unique identifier of the object in UUID format. |
Mark Goddard | 56399cc | 2018-02-16 13:37:25 +0000 | [diff] [blame^] | 163 | :param headers: List of headers to use in request. |
| 164 | :param extra_headers: Specify whether to use headers. |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 165 | :returns: Serialized object as a dictionary. |
| 166 | |
| 167 | """ |
| 168 | if 'uri' in kwargs: |
| 169 | uri = kwargs['uri'] |
| 170 | else: |
| 171 | uri = self._get_uri(resource, uuid=uuid, permanent=permanent) |
Mark Goddard | 56399cc | 2018-02-16 13:37:25 +0000 | [diff] [blame^] | 172 | resp, body = self.get(uri, headers=headers, |
| 173 | extra_headers=extra_headers) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 174 | self.expected_success(http_client.OK, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 175 | |
| 176 | return resp, self.deserialize(body) |
| 177 | |
| 178 | def _create_request(self, resource, object_dict): |
| 179 | """Create an object of the specified type. |
| 180 | |
| 181 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 182 | :param object_dict: A Python dict that represents an object of the |
| 183 | specified type. |
| 184 | :returns: A tuple with the server response and the deserialized created |
| 185 | object. |
| 186 | |
| 187 | """ |
| 188 | body = self.serialize(object_dict) |
| 189 | uri = self._get_uri(resource) |
| 190 | |
| 191 | resp, body = self.post(uri, body=body) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 192 | self.expected_success(http_client.CREATED, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 193 | |
| 194 | return resp, self.deserialize(body) |
| 195 | |
Sam Betts | 462e9e6 | 2016-11-30 18:43:35 +0000 | [diff] [blame] | 196 | def _create_request_no_response_body(self, resource, object_dict): |
| 197 | """Create an object of the specified type. |
| 198 | |
| 199 | Do not expect any body in the response. |
| 200 | |
| 201 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 202 | :param object_dict: A Python dict that represents an object of the |
| 203 | specified type. |
| 204 | :returns: The server response. |
| 205 | """ |
| 206 | |
| 207 | body = self.serialize(object_dict) |
| 208 | uri = self._get_uri(resource) |
| 209 | |
| 210 | resp, body = self.post(uri, body=body) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 211 | self.expected_success(http_client.NO_CONTENT, resp.status) |
Sam Betts | 462e9e6 | 2016-11-30 18:43:35 +0000 | [diff] [blame] | 212 | |
| 213 | return resp |
| 214 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 215 | def _delete_request(self, resource, uuid): |
| 216 | """Delete specified object. |
| 217 | |
| 218 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 219 | :param uuid: The unique identifier of an object in UUID format. |
| 220 | :returns: A tuple with the server response and the response body. |
| 221 | |
| 222 | """ |
| 223 | uri = self._get_uri(resource, uuid) |
| 224 | |
| 225 | resp, body = self.delete(uri) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 226 | self.expected_success(http_client.NO_CONTENT, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 227 | return resp, body |
| 228 | |
| 229 | def _patch_request(self, resource, uuid, patch_object): |
| 230 | """Update specified object with JSON-patch. |
| 231 | |
| 232 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 233 | :param uuid: The unique identifier of an object in UUID format. |
| 234 | :returns: A tuple with the server response and the serialized patched |
| 235 | object. |
| 236 | |
| 237 | """ |
| 238 | uri = self._get_uri(resource, uuid) |
| 239 | patch_body = json.dumps(patch_object) |
| 240 | |
| 241 | resp, body = self.patch(uri, body=patch_body) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 242 | self.expected_success(http_client.OK, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 243 | return resp, self.deserialize(body) |
| 244 | |
| 245 | @handle_errors |
| 246 | def get_api_description(self): |
| 247 | """Retrieves all versions of the Ironic API.""" |
| 248 | |
| 249 | return self._list_request('', permanent=True) |
| 250 | |
| 251 | @handle_errors |
| 252 | def get_version_description(self, version='v1'): |
Kyrylo Romanenko | 3600054 | 2016-09-16 15:07:40 +0300 | [diff] [blame] | 253 | """Retrieves the description of the API. |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 254 | |
| 255 | :param version: The version of the API. Default: 'v1'. |
| 256 | :returns: Serialized description of API resources. |
| 257 | |
| 258 | """ |
| 259 | return self._list_request(version, permanent=True) |
| 260 | |
| 261 | def _put_request(self, resource, put_object): |
| 262 | """Update specified object with JSON-patch.""" |
| 263 | uri = self._get_uri(resource) |
| 264 | put_body = json.dumps(put_object) |
| 265 | |
| 266 | resp, body = self.put(uri, body=put_body) |
Solio Sarabia | f78122e | 2017-02-23 16:17:34 -0600 | [diff] [blame] | 267 | self.expected_success([http_client.ACCEPTED, http_client.NO_CONTENT], |
| 268 | resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 269 | return resp, body |