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 | |
| 13 | import functools |
| 14 | |
| 15 | from oslo_serialization import jsonutils as json |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 16 | from six.moves.urllib import parse as urllib |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 17 | from tempest.lib.common import api_version_utils |
Lenny Verkhovsky | 8862504 | 2016-03-08 17:44:00 +0200 | [diff] [blame] | 18 | from tempest.lib.common import rest_client |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 19 | |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 20 | BAREMETAL_MICROVERSION = None |
| 21 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 22 | |
| 23 | def handle_errors(f): |
| 24 | """A decorator that allows to ignore certain types of errors.""" |
| 25 | |
| 26 | @functools.wraps(f) |
| 27 | def wrapper(*args, **kwargs): |
| 28 | param_name = 'ignore_errors' |
| 29 | ignored_errors = kwargs.get(param_name, tuple()) |
| 30 | |
| 31 | if param_name in kwargs: |
| 32 | del kwargs[param_name] |
| 33 | |
| 34 | try: |
| 35 | return f(*args, **kwargs) |
| 36 | except ignored_errors: |
| 37 | # Silently ignore errors |
| 38 | pass |
| 39 | |
| 40 | return wrapper |
| 41 | |
| 42 | |
| 43 | class BaremetalClient(rest_client.RestClient): |
| 44 | """Base Tempest REST client for Ironic API.""" |
| 45 | |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 46 | api_microversion_header_name = 'X-OpenStack-Ironic-API-Version' |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 47 | uri_prefix = '' |
| 48 | |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 49 | def get_headers(self): |
| 50 | headers = super(BaremetalClient, self).get_headers() |
| 51 | if BAREMETAL_MICROVERSION: |
| 52 | headers[self.api_microversion_header_name] = BAREMETAL_MICROVERSION |
| 53 | return headers |
| 54 | |
Vasyl Saienko | f20979c | 2016-05-27 11:25:01 +0300 | [diff] [blame] | 55 | def request(self, *args, **kwargs): |
| 56 | resp, resp_body = super(BaremetalClient, self).request(*args, **kwargs) |
Yuiko Takada | ff78500 | 2015-12-17 15:56:42 +0900 | [diff] [blame] | 57 | if (BAREMETAL_MICROVERSION and |
| 58 | BAREMETAL_MICROVERSION != api_version_utils.LATEST_MICROVERSION): |
| 59 | api_version_utils.assert_version_header_matches_request( |
| 60 | self.api_microversion_header_name, |
| 61 | BAREMETAL_MICROVERSION, |
| 62 | resp) |
| 63 | return resp, resp_body |
| 64 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 65 | def serialize(self, object_dict): |
| 66 | """Serialize an Ironic object.""" |
| 67 | |
| 68 | return json.dumps(object_dict) |
| 69 | |
| 70 | def deserialize(self, object_str): |
| 71 | """Deserialize an Ironic object.""" |
| 72 | |
| 73 | return json.loads(object_str) |
| 74 | |
| 75 | def _get_uri(self, resource_name, uuid=None, permanent=False): |
| 76 | """Get URI for a specific resource or object. |
| 77 | |
| 78 | :param resource_name: The name of the REST resource, e.g., 'nodes'. |
| 79 | :param uuid: The unique identifier of an object in UUID format. |
| 80 | :returns: Relative URI for the resource or object. |
| 81 | |
| 82 | """ |
| 83 | prefix = self.uri_prefix if not permanent else '' |
| 84 | |
| 85 | return '{pref}/{res}{uuid}'.format(pref=prefix, |
| 86 | res=resource_name, |
| 87 | uuid='/%s' % uuid if uuid else '') |
| 88 | |
| 89 | def _make_patch(self, allowed_attributes, **kwargs): |
| 90 | """Create a JSON patch according to RFC 6902. |
| 91 | |
| 92 | :param allowed_attributes: An iterable object that contains a set of |
| 93 | allowed attributes for an object. |
| 94 | :param **kwargs: Attributes and new values for them. |
| 95 | :returns: A JSON path that sets values of the specified attributes to |
| 96 | the new ones. |
| 97 | |
| 98 | """ |
| 99 | def get_change(kwargs, path='/'): |
Luong Anh Tuan | e22fbe1 | 2016-09-12 16:32:14 +0700 | [diff] [blame^] | 100 | for name, value in kwargs.items(): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 101 | if isinstance(value, dict): |
| 102 | for ch in get_change(value, path + '%s/' % name): |
| 103 | yield ch |
| 104 | else: |
| 105 | if value is None: |
| 106 | yield {'path': path + name, |
| 107 | 'op': 'remove'} |
| 108 | else: |
| 109 | yield {'path': path + name, |
| 110 | 'value': value, |
| 111 | 'op': 'replace'} |
| 112 | |
| 113 | patch = [ch for ch in get_change(kwargs) |
| 114 | if ch['path'].lstrip('/') in allowed_attributes] |
| 115 | |
| 116 | return patch |
| 117 | |
| 118 | def _list_request(self, resource, permanent=False, **kwargs): |
| 119 | """Get the list of objects of the specified type. |
| 120 | |
| 121 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 122 | :param **kwargs: Parameters for the request. |
| 123 | :returns: A tuple with the server response and deserialized JSON list |
| 124 | of objects |
| 125 | |
| 126 | """ |
| 127 | uri = self._get_uri(resource, permanent=permanent) |
| 128 | if kwargs: |
| 129 | uri += "?%s" % urllib.urlencode(kwargs) |
| 130 | |
| 131 | resp, body = self.get(uri) |
ghanshyam | 013f611 | 2016-04-21 17:19:06 +0900 | [diff] [blame] | 132 | self.expected_success(200, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 133 | |
| 134 | return resp, self.deserialize(body) |
| 135 | |
| 136 | def _show_request(self, resource, uuid, permanent=False, **kwargs): |
| 137 | """Gets a specific object of the specified type. |
| 138 | |
| 139 | :param uuid: Unique identifier of the object in UUID format. |
| 140 | :returns: Serialized object as a dictionary. |
| 141 | |
| 142 | """ |
| 143 | if 'uri' in kwargs: |
| 144 | uri = kwargs['uri'] |
| 145 | else: |
| 146 | uri = self._get_uri(resource, uuid=uuid, permanent=permanent) |
| 147 | resp, body = self.get(uri) |
ghanshyam | 013f611 | 2016-04-21 17:19:06 +0900 | [diff] [blame] | 148 | self.expected_success(200, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 149 | |
| 150 | return resp, self.deserialize(body) |
| 151 | |
| 152 | def _create_request(self, resource, object_dict): |
| 153 | """Create an object of the specified type. |
| 154 | |
| 155 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 156 | :param object_dict: A Python dict that represents an object of the |
| 157 | specified type. |
| 158 | :returns: A tuple with the server response and the deserialized created |
| 159 | object. |
| 160 | |
| 161 | """ |
| 162 | body = self.serialize(object_dict) |
| 163 | uri = self._get_uri(resource) |
| 164 | |
| 165 | resp, body = self.post(uri, body=body) |
ghanshyam | 013f611 | 2016-04-21 17:19:06 +0900 | [diff] [blame] | 166 | self.expected_success(201, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 167 | |
| 168 | return resp, self.deserialize(body) |
| 169 | |
| 170 | def _delete_request(self, resource, uuid): |
| 171 | """Delete specified object. |
| 172 | |
| 173 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 174 | :param uuid: The unique identifier of an object in UUID format. |
| 175 | :returns: A tuple with the server response and the response body. |
| 176 | |
| 177 | """ |
| 178 | uri = self._get_uri(resource, uuid) |
| 179 | |
| 180 | resp, body = self.delete(uri) |
ghanshyam | 013f611 | 2016-04-21 17:19:06 +0900 | [diff] [blame] | 181 | self.expected_success(204, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 182 | return resp, body |
| 183 | |
| 184 | def _patch_request(self, resource, uuid, patch_object): |
| 185 | """Update specified object with JSON-patch. |
| 186 | |
| 187 | :param resource: The name of the REST resource, e.g., 'nodes'. |
| 188 | :param uuid: The unique identifier of an object in UUID format. |
| 189 | :returns: A tuple with the server response and the serialized patched |
| 190 | object. |
| 191 | |
| 192 | """ |
| 193 | uri = self._get_uri(resource, uuid) |
| 194 | patch_body = json.dumps(patch_object) |
| 195 | |
| 196 | resp, body = self.patch(uri, body=patch_body) |
ghanshyam | 013f611 | 2016-04-21 17:19:06 +0900 | [diff] [blame] | 197 | self.expected_success(200, resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 198 | return resp, self.deserialize(body) |
| 199 | |
| 200 | @handle_errors |
| 201 | def get_api_description(self): |
| 202 | """Retrieves all versions of the Ironic API.""" |
| 203 | |
| 204 | return self._list_request('', permanent=True) |
| 205 | |
| 206 | @handle_errors |
| 207 | def get_version_description(self, version='v1'): |
| 208 | """Retrieves the desctription of the API. |
| 209 | |
| 210 | :param version: The version of the API. Default: 'v1'. |
| 211 | :returns: Serialized description of API resources. |
| 212 | |
| 213 | """ |
| 214 | return self._list_request(version, permanent=True) |
| 215 | |
| 216 | def _put_request(self, resource, put_object): |
| 217 | """Update specified object with JSON-patch.""" |
| 218 | uri = self._get_uri(resource) |
| 219 | put_body = json.dumps(put_object) |
| 220 | |
| 221 | resp, body = self.put(uri, body=put_body) |
ghanshyam | 013f611 | 2016-04-21 17:19:06 +0900 | [diff] [blame] | 222 | self.expected_success([202, 204], resp.status) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 223 | return resp, body |