blob: 46c0a2fc4ef782335ee7f862a725a9b64f0c36f3 [file] [log] [blame]
Yuiko Takadab6527002015-12-07 11:49:12 +09001# 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 Takadab6527002015-12-07 11:49:12 +090013
14from oslo_serialization import jsonutils as json
John L. Villalovosd88fe9f2018-02-01 16:24:43 -080015import six
Solio Sarabiaf78122e2017-02-23 16:17:34 -060016from six.moves import http_client
Yuiko Takadab6527002015-12-07 11:49:12 +090017from six.moves.urllib import parse as urllib
Yuiko Takadaff785002015-12-17 15:56:42 +090018from tempest.lib.common import api_version_utils
Lenny Verkhovsky88625042016-03-08 17:44:00 +020019from tempest.lib.common import rest_client
Yuiko Takadab6527002015-12-07 11:49:12 +090020
Vasyl Saienko4ddbeec2017-01-20 16:26:04 +000021# NOTE(vsaienko): concurrent tests work because they are launched in
22# separate processes so global variables are not shared among them.
Yuiko Takadaff785002015-12-17 15:56:42 +090023BAREMETAL_MICROVERSION = None
24
Yuiko Takadab6527002015-12-07 11:49:12 +090025
Vasyl Saienko4ddbeec2017-01-20 16:26:04 +000026def set_baremetal_api_microversion(baremetal_microversion):
27 global BAREMETAL_MICROVERSION
28 BAREMETAL_MICROVERSION = baremetal_microversion
29
30
31def reset_baremetal_api_microversion():
32 global BAREMETAL_MICROVERSION
33 BAREMETAL_MICROVERSION = None
34
35
Yuiko Takadab6527002015-12-07 11:49:12 +090036def handle_errors(f):
37 """A decorator that allows to ignore certain types of errors."""
38
John L. Villalovosd88fe9f2018-02-01 16:24:43 -080039 @six.wraps(f)
Yuiko Takadab6527002015-12-07 11:49:12 +090040 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
56class BaremetalClient(rest_client.RestClient):
57 """Base Tempest REST client for Ironic API."""
58
Yuiko Takadaff785002015-12-17 15:56:42 +090059 api_microversion_header_name = 'X-OpenStack-Ironic-API-Version'
Yuiko Takadab6527002015-12-07 11:49:12 +090060 uri_prefix = ''
61
Yuiko Takadaff785002015-12-17 15:56:42 +090062 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 Saienkof20979c2016-05-27 11:25:01 +030068 def request(self, *args, **kwargs):
69 resp, resp_body = super(BaremetalClient, self).request(*args, **kwargs)
Yuiko Takadaff785002015-12-17 15:56:42 +090070 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 Takadab6527002015-12-07 11:49:12 +090078 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
Dmitry Tantsure7548052018-07-16 17:48:32 +020088 def _get_uri(self, resource_name, uuid=None, permanent=False,
89 params=None):
Yuiko Takadab6527002015-12-07 11:49:12 +090090 """Get URI for a specific resource or object.
91
92 :param resource_name: The name of the REST resource, e.g., 'nodes'.
93 :param uuid: The unique identifier of an object in UUID format.
94 :returns: Relative URI for the resource or object.
95
96 """
97 prefix = self.uri_prefix if not permanent else ''
Dmitry Tantsure7548052018-07-16 17:48:32 +020098 if params:
99 params = '?' + '&'.join('%s=%s' % tpl for tpl in params.items())
100 else:
101 params = ''
Yuiko Takadab6527002015-12-07 11:49:12 +0900102
Dmitry Tantsure7548052018-07-16 17:48:32 +0200103 return '{pref}/{res}{uuid}{params}'.format(
104 pref=prefix, res=resource_name,
105 uuid='/%s' % uuid if uuid else '',
106 params=params)
Yuiko Takadab6527002015-12-07 11:49:12 +0900107
108 def _make_patch(self, allowed_attributes, **kwargs):
109 """Create a JSON patch according to RFC 6902.
110
111 :param allowed_attributes: An iterable object that contains a set of
112 allowed attributes for an object.
113 :param **kwargs: Attributes and new values for them.
114 :returns: A JSON path that sets values of the specified attributes to
115 the new ones.
116
117 """
118 def get_change(kwargs, path='/'):
Luong Anh Tuane22fbe12016-09-12 16:32:14 +0700119 for name, value in kwargs.items():
Yuiko Takadab6527002015-12-07 11:49:12 +0900120 if isinstance(value, dict):
121 for ch in get_change(value, path + '%s/' % name):
122 yield ch
123 else:
124 if value is None:
125 yield {'path': path + name,
126 'op': 'remove'}
127 else:
128 yield {'path': path + name,
129 'value': value,
130 'op': 'replace'}
131
132 patch = [ch for ch in get_change(kwargs)
133 if ch['path'].lstrip('/') in allowed_attributes]
134
135 return patch
136
Sam Betts462e9e62016-11-30 18:43:35 +0000137 def _list_request(self, resource, permanent=False, headers=None,
138 extra_headers=False, **kwargs):
Yuiko Takadab6527002015-12-07 11:49:12 +0900139 """Get the list of objects of the specified type.
140
141 :param resource: The name of the REST resource, e.g., 'nodes'.
Sam Betts462e9e62016-11-30 18:43:35 +0000142 :param headers: List of headers to use in request.
143 :param extra_headers: Specify whether to use headers.
Yuiko Takadab6527002015-12-07 11:49:12 +0900144 :param **kwargs: Parameters for the request.
145 :returns: A tuple with the server response and deserialized JSON list
146 of objects
147
148 """
149 uri = self._get_uri(resource, permanent=permanent)
150 if kwargs:
151 uri += "?%s" % urllib.urlencode(kwargs)
152
Sam Betts462e9e62016-11-30 18:43:35 +0000153 resp, body = self.get(uri, headers=headers,
154 extra_headers=extra_headers)
Solio Sarabiaf78122e2017-02-23 16:17:34 -0600155 self.expected_success(http_client.OK, resp.status)
Yuiko Takadab6527002015-12-07 11:49:12 +0900156
157 return resp, self.deserialize(body)
158
SofiiaAndriichenko569f0a42016-12-08 05:02:17 -0500159 def _show_request(self,
160 resource,
161 uuid=None,
162 permanent=False,
Mark Goddard56399cc2018-02-16 13:37:25 +0000163 headers=None,
164 extra_headers=False,
SofiiaAndriichenko569f0a42016-12-08 05:02:17 -0500165 **kwargs):
Yuiko Takadab6527002015-12-07 11:49:12 +0900166 """Gets a specific object of the specified type.
167
168 :param uuid: Unique identifier of the object in UUID format.
Mark Goddard56399cc2018-02-16 13:37:25 +0000169 :param headers: List of headers to use in request.
170 :param extra_headers: Specify whether to use headers.
Yuiko Takadab6527002015-12-07 11:49:12 +0900171 :returns: Serialized object as a dictionary.
172
173 """
174 if 'uri' in kwargs:
175 uri = kwargs['uri']
176 else:
177 uri = self._get_uri(resource, uuid=uuid, permanent=permanent)
Mark Goddard56399cc2018-02-16 13:37:25 +0000178 resp, body = self.get(uri, headers=headers,
179 extra_headers=extra_headers)
Solio Sarabiaf78122e2017-02-23 16:17:34 -0600180 self.expected_success(http_client.OK, resp.status)
Yuiko Takadab6527002015-12-07 11:49:12 +0900181
182 return resp, self.deserialize(body)
183
184 def _create_request(self, resource, object_dict):
185 """Create an object of the specified type.
186
187 :param resource: The name of the REST resource, e.g., 'nodes'.
188 :param object_dict: A Python dict that represents an object of the
189 specified type.
190 :returns: A tuple with the server response and the deserialized created
191 object.
192
193 """
194 body = self.serialize(object_dict)
195 uri = self._get_uri(resource)
196
197 resp, body = self.post(uri, body=body)
Solio Sarabiaf78122e2017-02-23 16:17:34 -0600198 self.expected_success(http_client.CREATED, resp.status)
Yuiko Takadab6527002015-12-07 11:49:12 +0900199
200 return resp, self.deserialize(body)
201
Sam Betts462e9e62016-11-30 18:43:35 +0000202 def _create_request_no_response_body(self, resource, object_dict):
203 """Create an object of the specified type.
204
205 Do not expect any body in the response.
206
207 :param resource: The name of the REST resource, e.g., 'nodes'.
208 :param object_dict: A Python dict that represents an object of the
209 specified type.
210 :returns: The server response.
211 """
212
213 body = self.serialize(object_dict)
214 uri = self._get_uri(resource)
215
216 resp, body = self.post(uri, body=body)
Solio Sarabiaf78122e2017-02-23 16:17:34 -0600217 self.expected_success(http_client.NO_CONTENT, resp.status)
Sam Betts462e9e62016-11-30 18:43:35 +0000218
219 return resp
220
Yolanda Roblaabd90112018-05-15 17:11:54 +0200221 def _delete_request(self, resource, uuid,
222 expected_status=http_client.NO_CONTENT):
Yuiko Takadab6527002015-12-07 11:49:12 +0900223 """Delete specified object.
224
225 :param resource: The name of the REST resource, e.g., 'nodes'.
226 :param uuid: The unique identifier of an object in UUID format.
Yolanda Roblaabd90112018-05-15 17:11:54 +0200227 :param expected_status: Expected response status code. By default is
228 http_client.NO_CONTENT (204)
Yuiko Takadab6527002015-12-07 11:49:12 +0900229 :returns: A tuple with the server response and the response body.
230
231 """
232 uri = self._get_uri(resource, uuid)
233
234 resp, body = self.delete(uri)
Yolanda Roblaabd90112018-05-15 17:11:54 +0200235 self.expected_success(expected_status, resp.status)
Yuiko Takadab6527002015-12-07 11:49:12 +0900236 return resp, body
237
Dmitry Tantsure7548052018-07-16 17:48:32 +0200238 def _patch_request(self, resource, uuid, patch_object, params=None):
Yuiko Takadab6527002015-12-07 11:49:12 +0900239 """Update specified object with JSON-patch.
240
241 :param resource: The name of the REST resource, e.g., 'nodes'.
242 :param uuid: The unique identifier of an object in UUID format.
Dmitry Tantsure7548052018-07-16 17:48:32 +0200243 :param params: query parameters to pass.
Yuiko Takadab6527002015-12-07 11:49:12 +0900244 :returns: A tuple with the server response and the serialized patched
245 object.
246
247 """
Dmitry Tantsure7548052018-07-16 17:48:32 +0200248 uri = self._get_uri(resource, uuid, params=params)
Yuiko Takadab6527002015-12-07 11:49:12 +0900249 patch_body = json.dumps(patch_object)
250
251 resp, body = self.patch(uri, body=patch_body)
Solio Sarabiaf78122e2017-02-23 16:17:34 -0600252 self.expected_success(http_client.OK, resp.status)
Yuiko Takadab6527002015-12-07 11:49:12 +0900253 return resp, self.deserialize(body)
254
255 @handle_errors
256 def get_api_description(self):
257 """Retrieves all versions of the Ironic API."""
258
259 return self._list_request('', permanent=True)
260
261 @handle_errors
262 def get_version_description(self, version='v1'):
Kyrylo Romanenko36000542016-09-16 15:07:40 +0300263 """Retrieves the description of the API.
Yuiko Takadab6527002015-12-07 11:49:12 +0900264
265 :param version: The version of the API. Default: 'v1'.
266 :returns: Serialized description of API resources.
267
268 """
269 return self._list_request(version, permanent=True)
270
271 def _put_request(self, resource, put_object):
272 """Update specified object with JSON-patch."""
273 uri = self._get_uri(resource)
274 put_body = json.dumps(put_object)
275
276 resp, body = self.put(uri, body=put_body)
Solio Sarabiaf78122e2017-02-23 16:17:34 -0600277 self.expected_success([http_client.ACCEPTED, http_client.NO_CONTENT],
278 resp.status)
Yuiko Takadab6527002015-12-07 11:49:12 +0900279 return resp, body