Added Bansho Config API

Change-Id: Iedbbe172276d9da2c97895ac402fb5b88660589f
This commit is contained in:
aviau 2015-04-30 14:02:43 -04:00
parent 0b4bd139f5
commit 656651eb4f
10 changed files with 181 additions and 1 deletions

View File

@ -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

View File

@ -8,3 +8,4 @@ V2 Web API
config
status
actions
bansho

View File

@ -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'
}

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

View File

@ -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
)

View File

@ -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()))