blob: 55b6be6d7dfffea7ae58352a583288772ad0b7fb [file] [log] [blame]
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +04001# Copyright (c) 2013 Mirantis Inc.
2#
Yaroslav Lobankov18544b02014-04-24 20:17:49 +04003# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +04006#
Yaroslav Lobankov18544b02014-04-24 20:17:49 +04007# http://www.apache.org/licenses/LICENSE-2.0
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +04008#
Yaroslav Lobankov18544b02014-04-24 20:17:49 +04009# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040014
15import json
16
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000017from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000018from tempest import config
19
20CONF = config.CONF
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040021
22
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000023class DataProcessingClient(service_client.ServiceClient):
Ken'ichi Ohmichi0cd316b2014-12-24 03:51:04 +000024
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000025 def __init__(self, auth_provider):
Ken'ichi Ohmichi0cd316b2014-12-24 03:51:04 +000026 super(DataProcessingClient, self).__init__(
Ken'ichi Ohmichie9f50412015-01-05 04:57:26 +000027 auth_provider,
28 CONF.data_processing.catalog_type,
29 CONF.identity.region,
Ken'ichi Ohmichi0690ea42015-01-02 07:03:51 +000030 endpoint_type=CONF.data_processing.endpoint_type)
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040031
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040032 def _request_and_check_resp(self, request_func, uri, resp_status):
33 """Make a request using specified request_func and check response
34 status code.
35
36 It returns pair: resp and response body.
37 """
38 resp, body = request_func(uri)
39 self.expected_success(resp_status, resp.status)
40 return resp, body
41
42 def _request_check_and_parse_resp(self, request_func, uri, resp_status,
43 resource_name, *args, **kwargs):
44 """Make a request using specified request_func, check response status
45 code and parse response body.
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040046
47 It returns pair: resp and parsed resource(s) body.
48 """
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040049 headers = {'Content-Type': 'application/json'}
50 resp, body = request_func(uri, headers=headers, *args, **kwargs)
51 self.expected_success(resp_status, resp.status)
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040052 body = json.loads(body)
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040053 return resp, body[resource_name]
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040054
55 def list_node_group_templates(self):
56 """List all node group templates for a user."""
57
58 uri = 'node-group-templates'
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040059 return self._request_check_and_parse_resp(self.get, uri,
60 200, 'node_group_templates')
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040061
62 def get_node_group_template(self, tmpl_id):
63 """Returns the details of a single node group template."""
64
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +040065 uri = 'node-group-templates/%s' % tmpl_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040066 return self._request_check_and_parse_resp(self.get, uri,
67 200, 'node_group_template')
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040068
69 def create_node_group_template(self, name, plugin_name, hadoop_version,
70 node_processes, flavor_id,
71 node_configs=None, **kwargs):
72 """Creates node group template with specified params.
73
74 It supports passing additional params using kwargs and returns created
75 object.
76 """
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +040077 uri = 'node-group-templates'
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040078 body = kwargs.copy()
79 body.update({
80 'name': name,
81 'plugin_name': plugin_name,
82 'hadoop_version': hadoop_version,
83 'node_processes': node_processes,
84 'flavor_id': flavor_id,
85 'node_configs': node_configs or dict(),
86 })
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040087 return self._request_check_and_parse_resp(self.post, uri, 202,
88 'node_group_template',
89 body=json.dumps(body))
Sergey Lukjanov3bc60e92013-12-10 16:40:50 +040090
91 def delete_node_group_template(self, tmpl_id):
92 """Deletes the specified node group template by id."""
93
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +040094 uri = 'node-group-templates/%s' % tmpl_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +040095 return self._request_and_check_resp(self.delete, uri, 204)
Sergey Lukjanovdad26cc2014-02-06 19:58:31 +040096
97 def list_plugins(self):
98 """List all enabled plugins."""
99
100 uri = 'plugins'
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400101 return self._request_check_and_parse_resp(self.get,
102 uri, 200, 'plugins')
Sergey Lukjanovdad26cc2014-02-06 19:58:31 +0400103
104 def get_plugin(self, plugin_name, plugin_version=None):
105 """Returns the details of a single plugin."""
106
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +0400107 uri = 'plugins/%s' % plugin_name
Sergey Lukjanovdad26cc2014-02-06 19:58:31 +0400108 if plugin_version:
109 uri += '/%s' % plugin_version
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400110 return self._request_check_and_parse_resp(self.get, uri, 200, 'plugin')
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +0400111
112 def list_cluster_templates(self):
113 """List all cluster templates for a user."""
114
115 uri = 'cluster-templates'
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400116 return self._request_check_and_parse_resp(self.get, uri,
117 200, 'cluster_templates')
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +0400118
119 def get_cluster_template(self, tmpl_id):
120 """Returns the details of a single cluster template."""
121
122 uri = 'cluster-templates/%s' % tmpl_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400123 return self._request_check_and_parse_resp(self.get,
124 uri, 200, 'cluster_template')
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +0400125
126 def create_cluster_template(self, name, plugin_name, hadoop_version,
127 node_groups, cluster_configs=None,
128 **kwargs):
129 """Creates cluster template with specified params.
130
131 It supports passing additional params using kwargs and returns created
132 object.
133 """
134 uri = 'cluster-templates'
135 body = kwargs.copy()
136 body.update({
137 'name': name,
138 'plugin_name': plugin_name,
139 'hadoop_version': hadoop_version,
140 'node_groups': node_groups,
141 'cluster_configs': cluster_configs or dict(),
142 })
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400143 return self._request_check_and_parse_resp(self.post, uri, 202,
144 'cluster_template',
145 body=json.dumps(body))
Yaroslav Lobankov93aa8192014-04-01 20:03:25 +0400146
147 def delete_cluster_template(self, tmpl_id):
148 """Deletes the specified cluster template by id."""
149
150 uri = 'cluster-templates/%s' % tmpl_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400151 return self._request_and_check_resp(self.delete, uri, 204)
Yaroslav Lobankov4267bcc2014-04-25 13:25:03 +0400152
153 def list_data_sources(self):
154 """List all data sources for a user."""
155
156 uri = 'data-sources'
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400157 return self._request_check_and_parse_resp(self.get,
158 uri, 200, 'data_sources')
Yaroslav Lobankov4267bcc2014-04-25 13:25:03 +0400159
160 def get_data_source(self, source_id):
161 """Returns the details of a single data source."""
162
163 uri = 'data-sources/%s' % source_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400164 return self._request_check_and_parse_resp(self.get,
165 uri, 200, 'data_source')
Yaroslav Lobankov4267bcc2014-04-25 13:25:03 +0400166
167 def create_data_source(self, name, data_source_type, url, **kwargs):
168 """Creates data source with specified params.
169
170 It supports passing additional params using kwargs and returns created
171 object.
172 """
173 uri = 'data-sources'
174 body = kwargs.copy()
175 body.update({
176 'name': name,
177 'type': data_source_type,
178 'url': url
179 })
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400180 return self._request_check_and_parse_resp(self.post, uri,
181 202, 'data_source',
182 body=json.dumps(body))
Yaroslav Lobankov4267bcc2014-04-25 13:25:03 +0400183
184 def delete_data_source(self, source_id):
185 """Deletes the specified data source by id."""
186
187 uri = 'data-sources/%s' % source_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400188 return self._request_and_check_resp(self.delete, uri, 204)
Yaroslav Lobankovd5dcf192014-05-21 13:58:10 +0400189
190 def list_job_binary_internals(self):
191 """List all job binary internals for a user."""
192
193 uri = 'job-binary-internals'
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400194 return self._request_check_and_parse_resp(self.get,
195 uri, 200, 'binaries')
Yaroslav Lobankovd5dcf192014-05-21 13:58:10 +0400196
197 def get_job_binary_internal(self, job_binary_id):
198 """Returns the details of a single job binary internal."""
199
200 uri = 'job-binary-internals/%s' % job_binary_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400201 return self._request_check_and_parse_resp(self.get, uri,
202 200, 'job_binary_internal')
Yaroslav Lobankovd5dcf192014-05-21 13:58:10 +0400203
204 def create_job_binary_internal(self, name, data):
205 """Creates job binary internal with specified params."""
206
207 uri = 'job-binary-internals/%s' % name
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400208 return self._request_check_and_parse_resp(self.put, uri, 202,
209 'job_binary_internal', data)
Yaroslav Lobankovd5dcf192014-05-21 13:58:10 +0400210
211 def delete_job_binary_internal(self, job_binary_id):
212 """Deletes the specified job binary internal by id."""
213
214 uri = 'job-binary-internals/%s' % job_binary_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400215 return self._request_and_check_resp(self.delete, uri, 204)
Yaroslav Lobankov889c0e22014-05-21 18:00:16 +0400216
217 def get_job_binary_internal_data(self, job_binary_id):
218 """Returns data of a single job binary internal."""
219
220 uri = 'job-binary-internals/%s/data' % job_binary_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400221 return self._request_and_check_resp(self.get, uri, 200)
Yaroslav Lobankov74c923b2014-05-21 13:13:07 +0400222
223 def list_job_binaries(self):
224 """List all job binaries for a user."""
225
226 uri = 'job-binaries'
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400227 return self._request_check_and_parse_resp(self.get,
228 uri, 200, 'binaries')
Yaroslav Lobankov74c923b2014-05-21 13:13:07 +0400229
230 def get_job_binary(self, job_binary_id):
231 """Returns the details of a single job binary."""
232
233 uri = 'job-binaries/%s' % job_binary_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400234 return self._request_check_and_parse_resp(self.get,
235 uri, 200, 'job_binary')
Yaroslav Lobankov74c923b2014-05-21 13:13:07 +0400236
237 def create_job_binary(self, name, url, extra=None, **kwargs):
238 """Creates job binary with specified params.
239
240 It supports passing additional params using kwargs and returns created
241 object.
242 """
243 uri = 'job-binaries'
244 body = kwargs.copy()
245 body.update({
246 'name': name,
247 'url': url,
248 'extra': extra or dict(),
249 })
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400250 return self._request_check_and_parse_resp(self.post, uri,
251 202, 'job_binary',
252 body=json.dumps(body))
Yaroslav Lobankov74c923b2014-05-21 13:13:07 +0400253
254 def delete_job_binary(self, job_binary_id):
255 """Deletes the specified job binary by id."""
256
257 uri = 'job-binaries/%s' % job_binary_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400258 return self._request_and_check_resp(self.delete, uri, 204)
Yaroslav Lobankov74c923b2014-05-21 13:13:07 +0400259
260 def get_job_binary_data(self, job_binary_id):
261 """Returns data of a single job binary."""
262
263 uri = 'job-binaries/%s/data' % job_binary_id
Yaroslav Lobankov2f8525e2014-07-21 16:40:23 +0400264 return self._request_and_check_resp(self.get, uri, 200)
Yaroslav Lobankovc04ae232014-07-04 16:13:55 +0400265
266 def list_jobs(self):
267 """List all jobs for a user."""
268
269 uri = 'jobs'
270 return self._request_check_and_parse_resp(self.get, uri, 200, 'jobs')
271
272 def get_job(self, job_id):
273 """Returns the details of a single job."""
274
275 uri = 'jobs/%s' % job_id
276 return self._request_check_and_parse_resp(self.get, uri, 200, 'job')
277
278 def create_job(self, name, job_type, mains, libs=None, **kwargs):
279 """Creates job with specified params.
280
281 It supports passing additional params using kwargs and returns created
282 object.
283 """
284 uri = 'jobs'
285 body = kwargs.copy()
286 body.update({
287 'name': name,
288 'type': job_type,
289 'mains': mains,
290 'libs': libs or list(),
291 })
292 return self._request_check_and_parse_resp(self.post, uri, 202,
293 'job', body=json.dumps(body))
294
295 def delete_job(self, job_id):
296 """Deletes the specified job by id."""
297
298 uri = 'jobs/%s' % job_id
299 return self._request_and_check_resp(self.delete, uri, 204)