Merge "Plugin types are not exposed to the client." into stable/kilo
Jenkins authored 9 years ago
Gerrit Code Review committed 9 years ago
2 | 2 | "default": "", |
3 | 3 | |
4 | 4 | "catalog_index": "role:admin", |
5 | "catalog_search": "" | |
5 | "catalog_search": "", | |
6 | "catalog_plugins": "" | |
6 | 7 | } |
689 | 689 | self.policy.enforce(self.context, 'catalog_search', {}) |
690 | 690 | return self.search_repo.search(*args, **kwargs) |
691 | 691 | |
692 | def plugins_info(self, *args, **kwargs): | |
693 | self.policy.enforce(self.context, 'catalog_plugins', {}) | |
694 | return self.search_repo.plugins_info(*args, **kwargs) | |
695 | ||
692 | 696 | def index(self, *args, **kwargs): |
693 | 697 | self.policy.enforce(self.context, 'catalog_index', {}) |
694 | 698 | return self.search_repo.index(*args, **kwargs) |
31 | 31 | import os |
32 | 32 | import platform |
33 | 33 | import re |
34 | import stevedore | |
34 | 35 | import subprocess |
35 | 36 | import sys |
36 | 37 | import uuid |
728 | 729 | conf['cert_file'] = CONF.cert_file |
729 | 730 | |
730 | 731 | return conf |
732 | ||
733 | ||
734 | def get_search_plugins(): | |
735 | namespace = 'glance.search.index_backend' | |
736 | ext_manager = stevedore.extension.ExtensionManager( | |
737 | namespace, invoke_on_load=True) | |
738 | return ext_manager.extensions |
15 | 15 | import elasticsearch |
16 | 16 | from elasticsearch import helpers |
17 | 17 | from oslo_config import cfg |
18 | ||
19 | from glance.common import utils | |
18 | 20 | |
19 | 21 | |
20 | 22 | search_opts = [ |
39 | 41 | def __init__(self, context, es_api): |
40 | 42 | self.context = context |
41 | 43 | self.es_api = es_api |
44 | self.plugins = utils.get_search_plugins() or [] | |
45 | self.plugins_info_dict = self._get_plugin_info() | |
42 | 46 | |
43 | 47 | def search(self, index, doc_type, query, fields, offset, limit, |
44 | 48 | ignore_unavailable=True): |
57 | 61 | index=default_index, |
58 | 62 | doc_type=default_type, |
59 | 63 | actions=actions) |
64 | ||
65 | def plugins_info(self): | |
66 | return self.plugins_info_dict | |
67 | ||
68 | def _get_plugin_info(self): | |
69 | plugin_info = dict() | |
70 | plugin_info['plugins'] = [] | |
71 | for plugin in self.plugins: | |
72 | info = dict() | |
73 | info['type'] = plugin.obj.get_document_type() | |
74 | info['index'] = plugin.obj.get_index_name() | |
75 | plugin_info['plugins'].append(info) | |
76 | return plugin_info |
40 | 40 | conditions={'method': ['PUT', 'DELETE', |
41 | 41 | 'PATCH', 'HEAD']}) |
42 | 42 | |
43 | mapper.connect('/search/plugins', | |
44 | controller=search_catalog_resource, | |
45 | action='plugins_info', | |
46 | conditions={'method': ['GET']}) | |
47 | mapper.connect('/search/plugins', | |
48 | controller=reject_method_resource, | |
49 | action='reject', | |
50 | allowed_methods='GET', | |
51 | conditions={'method': ['POST', 'PUT', 'DELETE', | |
52 | 'PATCH', 'HEAD']}) | |
53 | ||
43 | 54 | mapper.connect('/index', |
44 | 55 | controller=search_catalog_resource, |
45 | 56 | action='index', |
17 | 17 | from oslo.config import cfg |
18 | 18 | from oslo_log import log as logging |
19 | 19 | import six |
20 | import stevedore | |
21 | 20 | import webob.exc |
22 | 21 | |
23 | 22 | from glance.api import policy |
71 | 70 | raise webob.exc.HTTPNotFound(explanation=e.msg) |
72 | 71 | except exception.Duplicate as e: |
73 | 72 | raise webob.exc.HTTPConflict(explanation=e.msg) |
73 | except Exception as e: | |
74 | LOG.error(utils.exception_to_str(e)) | |
75 | raise webob.exc.HTTPInternalServerError() | |
76 | ||
77 | def plugins_info(self, req): | |
78 | try: | |
79 | search_repo = self.gateway.get_catalog_search_repo(req.context) | |
80 | return search_repo.plugins_info() | |
81 | except exception.Forbidden as e: | |
82 | raise webob.exc.HTTPForbidden(explanation=e.msg) | |
83 | except exception.NotFound as e: | |
84 | raise webob.exc.HTTPNotFound(explanation=e.msg) | |
74 | 85 | except Exception as e: |
75 | 86 | LOG.error(utils.exception_to_str(e)) |
76 | 87 | raise webob.exc.HTTPInternalServerError() |
350 | 361 | response.unicode_body = six.text_type(body) |
351 | 362 | response.content_type = 'application/json' |
352 | 363 | |
364 | def plugins_info(self, response, query_result): | |
365 | body = json.dumps(query_result, ensure_ascii=False) | |
366 | response.unicode_body = six.text_type(body) | |
367 | response.content_type = 'application/json' | |
368 | ||
353 | 369 | def index(self, response, query_result): |
354 | 370 | body = json.dumps(query_result, ensure_ascii=False) |
355 | 371 | response.unicode_body = six.text_type(body) |
356 | 372 | response.content_type = 'application/json' |
357 | 373 | |
358 | 374 | |
359 | def get_plugins(): | |
360 | namespace = 'glance.search.index_backend' | |
361 | ext_manager = stevedore.extension.ExtensionManager( | |
362 | namespace, invoke_on_load=True) | |
363 | return ext_manager.extensions | |
364 | ||
365 | ||
366 | 375 | def create_resource(): |
367 | 376 | """Search resource factory method""" |
368 | plugins = get_plugins() | |
377 | plugins = utils.get_search_plugins() | |
369 | 378 | deserializer = RequestDeserializer(plugins) |
370 | 379 | serializer = ResponseSerializer() |
371 | 380 | controller = SearchController(plugins) |
17 | 17 | import webob.exc |
18 | 18 | |
19 | 19 | from glance.common import exception |
20 | from glance.common import utils | |
20 | 21 | import glance.gateway |
21 | 22 | import glance.search |
22 | 23 | from glance.search.api.v0_1 import search as search |
233 | 234 | webob.exc.HTTPInternalServerError, self.search_controller.index, |
234 | 235 | request, actions) |
235 | 236 | |
237 | def test_plugins_info(self): | |
238 | request = unit_test_utils.get_fake_request() | |
239 | self.search_controller.plugins_info = mock.Mock(return_value="{}") | |
240 | self.search_controller.plugins_info(request) | |
241 | self.search_controller.plugins_info.assert_called_once_with(request) | |
242 | ||
243 | def test_plugins_info_repo(self): | |
244 | request = unit_test_utils.get_fake_request() | |
245 | repo = glance.search.CatalogSearchRepo | |
246 | repo.plugins_info = mock.Mock(return_value="{}") | |
247 | self.search_controller.plugins_info(request) | |
248 | repo.plugins_info.assert_called_once_with() | |
249 | ||
250 | def test_plugins_info_forbidden(self): | |
251 | request = unit_test_utils.get_fake_request() | |
252 | repo = glance.search.CatalogSearchRepo | |
253 | repo.plugins_info = mock.Mock(side_effect=exception.Forbidden) | |
254 | ||
255 | self.assertRaises( | |
256 | webob.exc.HTTPForbidden, self.search_controller.plugins_info, | |
257 | request) | |
258 | ||
259 | def test_plugins_info_not_found(self): | |
260 | request = unit_test_utils.get_fake_request() | |
261 | repo = glance.search.CatalogSearchRepo | |
262 | repo.plugins_info = mock.Mock(side_effect=exception.NotFound) | |
263 | ||
264 | self.assertRaises(webob.exc.HTTPNotFound, | |
265 | self.search_controller.plugins_info, request) | |
266 | ||
267 | def test_plugins_info_internal_server_error(self): | |
268 | request = unit_test_utils.get_fake_request() | |
269 | repo = glance.search.CatalogSearchRepo | |
270 | repo.plugins_info = mock.Mock(side_effect=Exception) | |
271 | ||
272 | self.assertRaises(webob.exc.HTTPInternalServerError, | |
273 | self.search_controller.plugins_info, request) | |
274 | ||
236 | 275 | |
237 | 276 | class TestSearchDeserializer(test_utils.BaseTestCase): |
238 | 277 | |
239 | 278 | def setUp(self): |
240 | 279 | super(TestSearchDeserializer, self).setUp() |
241 | self.deserializer = search.RequestDeserializer(search.get_plugins()) | |
280 | self.deserializer = search.RequestDeserializer( | |
281 | utils.get_search_plugins() | |
282 | ) | |
242 | 283 | |
243 | 284 | def test_single_index(self): |
244 | 285 | request = unit_test_utils.get_fake_request() |
410 | 451 | |
411 | 452 | def setUp(self): |
412 | 453 | super(TestIndexDeserializer, self).setUp() |
413 | self.deserializer = search.RequestDeserializer(search.get_plugins()) | |
454 | self.deserializer = search.RequestDeserializer( | |
455 | utils.get_search_plugins() | |
456 | ) | |
414 | 457 | |
415 | 458 | def test_empty_request(self): |
416 | 459 | request = unit_test_utils.get_fake_request() |
873 | 916 | super(TestResponseSerializer, self).setUp() |
874 | 917 | self.serializer = search.ResponseSerializer() |
875 | 918 | |
919 | def test_plugins_info(self): | |
920 | expected = { | |
921 | "plugins": [ | |
922 | { | |
923 | "index": "glance", | |
924 | "type": "image" | |
925 | }, | |
926 | { | |
927 | "index": "glance", | |
928 | "type": "metadef" | |
929 | } | |
930 | ] | |
931 | } | |
932 | ||
933 | request = webob.Request.blank('/v0.1/search') | |
934 | response = webob.Response(request=request) | |
935 | result = { | |
936 | "plugins": [ | |
937 | { | |
938 | "index": "glance", | |
939 | "type": "image" | |
940 | }, | |
941 | { | |
942 | "index": "glance", | |
943 | "type": "metadef" | |
944 | } | |
945 | ] | |
946 | } | |
947 | self.serializer.search(response, result) | |
948 | actual = jsonutils.loads(response.body) | |
949 | self.assertEqual(expected, actual) | |
950 | self.assertEqual('application/json', response.content_type) | |
951 | ||
876 | 952 | def test_search(self): |
877 | 953 | expected = [{ |
878 | 954 | 'id': '1', |