diff --git a/doc/source/webapi/v2/bansho.rst b/doc/source/webapi/v2/bansho.rst new file mode 100644 index 0000000..55cce7b --- /dev/null +++ b/doc/source/webapi/v2/bansho.rst @@ -0,0 +1,14 @@ +.. docbookrestapi + +====== +Bansho +====== + +.. rest-controller:: surveil.api.controllers.v2.bansho:BanshoController + :webprefix: /v2/bansho + +Config +====== + +.. rest-controller:: surveil.api.controllers.v2.bansho.config:ConfigController + :webprefix: /v2/bansho/config diff --git a/doc/source/webapi/v2/index.rst b/doc/source/webapi/v2/index.rst index d1fc9e9..7f69d15 100644 --- a/doc/source/webapi/v2/index.rst +++ b/doc/source/webapi/v2/index.rst @@ -8,3 +8,4 @@ V2 Web API config status actions + bansho diff --git a/surveil/api/authmiddleware/auth.py b/surveil/api/authmiddleware/auth.py index 2dd030a..dc01fa1 100644 --- a/surveil/api/authmiddleware/auth.py +++ b/surveil/api/authmiddleware/auth.py @@ -94,7 +94,7 @@ class AuthProtocol(object): user_headers = { 'X-Identity-Status': 'Confirmed', - 'X-User-Id': 'surveil', + 'X-User-Id': 'surveil-default-user', 'X-Roles': 'admin', 'X-Service-Catalog': 'surveil' } diff --git a/surveil/api/controllers/v2/bansho/__init__.py b/surveil/api/controllers/v2/bansho/__init__.py new file mode 100644 index 0000000..242b197 --- /dev/null +++ b/surveil/api/controllers/v2/bansho/__init__.py @@ -0,0 +1,22 @@ +# Copyright 2015 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from surveil.api.controllers.v2.bansho import config + +from pecan import rest + + +class BanshoController(rest.RestController): + """Root Bansho controller.""" + config = config.ConfigController() diff --git a/surveil/api/controllers/v2/bansho/config.py b/surveil/api/controllers/v2/bansho/config.py new file mode 100644 index 0000000..3f18b85 --- /dev/null +++ b/surveil/api/controllers/v2/bansho/config.py @@ -0,0 +1,41 @@ +# Copyright 2014 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import pecan +from pecan import rest +import wsmeext.pecan as wsme_pecan + +from surveil.api.handlers.bansho import config_handler + + +class ConfigController(rest.RestController): + + @wsme_pecan.wsexpose(unicode) + def get(self): + """Retrieve user config, empty dict if no config exists.""" + user_name = pecan.request.headers.get('X-User-Id') + handler = config_handler.ConfigHandler(pecan.request) + config = handler.get(user_name) + return config + + @wsme_pecan.wsexpose(body=unicode) + def post(self, config): + """Save user config. + + :param config: JSON config object + """ + user_name = pecan.request.headers.get('X-User-Id') + handler = config_handler.ConfigHandler(pecan.request) + handler.update(user_name, config) diff --git a/surveil/api/controllers/v2/v2.py b/surveil/api/controllers/v2/v2.py index e66db5a..41f4810 100644 --- a/surveil/api/controllers/v2/v2.py +++ b/surveil/api/controllers/v2/v2.py @@ -15,6 +15,7 @@ from surveil.api.controllers.v2 import actions as v2_actions from surveil.api.controllers.v2 import admin as v2_admin from surveil.api.controllers.v2 import auth as v2_auth +from surveil.api.controllers.v2 import bansho as v2_bansho from surveil.api.controllers.v2 import config as v2_config from surveil.api.controllers.v2 import hello as v2_hello from surveil.api.controllers.v2 import logs as v2_logs @@ -30,3 +31,4 @@ class V2Controller(object): surveil = v2_admin.AdminController() auth = v2_auth.AuthController() logs = v2_logs.LogsController() + bansho = v2_bansho.BanshoController() diff --git a/surveil/api/handlers/bansho/__init__.py b/surveil/api/handlers/bansho/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/api/handlers/bansho/config_handler.py b/surveil/api/handlers/bansho/config_handler.py new file mode 100644 index 0000000..a13fd68 --- /dev/null +++ b/surveil/api/handlers/bansho/config_handler.py @@ -0,0 +1,52 @@ +# Copyright 2015 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from surveil.api.handlers import handler + + +class ConfigHandler(handler.Handler): + """Fulfills a request on the bansho config ressource.""" + + def get(self, user_name): + """Returns user config, empty dict by default.""" + user_name = str(user_name) + c = self.request.mongo_connection.surveil.bansho.config.find_one( + {"user_name": user_name}, {'_id': 0} + ) + config = (c or {}).get('config', {}) + return config + + def update(self, user_name, config): + """Modify user config.""" + user_name = str(user_name) + + config_dict = { + "user_name": user_name, + "config": config + } + + c = self.request.mongo_connection.surveil.bansho.config.find_one( + {"user_name": user_name}, {'_id': 0} + ) + + if c is None: + self.request.mongo_connection.surveil.bansho.config.insert( + config_dict + ) + else: + self.request.mongo_connection.surveil.bansho.config.update( + {"user_name": user_name}, + config_dict + ) diff --git a/surveil/tests/api/controllers/v2/bansho/__init__.py b/surveil/tests/api/controllers/v2/bansho/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/tests/api/controllers/v2/bansho/test_config.py b/surveil/tests/api/controllers/v2/bansho/test_config.py new file mode 100644 index 0000000..d63c58c --- /dev/null +++ b/surveil/tests/api/controllers/v2/bansho/test_config.py @@ -0,0 +1,48 @@ +# Copyright 2015 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import copy +import json + +from surveil.tests.api import functionalTest + + +class TestConfigController(functionalTest.FunctionalTest): + + def setUp(self): + super(TestConfigController, self).setUp() + self.config = [ + { + "user_name": "bob", + "config": {"k": "v", + "k2": "v2"} + } + ] + self.mongoconnection.surveil.bansho.config.insert( + copy.deepcopy(self.config) + ) + + def test_get_post_get(self): + # At first, conf is empty + response = self.app.get('/v2/bansho/config') + self.assertEqual({}, json.loads(response.body.decode())) + + # Now, post config + config = {"key": "val", + "morekey": "moreval"} + self.app.post_json('/v2/bansho/config', params=config) + + # Now config is what we gave to the API + response = self.app.get('/v2/bansho/config') + self.assertEqual(config, json.loads(response.body.decode()))