diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 88db116f..58240e88 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 0dd233e2..a5b10bae 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 16492983..3838d955 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',