diff --git a/ceilometer/api/app.py b/ceilometer/api/app.py index 4ea522d7c..215113325 100644 --- a/ceilometer/api/app.py +++ b/ceilometer/api/app.py @@ -21,6 +21,7 @@ import flask from ceilometer.openstack.common import cfg +from ceilometer.openstack.common import jsonutils from ceilometer import storage from ceilometer.api import v1 from ceilometer.api import acl @@ -39,4 +40,11 @@ def attach_config(): flask.request.storage_engine = storage_engine flask.request.storage_conn = storage_engine.get_connection(cfg.CONF) + +@app.before_request +def attach_sources(): + with open("sources.json", "r") as f: + flask.request.sources = jsonutils.load(f) + + acl.install(app) diff --git a/ceilometer/api/v1.py b/ceilometer/api/v1.py index 77fa1ae3a..f2176c930 100644 --- a/ceilometer/api/v1.py +++ b/ceilometer/api/v1.py @@ -145,6 +145,15 @@ def list_all_resources(): return _list_resources() +@blueprint.route('/sources/') +def get_source(source): + """Return a source details. + + :param source: The ID of the reporting source. + """ + return flask.jsonify(flask.request.sources.get(source, {})) + + @blueprint.route('/sources//resources') def list_resources_by_source(source): """Return a list of resources for which a source is reporting diff --git a/ceilometer/tests/api.py b/ceilometer/tests/api.py index 1793199fd..375fbbf87 100644 --- a/ceilometer/tests/api.py +++ b/ceilometer/tests/api.py @@ -25,6 +25,7 @@ import flask from ceilometer.tests import db as db_test_base from ceilometer.api import v1 +from ceilometer.api import app class TestBase(db_test_base.TestBase): @@ -38,7 +39,8 @@ class TestBase(db_test_base.TestBase): @self.app.before_request def attach_storage_connection(): flask.request.storage_conn = self.conn - return + + self.app.before_request(app.attach_sources) def get(self, path, **kwds): if kwds: diff --git a/tests/api/v1/test_list_sources.py b/tests/api/v1/test_list_sources.py new file mode 100644 index 000000000..d5c8d029d --- /dev/null +++ b/tests/api/v1/test_list_sources.py @@ -0,0 +1,39 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2012 Julien Danjou +# +# Author: Julien Danjou +# +# 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. +"""Test listing users. +""" + +import datetime +import logging + +from ceilometer import counter +from ceilometer import meter + +from ceilometer.tests import api as tests_api + + +class TestListSource(tests_api.TestBase): + + def test_source(self): + ydata = self.get('/sources/test_source') + self.assert_("somekey" in ydata) + self.assertEqual(ydata["somekey"], 666) + + def test_unknownsource(self): + ydata = self.get('/sources/test_source_that_does_not_exist') + self.assertEqual(ydata, {}) diff --git a/tests/sources.json b/tests/sources.json new file mode 100644 index 000000000..8038cc1cf --- /dev/null +++ b/tests/sources.json @@ -0,0 +1,6 @@ +{ + "test_source": + { + "somekey": 666 + } +}