validate counter_type when posting samples

check to ensure counter_type is valid when posting samples via API

Change-Id: I097c54f6b410c67b117b07eaf146828766f4852a
Fixes: bug #1221007
This commit is contained in:
Gordon Chung 2013-09-05 19:08:07 -04:00 committed by Gerrit Code Review
parent f505ae11b7
commit 42d910d820
3 changed files with 26 additions and 3 deletions

View File

@ -621,6 +621,11 @@ class MeterController(rest.RestController):
raise wsme.exc.InvalidInput('message_id', s.message_id,
'The message_id must not be set')
if s.counter_type not in sample.TYPES:
raise wsme.exc.InvalidInput('counter_type', s.counter_type,
'The counter type must be: ' +
', '.join(sample.TYPES))
s.user_id = (s.user_id or def_user_id)
s.project_id = (s.project_id or def_project_id)
s.source = '%s:%s' % (s.project_id, (s.source or def_source))
@ -700,9 +705,7 @@ class Meter(_Base):
name = wtypes.text
"The unique name for the meter"
type = wtypes.Enum(str, sample.TYPE_GAUGE,
sample.TYPE_CUMULATIVE,
sample.TYPE_DELTA)
type = wtypes.Enum(str, *sample.TYPES)
"The meter type (see :ref:`measurements`)"
unit = wtypes.text

View File

@ -95,3 +95,5 @@ class Sample(object):
TYPE_GAUGE = 'gauge'
TYPE_DELTA = 'delta'
TYPE_CUMULATIVE = 'cumulative'
TYPES = (TYPE_GAUGE, TYPE_DELTA, TYPE_CUMULATIVE)

View File

@ -72,6 +72,24 @@ class TestPostSamples(FunctionalTest,
self.assertEqual(s1, data.json)
self.assertEqual(s1[0], self.published[0][1]['args']['data'][0])
def test_invalid_counter_type(self):
s1 = [{'counter_name': 'my_counter_name',
'counter_type': 'INVALID_TYPE',
'counter_unit': 'instance',
'counter_volume': 1,
'source': 'closedstack',
'resource_id': 'bd9431c1-8d69-4ad3-803a-8d4a6b89fd36',
'project_id': '35b17138-b364-4e6a-a131-8f3099c5be68',
'user_id': 'efd87807-12d2-4b38-9c70-5f5c2ac427ff',
'resource_metadata': {'name1': 'value1',
'name2': 'value2'}}]
data = self.post_json('/meters/my_counter_name/', s1,
expect_errors=True)
self.assertEqual(data.status_int, 400)
self.assertEqual(len(self.published), 0)
def test_messsage_id_provided(self):
"""Do not accept sample with message_id."""
s1 = [{'counter_name': 'my_counter_name',