From 42d910d820a4d4fa58ed5f29e00e58793afbb306 Mon Sep 17 00:00:00 2001 From: Gordon Chung Date: Thu, 5 Sep 2013 19:08:07 -0400 Subject: [PATCH] validate counter_type when posting samples check to ensure counter_type is valid when posting samples via API Change-Id: I097c54f6b410c67b117b07eaf146828766f4852a Fixes: bug #1221007 --- ceilometer/api/controllers/v2.py | 9 ++++++--- ceilometer/sample.py | 2 ++ tests/api/v2/test_post_samples_scenarios.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 88db116f9..58240e88d 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -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 diff --git a/ceilometer/sample.py b/ceilometer/sample.py index 0dd233e2c..a5b10bae1 100644 --- a/ceilometer/sample.py +++ b/ceilometer/sample.py @@ -95,3 +95,5 @@ class Sample(object): TYPE_GAUGE = 'gauge' TYPE_DELTA = 'delta' TYPE_CUMULATIVE = 'cumulative' + +TYPES = (TYPE_GAUGE, TYPE_DELTA, TYPE_CUMULATIVE) diff --git a/tests/api/v2/test_post_samples_scenarios.py b/tests/api/v2/test_post_samples_scenarios.py index 164929839..3838d9550 100644 --- a/tests/api/v2/test_post_samples_scenarios.py +++ b/tests/api/v2/test_post_samples_scenarios.py @@ -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',