From 755da47df3fd6dd306e3de0ae3ff0eacf22bea6c Mon Sep 17 00:00:00 2001 From: Eric Juma Date: Thu, 15 Dec 2016 12:48:17 -0500 Subject: [PATCH] Add config option to enable/disable services for each sp Modified proxy.py and config.py to support the option. Change-Id: Ibd7d701f4efb1fc952cd4750cd39d98c9eb98a79 --- etc/mixmatch.conf.sample | 2 + mixmatch/config.py | 6 +- mixmatch/proxy.py | 11 +++- mixmatch/tests/unit/test_request_handler.py | 64 ++++++++++++++++++++- mixmatch/tests/unit/test_services.py | 6 +- 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/etc/mixmatch.conf.sample b/etc/mixmatch.conf.sample index 1a8c745..56bd2a8 100644 --- a/etc/mixmatch.conf.sample +++ b/etc/mixmatch.conf.sample @@ -27,6 +27,7 @@ messagebus="rabbit://stackrabbit:stackqueue@localhost" auth_url="http://127.0.0.1:5000/v3" image_endpoint="http://localhost:9292" volume_endpoint="http://localhost:8776" +enabled_services=image, volume [sp_cornmeal-sp] sp_name=cornmeal-sp @@ -34,6 +35,7 @@ messagebus="rabbit://stackrabbit:stackqueue@192.168.0.141" auth_url="http://192.168.0.141:5000/v3" image_endpoint="http://192.168.0.141:9292" volume_endpoint="http://192.168.0.141:8776" +enabled_services=image, volume # Logging [loggers] diff --git a/mixmatch/config.py b/mixmatch/config.py index dd4c69f..87c5ba4 100644 --- a/mixmatch/config.py +++ b/mixmatch/config.py @@ -151,7 +151,11 @@ def more_config(): cfg.StrOpt('volume_endpoint', default=None, - help="Volume Endpoint for Service Provider") + help="Volume Endpoint for Service Provider"), + + cfg.ListOpt('enabled_services', + default=['image', 'volume'], + help="Services to enable for Service Provider") ] CONF.register_group(sp_group) diff --git a/mixmatch/proxy.py b/mixmatch/proxy.py index c5de22e..62be65d 100644 --- a/mixmatch/proxy.py +++ b/mixmatch/proxy.py @@ -55,6 +55,11 @@ class RequestHandler(object): self.request_path.insert(0, 'image') self.service_type = self.request_path[0] + self.enabled_sps = filter( + lambda sp: (self.service_type in + config.get_conf_for_sp(sp).enabled_services), + CONF.service_providers + ) if len(self.request_path) == 1: # unversioned calls with no action @@ -185,7 +190,7 @@ class RequestHandler(object): if not CONF.search_by_broadcast: return self._local_forward() - for sp in CONF.service_providers: + for sp in self.enabled_sps: if sp == 'default': response = self._do_request_on('default') if 200 <= response.status_code < 300: @@ -207,7 +212,7 @@ class RequestHandler(object): responses = {} - for sp in CONF.service_providers: + for sp in self.enabled_sps: if sp == 'default': responses['default'] = self._do_request_on('default') else: @@ -222,7 +227,7 @@ class RequestHandler(object): path=request.base_url, detailed=self.detailed), 200, - content_type=responses['default'].headers['content-type'] + content_type='application/json' ) def _list_api_versions(self): diff --git a/mixmatch/tests/unit/test_request_handler.py b/mixmatch/tests/unit/test_request_handler.py index 5bd6b1d..36189c1 100644 --- a/mixmatch/tests/unit/test_request_handler.py +++ b/mixmatch/tests/unit/test_request_handler.py @@ -13,15 +13,19 @@ # under the License. import uuid +import json -from testtools import testcase +from oslo_config import fixture as config_fixture from mixmatch import proxy +from mixmatch.config import CONF +from mixmatch.tests.unit.base import BaseTest -class TestRequestHandler(testcase.TestCase): +class TestRequestHandler(BaseTest): def setUp(self): super(TestRequestHandler, self).setUp() + self.config_fixture = self.useFixture(config_fixture.Config(conf=CONF)) def test_prepare_headers(self): user_headers = { @@ -37,3 +41,59 @@ class TestRequestHandler(testcase.TestCase): } args = proxy.RequestHandler._prepare_args(user_args) self.assertEqual({}, args) + + def test_toggle_services(self): + self.config_fixture.load_raw_values( + group='sp_remote1', + enabled_services='volume' + ) + REMOTE_PROJECT_ID = "319d8162b38342609f5fafe1404216b9" + self.session_fixture.add_local_auth('local-tok', 'my_project_id') + self.session_fixture.add_sp_auth('remote1', 'local-tok', + REMOTE_PROJECT_ID, 'remote-tok') + self.session_fixture.add_project_at_sp('remote1', REMOTE_PROJECT_ID) + + LOCAL_IMAGES = { + "images": [ + {"id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27", + "size": 4096}, + {"id": "781b3762-9469-4cec-b58d-3349e5de4e9c", + "size": 476704768} + ], + } + + self.requests_fixture.get( + 'http://images.local/v2/images', + text=json.dumps(LOCAL_IMAGES), + status_code=200, + request_headers={'X-AUTH-TOKEN': 'local-tok'}, + headers={'CONTENT-TYPE': 'application/json'}) + + response = self.app.get( + '/image/v2/images', + headers={'X-AUTH-TOKEN': 'local-tok', + 'CONTENT-TYPE': 'application/json'}) + actual = json.loads(response.data.decode("ascii")) + self.assertEqual(actual, LOCAL_IMAGES) + + def test_toggle_services_no_sps(self): + self.config_fixture.load_raw_values( + group='sp_remote1', + enabled_services='volume' + ) + self.config_fixture.load_raw_values( + group='sp_default', + enabled_services='volume' + ) + REMOTE_PROJECT_ID = "319d8162b38342609f5fafe1404216b9" + self.session_fixture.add_local_auth('local-tok', 'my_project_id') + self.session_fixture.add_sp_auth('remote1', 'local-tok', + REMOTE_PROJECT_ID, 'remote-tok') + self.session_fixture.add_project_at_sp('remote1', REMOTE_PROJECT_ID) + + response = self.app.get( + '/image/v2/images', + headers={'X-AUTH-TOKEN': 'local-tok', + 'CONTENT-TYPE': 'application/json'}) + actual = json.loads(response.data.decode("ascii")) + self.assertEqual(actual, {'images': []}) diff --git a/mixmatch/tests/unit/test_services.py b/mixmatch/tests/unit/test_services.py index 9944d96..e52f936 100644 --- a/mixmatch/tests/unit/test_services.py +++ b/mixmatch/tests/unit/test_services.py @@ -14,12 +14,12 @@ import json from six.moves.urllib import parse -from oslo_config import fixture as config_fixture -from mixmatch.config import CONF - from testtools import testcase +from oslo_config import fixture as config_fixture + from mixmatch import services +from mixmatch.config import CONF from mixmatch.tests.unit import samples