Validate required fields in meter definition

Change-Id: I64b7d5b5189bb081014745bcb84edd9abefee414
Closes-Bug: #1496120
This commit is contained in:
Pradeep Kilambi 2015-09-15 17:41:45 -04:00
parent 92e177eb28
commit 663c04b285
2 changed files with 16 additions and 9 deletions

View File

@ -57,13 +57,17 @@ class MeterDefinition(object):
JSONPATH_RW_PARSER = parser.ExtentedJsonPathParser()
REQUIRED_FIELDS = ['name', 'type', 'event_type', 'unit', 'volume',
'resource_id']
def __init__(self, definition_cfg):
self.cfg = definition_cfg
self._event_type = self.cfg.get('event_type')
if not self._event_type:
missing = [field for field in self.REQUIRED_FIELDS
if not self.cfg.get(field)]
if missing:
raise MeterDefinitionException(
_LE("Required field event_type not specified"), self.cfg)
_LE("Required fields %s not specified") % missing, self.cfg)
self._event_type = self.cfg.get('event_type')
if isinstance(self._event_type, six.string_types):
self._event_type = [self._event_type]

View File

@ -231,16 +231,19 @@ class TestMeterDefinition(test.BaseTestCase):
self.assertEqual("30be1fc9a03c4e94ab05c403a8a377f2",
handler.parse_fields("project_id", NOTIFICATION))
def test_config_missing_fields(self):
cfg = dict(name="test", type="delta")
def test_config_required_missing_fields(self):
cfg = dict()
try:
notifications.MeterDefinition(cfg)
except notifications.MeterDefinitionException as e:
self.assertEqual("Required field event_type not specified",
e.message)
self.assertEqual("Required fields ['name', 'type', 'event_type',"
" 'unit', 'volume', 'resource_id']"
" not specified", e.message)
def test_bad_type_cfg_definition(self):
cfg = dict(name="test", type="foo", event_type="bar.create")
cfg = dict(name="test", type="foo", event_type="bar.create",
unit="foo", volume="bar",
resource_id="bea70e51c7340cb9d555b15cbfcaec23")
try:
notifications.MeterDefinition(cfg)
except notifications.MeterDefinitionException as e: