diff --git a/ceilometer/api/v1.py b/ceilometer/api/v1.py index 305f718c..bd0ddd48 100644 --- a/ceilometer/api/v1.py +++ b/ceilometer/api/v1.py @@ -35,8 +35,16 @@ blueprint = flask.Blueprint('v1', __name__) @blueprint.route('/resources', defaults={'source': None}) @blueprint.route('/sources//resources') -def list_resources(source): - resources = flask.request.storage_conn.get_resources(source=source) +@blueprint.route('/users//resources') +@blueprint.route('/projects//resources') +@blueprint.route('/sources//users//resources') +@blueprint.route('/sources//projects//resources') +def list_resources(source=None, user=None, project=None): + resources = flask.request.storage_conn.get_resources( + source=source, + user=user, + project=project, + ) return flask.jsonify(resources=list(resources)) @@ -53,6 +61,16 @@ def list_users(source): ## APIs for working with events. +@blueprint.route('/projects/') +@blueprint.route('/projects//meters/') +@blueprint.route('/projects//resources/') +@blueprint.route('/projects//resources//meters/') +@blueprint.route('/sources//projects/') +@blueprint.route('/sources//projects//meters/') +@blueprint.route('/sources//projects//resources/') +@blueprint.route( + '/sources//projects//resources//meters/' + ) @blueprint.route('/users/') @blueprint.route('/users//meters/') @blueprint.route('/users//resources/') @@ -63,8 +81,14 @@ def list_users(source): @blueprint.route( '/sources//users//resources//meters/' ) -def list_events(user, meter=None, resource=None, source=None): +def list_events(user=None, + meter=None, + resource=None, + source=None, + project=None, + ): f = storage.EventFilter(user=user, + project=project, source=source, meter=meter, resource=resource, diff --git a/tests/api/v1/test_list_events.py b/tests/api/v1/test_list_events.py index 74bbd7af..e3dd6869 100644 --- a/tests/api/v1/test_list_events.py +++ b/tests/api/v1/test_list_events.py @@ -39,7 +39,7 @@ class TestListEvents(tests_api.TestBase): 'cumulative', 1, 'user-id', - 'project-id', + 'project1', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, @@ -56,7 +56,7 @@ class TestListEvents(tests_api.TestBase): 'cumulative', 1, 'user-id', - 'project-id', + 'project2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, @@ -92,3 +92,27 @@ class TestListEvents(tests_api.TestBase): data = self.get('/users/user-id/resources/resource-id') ids = [r['resource_id'] for r in data['events']] self.assertEquals(['resource-id'], ids) + + + def test_with_project(self): + data = self.get('/projects/project1') + self.assertEquals(1, len(data['events'])) + + def test_with_project_and_meters(self): + data = self.get('/projects/project1/meters/instance') + self.assertEquals(1, len(data['events'])) + + def test_with_project_and_meters_invalid(self): + data = self.get('/projects/project2/meters/no-such-meter') + self.assertEquals(0, len(data['events'])) + + def test_with_source_and_project(self): + data = self.get('/sources/source1/projects/project1') + ids = [r['resource_id'] for r in data['events']] + self.assertEquals(['resource-id'], ids) + + def test_with_resource(self): + data = self.get('/projects/project1/resources/resource-id') + ids = [r['resource_id'] for r in data['events']] + self.assertEquals(['resource-id'], ids) + diff --git a/tests/api/v1/test_list_resources.py b/tests/api/v1/test_list_resources.py index 6d5e0f24..d7b473b7 100644 --- a/tests/api/v1/test_list_resources.py +++ b/tests/api/v1/test_list_resources.py @@ -111,3 +111,82 @@ class TestListResources(tests_api.TestBase): data = self.get('/sources/test_list_resources/resources') ids = [r['resource_id'] for r in data['resources']] self.assertEquals(['resource-id'], ids) + + def test_with_user(self): + counter1 = counter.Counter( + 'test_list_resources', + 'instance', + 'cumulative', + 1, + 'user-id', + 'project-id', + 'resource-id', + timestamp=datetime.datetime(2012, 7, 2, 10, 40), + duration=0, + resource_metadata={'display_name': 'test-server', + 'tag': 'self.counter', + } + ) + msg = meter.meter_message_from_counter(counter1) + self.conn.record_metering_data(msg) + + counter2 = counter.Counter( + 'not-test', + 'instance', + 'cumulative', + 1, + 'user-id2', + 'project-id', + 'resource-id-alternate', + timestamp=datetime.datetime(2012, 7, 2, 10, 41), + duration=0, + resource_metadata={'display_name': 'test-server', + 'tag': 'self.counter2', + } + ) + msg2 = meter.meter_message_from_counter(counter2) + self.conn.record_metering_data(msg2) + + data = self.get('/users/user-id/resources') + ids = [r['resource_id'] for r in data['resources']] + self.assertEquals(['resource-id'], ids) + + def test_with_project(self): + counter1 = counter.Counter( + 'test_list_resources', + 'instance', + 'cumulative', + 1, + 'user-id', + 'project-id', + 'resource-id', + timestamp=datetime.datetime(2012, 7, 2, 10, 40), + duration=0, + resource_metadata={'display_name': 'test-server', + 'tag': 'self.counter', + } + ) + msg = meter.meter_message_from_counter(counter1) + self.conn.record_metering_data(msg) + + counter2 = counter.Counter( + 'not-test', + 'instance', + 'cumulative', + 1, + 'user-id2', + 'project-id2', + 'resource-id-alternate', + timestamp=datetime.datetime(2012, 7, 2, 10, 41), + duration=0, + resource_metadata={'display_name': 'test-server', + 'tag': 'self.counter2', + } + ) + msg2 = meter.meter_message_from_counter(counter2) + self.conn.record_metering_data(msg2) + + data = self.get('/projects/project-id/resources') + ids = [r['resource_id'] for r in data['resources']] + self.assertEquals(['resource-id'], ids) +