v1-api: Added timestamp support.

Added timestamp support for resource/sample api. Fixed bug #1118542.

Change-Id: I644c480ca00f57549dc66bd387721c25d3b353c4
This commit is contained in:
Lianhao Lu
2013-02-20 15:57:50 +08:00
parent feb4bcc614
commit 8b3ef28c75
4 changed files with 104 additions and 9 deletions

View File

@@ -16,6 +16,21 @@
from ceilometerclient.common import base
def _get_opt_path(simple_params=[], **kwargs):
l = []
#get simple paramters
for key in simple_params:
val = kwargs.get(key)
if val:
l.append(key + '=' + val)
#get metadata query paramters
metaquery = kwargs.get('metaquery')
if metaquery:
l.extend(metaquery.split(':'))
return '&'.join(l)
class User(base.Resource):
def __init__(self, manager, info, loaded=False):
_d = {unicode('user_id'): info}
@@ -80,7 +95,8 @@ class ResourceManager(base.Manager):
u = kwargs.get('user_id')
s = kwargs.get('source')
p = kwargs.get('project_id')
opts = kwargs.get('metaquery')
opts_path = _get_opt_path(['start_timestamp', 'end_timestamp'],
**kwargs)
if u:
path = '/users/%s/resources' % (u)
elif s:
@@ -89,8 +105,8 @@ class ResourceManager(base.Manager):
path = '/projects/%s/resources' % (p)
else:
path = '/resources'
if opts:
path = '/v1%s?%s' % (path, '&'.join(opts.split(':')))
if opts_path:
path = '/v1%s?%s' % (path, opts_path)
else:
path = '/v1%s' % (path)
return self._list(path, 'resources')
@@ -118,7 +134,8 @@ class SampleManager(base.Manager):
u = kwargs.get('user_id')
p = kwargs.get('project_id')
s = kwargs.get('source')
opts = kwargs.get('metaquery')
opts_path = _get_opt_path(['start_timestamp', 'end_timestamp'],
**kwargs)
if r:
path = '/resources/%s/meters/%s' % (r, c)
elif u:
@@ -130,8 +147,8 @@ class SampleManager(base.Manager):
else:
path = '/meters'
if opts:
path = '/v1%s?%s' % (path, '&'.join(opts.split(':')))
if opts_path:
path = '/v1%s?%s' % (path, opts_path)
else:
path = '/v1%s' % (path)
return self._list(path, 'events')
@@ -153,7 +170,7 @@ class MeterManager(base.Manager):
u = kwargs.get('user_id')
p = kwargs.get('project_id')
s = kwargs.get('source')
opts = kwargs.get('metaquery')
opts_path = _get_opt_path(**kwargs)
if u:
path = '/users/%s/meters' % u
elif r:
@@ -164,8 +181,8 @@ class MeterManager(base.Manager):
path = '/sources/%s/meters' % s
else:
path = '/meters'
if opts:
path = '/v1%s?%s' % (path, '&'.join(opts.split(':')))
if opts_path:
path = '/v1%s?%s' % (path, opts_path)
else:
path = '/v1%s' % (path)
return self._list(path, 'meters')

View File

@@ -29,6 +29,12 @@ import ceilometerclient.exc as exc
help='ID of the project to show samples for.')
@utils.arg('-c', '--counter_name', metavar='<NAME>',
help='Name of meter to show samples for.')
@utils.arg('--start', metavar='<START_TIMESTAMP>',
help='ISO date in UTC which limits events by '
'timestamp >= this value')
@utils.arg('--end', metavar='<END_TIMESTAMP>',
help='ISO date in UTC which limits events by '
'timestamp <= this value')
def do_sample_list(cc, args):
'''List the samples for this meters'''
fields = {'counter_name': args.counter_name,
@@ -36,6 +42,8 @@ def do_sample_list(cc, args):
'user_id': args.user_id,
'project_id': args.project_id,
'source': args.source,
'start_timestamp': args.start,
'end_timestamp': args.end,
'metaquery': args.metaquery}
try:
samples = cc.samples.list(**fields)
@@ -49,6 +57,8 @@ def do_sample_list(cc, args):
sortby=0)
@utils.arg('-m', '--metaquery', metavar='<METAQUERY>',
help='Query into the metadata metadata.key=value:..')
@utils.arg('-s', '--source', metavar='<SOURCE>',
help='ID of the resource to show samples for.')
@utils.arg('-r', '--resource_id', metavar='<RESOURCE_ID>',
@@ -91,11 +101,19 @@ def do_user_list(cc, args={}):
help='ID of the project to show samples for.')
@utils.arg('-m', '--metaquery', metavar='<METAQUERY>',
help='Query into the metadata metadata.key=value:..')
@utils.arg('--start', metavar='<START_TIMESTAMP>',
help='ISO date in UTC which limits resouces by '
'last update time >= this value')
@utils.arg('--end', metavar='<END_TIMESTAMP>',
help='ISO date in UTC which limits resouces by '
'last update time <= this value')
def do_resource_list(cc, args={}):
'''List the users'''
kwargs = {'source': args.source,
'user_id': args.user_id,
'project_id': args.project_id,
'start_timestamp': args.start,
'end_timestamp': args.end,
'metaquery': args.metaquery}
resources = cc.resources.list(**kwargs)

View File

@@ -88,6 +88,21 @@ fixtures = {
]},
),
},
'/v1/resources?start_timestamp=now&end_timestamp=now': {
'GET': (
{},
{'resources': [
{
'resource_id': 'b',
'project_id': 'dig_the_ditch',
'user_id': 'joey',
'timestamp': 'now',
'meter': ['this', 'that'],
'metadata': {'zxc_id': 'foo'},
},
]},
),
},
}
@@ -133,3 +148,12 @@ class ResourceManagerTest(unittest.TestCase):
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].resource_id, 'a')
def test_list_by_timestamp(self):
resources = list(self.mgr.list(start_timestamp='now', end_timestamp='now'))
expect = [
('GET', '/v1/resources?start_timestamp=now&end_timestamp=now', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].resource_id, 'b')

View File

@@ -92,6 +92,25 @@ fixtures = {
]},
),
},
'/v1/users/freddy/meters/balls?start_timestamp=now&end_timestamp=now': {
'GET': (
{},
{'events': [
{
'resource_id': 'inst-0045',
'project_id': 'melbourne_open',
'user_id': 'freddy',
'name': 'tennis',
'type': 'counter',
'unit': 'balls',
'volume': 3,
'timestamp': 'now',
'resource_metadata': None,
},
]},
),
},
}
@@ -144,3 +163,20 @@ class SampleManagerTest(unittest.TestCase):
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(samples), 1)
self.assertEqual(samples[0].resource_metadata['zxc_id'], 'foo')
def test_list_by_timestamp(self):
samples = list(self.mgr.list(user_id='freddy',
counter_name='balls',
start_timestamp='now',
end_timestamp='now'))
expect = [
('GET',
'/v1/users/freddy/meters/balls?' +
'start_timestamp=now&end_timestamp=now',
{}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(samples), 1)
self.assertEqual(samples[0].project_id, 'melbourne_open')
self.assertEqual(samples[0].user_id, 'freddy')
self.assertEqual(samples[0].volume, 3)