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