blob: 0b5426e37f08177822eac8a5ca2b4f1130319815 [file] [log] [blame]
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +03001# 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
13import functools
14import json
15
16import six
17
18from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
20
21CONF = config.CONF
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030022
23
24def handle_errors(f):
25 """A decorator that allows to ignore certain types of errors."""
26
27 @functools.wraps(f)
28 def wrapper(*args, **kwargs):
29 param_name = 'ignore_errors'
30 ignored_errors = kwargs.get(param_name, tuple())
31
32 if param_name in kwargs:
33 del kwargs[param_name]
34
35 try:
36 return f(*args, **kwargs)
37 except ignored_errors:
38 # Silently ignore errors
39 pass
40
41 return wrapper
42
43
44class BaremetalClient(rest_client.RestClient):
45 """
46 Base Tempest REST client for Ironic API.
47
48 """
49
Matthew Treinish684d8992014-01-30 16:27:40 +000050 def __init__(self, username, password, auth_url, tenant_name=None):
51 super(BaremetalClient, self).__init__(username, password,
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030052 auth_url, tenant_name)
Matthew Treinish684d8992014-01-30 16:27:40 +000053 self.service = CONF.baremetal.catalog_type
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030054 self.uri_prefix = ''
55
56 def serialize(self, object_type, object_dict):
57 """Serialize an Ironic object."""
58
59 raise NotImplementedError
60
61 def deserialize(self, object_str):
62 """Deserialize an Ironic object."""
63
64 raise NotImplementedError
65
66 def _get_uri(self, resource_name, uuid=None, permanent=False):
67 """
68 Get URI for a specific resource or object.
69
70 :param resource_name: The name of the REST resource, e.g., 'nodes'.
71 :param uuid: The unique identifier of an object in UUID format.
72 :return: Relative URI for the resource or object.
73
74 """
75 prefix = self.uri_prefix if not permanent else ''
76
77 return '{pref}/{res}{uuid}'.format(pref=prefix,
78 res=resource_name,
79 uuid='/%s' % uuid if uuid else '')
80
81 def _make_patch(self, allowed_attributes, **kw):
82 """
83 Create a JSON patch according to RFC 6902.
84
85 :param allowed_attributes: An iterable object that contains a set of
86 allowed attributes for an object.
87 :param **kw: Attributes and new values for them.
88 :return: A JSON path that sets values of the specified attributes to
89 the new ones.
90
91 """
92 def get_change(kw, path='/'):
93 for name, value in six.iteritems(kw):
94 if isinstance(value, dict):
95 for ch in get_change(value, path + '%s/' % name):
96 yield ch
97 else:
98 yield {'path': path + name,
99 'value': value,
100 'op': 'replace'}
101
102 patch = [ch for ch in get_change(kw)
103 if ch['path'].lstrip('/') in allowed_attributes]
104
105 return patch
106
107 def _list_request(self, resource, permanent=False):
108 """
109 Get the list of objects of the specified type.
110
111 :param resource: The name of the REST resource, e.g., 'nodes'.
112 :return: A tuple with the server response and deserialized JSON list
113 of objects
114
115 """
116 uri = self._get_uri(resource, permanent=permanent)
117
118 resp, body = self.get(uri, self.headers)
119
120 return resp, self.deserialize(body)
121
122 def _show_request(self, resource, uuid, permanent=False):
123 """
124 Gets a specific object of the specified type.
125
126 :param uuid: Unique identifier of the object in UUID format.
127 :return: Serialized object as a dictionary.
128
129 """
130 uri = self._get_uri(resource, uuid=uuid, permanent=permanent)
131 resp, body = self.get(uri, self.headers)
132
133 return resp, self.deserialize(body)
134
135 def _create_request(self, resource, object_type, object_dict):
136 """
137 Create an object of the specified type.
138
139 :param resource: The name of the REST resource, e.g., 'nodes'.
140 :param object_dict: A Python dict that represents an object of the
141 specified type.
142 :return: A tuple with the server response and the deserialized created
143 object.
144
145 """
146 body = self.serialize(object_type, object_dict)
147 uri = self._get_uri(resource)
148
149 resp, body = self.post(uri, headers=self.headers, body=body)
150
151 return resp, self.deserialize(body)
152
153 def _delete_request(self, resource, uuid):
154 """
155 Delete specified object.
156
157 :param resource: The name of the REST resource, e.g., 'nodes'.
158 :param uuid: The unique identifier of an object in UUID format.
159 :return: A tuple with the server response and the response body.
160
161 """
162 uri = self._get_uri(resource, uuid)
163
164 resp, body = self.delete(uri, self.headers)
165 return resp, body
166
167 def _patch_request(self, resource, uuid, patch_object):
168 """
169 Update specified object with JSON-patch.
170
171 :param resource: The name of the REST resource, e.g., 'nodes'.
172 :param uuid: The unique identifier of an object in UUID format.
173 :return: A tuple with the server response and the serialized patched
174 object.
175
176 """
177 uri = self._get_uri(resource, uuid)
178 patch_body = json.dumps(patch_object)
179
180 resp, body = self.patch(uri, headers=self.headers, body=patch_body)
181 return resp, self.deserialize(body)
182
183 @handle_errors
184 def get_api_description(self):
185 """Retrieves all versions of the Ironic API."""
186
187 return self._list_request('', permanent=True)
188
189 @handle_errors
190 def get_version_description(self, version='v1'):
191 """
192 Retrieves the desctription of the API.
193
194 :param version: The version of the API. Default: 'v1'.
195 :return: Serialized description of API resources.
196
197 """
198 return self._list_request(version, permanent=True)