Merge "Validate required fields in meter definition"

This commit is contained in:
Jenkins 2015-09-16 20:34:53 +00:00 committed by Gerrit Code Review
commit 0c279d885b
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: