Add project arg to event and resource queries

Change-Id: Idfb39d02e8af2b00f6f14520b5219f48dcf8fe8a
Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
This commit is contained in:
Doug Hellmann 2012-08-06 16:14:47 -04:00
parent 1c92548c5a
commit fd6348d363
3 changed files with 132 additions and 5 deletions

View File

@ -35,8 +35,16 @@ blueprint = flask.Blueprint('v1', __name__)
@blueprint.route('/resources', defaults={'source': None}) @blueprint.route('/resources', defaults={'source': None})
@blueprint.route('/sources/<source>/resources') @blueprint.route('/sources/<source>/resources')
def list_resources(source): @blueprint.route('/users/<user>/resources')
resources = flask.request.storage_conn.get_resources(source=source) @blueprint.route('/projects/<project>/resources')
@blueprint.route('/sources/<source>/users/<user>/resources')
@blueprint.route('/sources/<source>/projects/<project>/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)) return flask.jsonify(resources=list(resources))
@ -53,6 +61,16 @@ def list_users(source):
## APIs for working with events. ## APIs for working with events.
@blueprint.route('/projects/<project>')
@blueprint.route('/projects/<project>/meters/<meter>')
@blueprint.route('/projects/<project>/resources/<resource>')
@blueprint.route('/projects/<project>/resources/<resource>/meters/<meter>')
@blueprint.route('/sources/<source>/projects/<project>')
@blueprint.route('/sources/<source>/projects/<project>/meters/<meter>')
@blueprint.route('/sources/<source>/projects/<project>/resources/<resource>')
@blueprint.route(
'/sources/<source>/projects/<project>/resources/<resource>/meters/<meter>'
)
@blueprint.route('/users/<user>') @blueprint.route('/users/<user>')
@blueprint.route('/users/<user>/meters/<meter>') @blueprint.route('/users/<user>/meters/<meter>')
@blueprint.route('/users/<user>/resources/<resource>') @blueprint.route('/users/<user>/resources/<resource>')
@ -63,8 +81,14 @@ def list_users(source):
@blueprint.route( @blueprint.route(
'/sources/<source>/users/<user>/resources/<resource>/meters/<meter>' '/sources/<source>/users/<user>/resources/<resource>/meters/<meter>'
) )
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, f = storage.EventFilter(user=user,
project=project,
source=source, source=source,
meter=meter, meter=meter,
resource=resource, resource=resource,

View File

@ -39,7 +39,7 @@ class TestListEvents(tests_api.TestBase):
'cumulative', 'cumulative',
1, 1,
'user-id', 'user-id',
'project-id', 'project1',
'resource-id', 'resource-id',
timestamp=datetime.datetime(2012, 7, 2, 10, 40), timestamp=datetime.datetime(2012, 7, 2, 10, 40),
duration=0, duration=0,
@ -56,7 +56,7 @@ class TestListEvents(tests_api.TestBase):
'cumulative', 'cumulative',
1, 1,
'user-id', 'user-id',
'project-id', 'project2',
'resource-id-alternate', 'resource-id-alternate',
timestamp=datetime.datetime(2012, 7, 2, 10, 41), timestamp=datetime.datetime(2012, 7, 2, 10, 41),
duration=0, duration=0,
@ -92,3 +92,27 @@ class TestListEvents(tests_api.TestBase):
data = self.get('/users/user-id/resources/resource-id') data = self.get('/users/user-id/resources/resource-id')
ids = [r['resource_id'] for r in data['events']] ids = [r['resource_id'] for r in data['events']]
self.assertEquals(['resource-id'], ids) 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)

View File

@ -111,3 +111,82 @@ class TestListResources(tests_api.TestBase):
data = self.get('/sources/test_list_resources/resources') data = self.get('/sources/test_list_resources/resources')
ids = [r['resource_id'] for r in data['resources']] ids = [r['resource_id'] for r in data['resources']]
self.assertEquals(['resource-id'], ids) 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)