blob: 5f6b513ba9280e8b0bf88e492024e572d6bb7ae2 [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
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000050 def __init__(self, auth_provider):
51 super(BaremetalClient, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000052 self.service = CONF.baremetal.catalog_type
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030053 self.uri_prefix = ''
54
55 def serialize(self, object_type, object_dict):
56 """Serialize an Ironic object."""
57
58 raise NotImplementedError
59
60 def deserialize(self, object_str):
61 """Deserialize an Ironic object."""
62
63 raise NotImplementedError
64
65 def _get_uri(self, resource_name, uuid=None, permanent=False):
66 """
67 Get URI for a specific resource or object.
68
69 :param resource_name: The name of the REST resource, e.g., 'nodes'.
70 :param uuid: The unique identifier of an object in UUID format.
71 :return: Relative URI for the resource or object.
72
73 """
74 prefix = self.uri_prefix if not permanent else ''
75
76 return '{pref}/{res}{uuid}'.format(pref=prefix,
77 res=resource_name,
78 uuid='/%s' % uuid if uuid else '')
79
80 def _make_patch(self, allowed_attributes, **kw):
81 """
82 Create a JSON patch according to RFC 6902.
83
84 :param allowed_attributes: An iterable object that contains a set of
85 allowed attributes for an object.
86 :param **kw: Attributes and new values for them.
87 :return: A JSON path that sets values of the specified attributes to
88 the new ones.
89
90 """
91 def get_change(kw, path='/'):
92 for name, value in six.iteritems(kw):
93 if isinstance(value, dict):
94 for ch in get_change(value, path + '%s/' % name):
95 yield ch
96 else:
97 yield {'path': path + name,
98 'value': value,
99 'op': 'replace'}
100
101 patch = [ch for ch in get_change(kw)
102 if ch['path'].lstrip('/') in allowed_attributes]
103
104 return patch
105
106 def _list_request(self, resource, permanent=False):
107 """
108 Get the list of objects of the specified type.
109
110 :param resource: The name of the REST resource, e.g., 'nodes'.
111 :return: A tuple with the server response and deserialized JSON list
112 of objects
113
114 """
115 uri = self._get_uri(resource, permanent=permanent)
116
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200117 resp, body = self.get(uri)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300118
119 return resp, self.deserialize(body)
120
121 def _show_request(self, resource, uuid, permanent=False):
122 """
123 Gets a specific object of the specified type.
124
125 :param uuid: Unique identifier of the object in UUID format.
126 :return: Serialized object as a dictionary.
127
128 """
129 uri = self._get_uri(resource, uuid=uuid, permanent=permanent)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200130 resp, body = self.get(uri)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300131
132 return resp, self.deserialize(body)
133
134 def _create_request(self, resource, object_type, object_dict):
135 """
136 Create an object of the specified type.
137
138 :param resource: The name of the REST resource, e.g., 'nodes'.
139 :param object_dict: A Python dict that represents an object of the
140 specified type.
141 :return: A tuple with the server response and the deserialized created
142 object.
143
144 """
145 body = self.serialize(object_type, object_dict)
146 uri = self._get_uri(resource)
147
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200148 resp, body = self.post(uri, body=body)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300149
150 return resp, self.deserialize(body)
151
152 def _delete_request(self, resource, uuid):
153 """
154 Delete specified object.
155
156 :param resource: The name of the REST resource, e.g., 'nodes'.
157 :param uuid: The unique identifier of an object in UUID format.
158 :return: A tuple with the server response and the response body.
159
160 """
161 uri = self._get_uri(resource, uuid)
162
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200163 resp, body = self.delete(uri)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300164 return resp, body
165
166 def _patch_request(self, resource, uuid, patch_object):
167 """
168 Update specified object with JSON-patch.
169
170 :param resource: The name of the REST resource, e.g., 'nodes'.
171 :param uuid: The unique identifier of an object in UUID format.
172 :return: A tuple with the server response and the serialized patched
173 object.
174
175 """
176 uri = self._get_uri(resource, uuid)
177 patch_body = json.dumps(patch_object)
178
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200179 resp, body = self.patch(uri, body=patch_body)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300180 return resp, self.deserialize(body)
181
182 @handle_errors
183 def get_api_description(self):
184 """Retrieves all versions of the Ironic API."""
185
186 return self._list_request('', permanent=True)
187
188 @handle_errors
189 def get_version_description(self, version='v1'):
190 """
191 Retrieves the desctription of the API.
192
193 :param version: The version of the API. Default: 'v1'.
194 :return: Serialized description of API resources.
195
196 """
197 return self._list_request(version, permanent=True)