summaryrefslogtreecommitdiff
path: root/watcher_tempest_plugin/services/infra_optim/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'watcher_tempest_plugin/services/infra_optim/base.py')
-rw-r--r--watcher_tempest_plugin/services/infra_optim/base.py211
1 files changed, 0 insertions, 211 deletions
diff --git a/watcher_tempest_plugin/services/infra_optim/base.py b/watcher_tempest_plugin/services/infra_optim/base.py
deleted file mode 100644
index d248774..0000000
--- a/watcher_tempest_plugin/services/infra_optim/base.py
+++ /dev/null
@@ -1,211 +0,0 @@
1# -*- encoding: utf-8 -*-
2# Copyright (c) 2016 b<>com
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import abc
18import functools
19
20import six
21import six.moves.urllib.parse as urlparse
22
23from tempest.lib.common import rest_client
24
25
26def handle_errors(f):
27 """A decorator that allows to ignore certain types of errors."""
28
29 @functools.wraps(f)
30 def wrapper(*args, **kwargs):
31 param_name = 'ignore_errors'
32 ignored_errors = kwargs.get(param_name, tuple())
33
34 if param_name in kwargs:
35 del kwargs[param_name]
36
37 try:
38 return f(*args, **kwargs)
39 except ignored_errors:
40 # Silently ignore errors
41 pass
42
43 return wrapper
44
45
46@six.add_metaclass(abc.ABCMeta)
47class BaseInfraOptimClient(rest_client.RestClient):
48 """Base Tempest REST client for Watcher API."""
49
50 URI_PREFIX = ''
51
52 @abc.abstractmethod
53 def serialize(self, object_dict):
54 """Serialize an Watcher object."""
55 raise NotImplementedError()
56
57 @abc.abstractmethod
58 def deserialize(self, object_str):
59 """Deserialize an Watcher object."""
60 raise NotImplementedError()
61
62 def _get_uri(self, resource_name, uuid=None, permanent=False):
63 """Get URI for a specific resource or object.
64
65 :param resource_name: The name of the REST resource, e.g., 'audits'.
66 :param uuid: The unique identifier of an object in UUID format.
67 :return: Relative URI for the resource or object.
68 """
69
70 prefix = self.URI_PREFIX if not permanent else ''
71
72 return '{pref}/{res}{uuid}'.format(pref=prefix,
73 res=resource_name,
74 uuid='/%s' % uuid if uuid else '')
75
76 def _make_patch(self, allowed_attributes, **kw):
77 """Create a JSON patch according to RFC 6902.
78
79 :param allowed_attributes: An iterable object that contains a set of
80 allowed attributes for an object.
81 :param **kw: Attributes and new values for them.
82 :return: A JSON path that sets values of the specified attributes to
83 the new ones.
84 """
85
86 def get_change(kw, path='/'):
87 for name, value in kw.items():
88 if isinstance(value, dict):
89 for ch in get_change(value, path + '%s/' % name):
90 yield ch
91 else:
92 if value is None:
93 yield {'path': path + name,
94 'op': 'remove'}
95 else:
96 yield {'path': path + name,
97 'value': value,
98 'op': 'replace'}
99
100 patch = [ch for ch in get_change(kw)
101 if ch['path'].lstrip('/') in allowed_attributes]
102
103 return patch
104
105 def _list_request(self, resource, permanent=False, **kwargs):
106 """Get the list of objects of the specified type.
107
108 :param resource: The name of the REST resource, e.g., 'audits'.
109 "param **kw: Parameters for the request.
110 :return: A tuple with the server response and deserialized JSON list
111 of objects
112 """
113
114 uri = self._get_uri(resource, permanent=permanent)
115 if kwargs:
116 uri += "?%s" % urlparse.urlencode(kwargs)
117
118 resp, body = self.get(uri)
119 self.expected_success(200, int(resp['status']))
120
121 return resp, self.deserialize(body)
122
123 def _show_request(self, resource, uuid, permanent=False, **kwargs):
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 if 'uri' in kwargs:
131 uri = kwargs['uri']
132 else:
133 uri = self._get_uri(resource, uuid=uuid, permanent=permanent)
134 resp, body = self.get(uri)
135 self.expected_success(200, int(resp['status']))
136
137 return resp, self.deserialize(body)
138
139 def _create_request(self, resource, object_dict):
140 """Create an object of the specified type.
141
142 :param resource: The name of the REST resource, e.g., 'audits'.
143 :param object_dict: A Python dict that represents an object of the
144 specified type.
145 :return: A tuple with the server response and the deserialized created
146 object.
147 """
148
149 body = self.serialize(object_dict)
150 uri = self._get_uri(resource)
151
152 resp, body = self.post(uri, body=body)
153 self.expected_success(201, int(resp['status']))
154
155 return resp, self.deserialize(body)
156
157 def _delete_request(self, resource, uuid):
158 """Delete specified object.
159
160 :param resource: The name of the REST resource, e.g., 'audits'.
161 :param uuid: The unique identifier of an object in UUID format.
162 :return: A tuple with the server response and the response body.
163 """
164
165 uri = self._get_uri(resource, uuid)
166
167 resp, body = self.delete(uri)
168 self.expected_success(204, int(resp['status']))
169 return resp, body
170
171 def _patch_request(self, resource, uuid, patch_object):
172 """Update specified object with JSON-patch.
173
174 :param resource: The name of the REST resource, e.g., 'audits'.
175 :param uuid: The unique identifier of an object in UUID format.
176 :return: A tuple with the server response and the serialized patched
177 object.
178 """
179
180 uri = self._get_uri(resource, uuid)
181 patch_body = self.serialize(patch_object)
182
183 resp, body = self.patch(uri, body=patch_body)
184 self.expected_success(200, int(resp['status']))
185 return resp, self.deserialize(body)
186
187 @handle_errors
188 def get_api_description(self):
189 """Retrieves all versions of the Watcher API."""
190
191 return self._list_request('', permanent=True)
192
193 @handle_errors
194 def get_version_description(self, version='v1'):
195 """Retrieves the description of the API.
196
197 :param version: The version of the API. Default: 'v1'.
198 :return: Serialized description of API resources.
199 """
200
201 return self._list_request(version, permanent=True)
202
203 def _put_request(self, resource, put_object):
204 """Update specified object with JSON-patch."""
205
206 uri = self._get_uri(resource)
207 put_body = self.serialize(put_object)
208
209 resp, body = self.put(uri, body=put_body)
210 self.expected_success(202, int(resp['status']))
211 return resp, body