api: add source detail retrieval

Change-Id: I031d237f870172e87b9bcb413573a0ad5c18afca
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2012-10-26 14:34:01 +02:00
parent f6868e5e48
commit acf8fb88d5
5 changed files with 65 additions and 1 deletions

View File

@ -21,6 +21,7 @@
import flask import flask
from ceilometer.openstack.common import cfg from ceilometer.openstack.common import cfg
from ceilometer.openstack.common import jsonutils
from ceilometer import storage from ceilometer import storage
from ceilometer.api import v1 from ceilometer.api import v1
from ceilometer.api import acl from ceilometer.api import acl
@ -39,4 +40,11 @@ def attach_config():
flask.request.storage_engine = storage_engine flask.request.storage_engine = storage_engine
flask.request.storage_conn = storage_engine.get_connection(cfg.CONF) 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) acl.install(app)

View File

@ -145,6 +145,15 @@ def list_all_resources():
return _list_resources() return _list_resources()
@blueprint.route('/sources/<source>')
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/<source>/resources') @blueprint.route('/sources/<source>/resources')
def list_resources_by_source(source): def list_resources_by_source(source):
"""Return a list of resources for which a source is reporting """Return a list of resources for which a source is reporting

View File

@ -25,6 +25,7 @@ import flask
from ceilometer.tests import db as db_test_base from ceilometer.tests import db as db_test_base
from ceilometer.api import v1 from ceilometer.api import v1
from ceilometer.api import app
class TestBase(db_test_base.TestBase): class TestBase(db_test_base.TestBase):
@ -38,7 +39,8 @@ class TestBase(db_test_base.TestBase):
@self.app.before_request @self.app.before_request
def attach_storage_connection(): def attach_storage_connection():
flask.request.storage_conn = self.conn flask.request.storage_conn = self.conn
return
self.app.before_request(app.attach_sources)
def get(self, path, **kwds): def get(self, path, **kwds):
if kwds: if kwds:

View File

@ -0,0 +1,39 @@
# -*- encoding: utf-8 -*-
#
# Copyright © 2012 Julien Danjou
#
# Author: Julien Danjou <julien@danjou.info>
#
# 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, {})

6
tests/sources.json Normal file
View File

@ -0,0 +1,6 @@
{
"test_source":
{
"somekey": 666
}
}