diff --git a/docs/monasca-log-api-spec.md b/docs/monasca-log-api-spec.md index eb88b99e..bc8c0dc0 100644 --- a/docs/monasca-log-api-spec.md +++ b/docs/monasca-log-api-spec.md @@ -16,7 +16,7 @@ Create log. * X-Auth-Token (string, required) - Keystone auth token * Content-Type (string, required) - application/json; text/plain * X-Application-Type (string(255), optional) - Type of application -* X-Dimensions ({string(255):string(255)}, optional) - A dictionary consisting of (key, value) pairs used to structure logs. +* X-Dimensions ({string(255):string(255)}, required) - A dictionary consisting of (key, value) pairs used to structure logs. #### Path Parameters None. diff --git a/monasca_log_api/tests/test_service.py b/monasca_log_api/tests/test_service.py index a02398af..3c5b5b19 100644 --- a/monasca_log_api/tests/test_service.py +++ b/monasca_log_api/tests/test_service.py @@ -350,6 +350,30 @@ class EnvelopeSizeValidations(testing.TestBase): ) +class LogMessageValidations(testing.TestBase): + + def test_should_pass_message_in_log_property(self): + log_object = { + 'message': 'some messages', + 'application_type': 'monasca-log-api', + 'dimensions': { + 'hostname': 'devstack' + } + } + common_service.Validations.validate_log_message(log_object) + + @unittest.expectedFailure + def test_should_fail_pass_for_non_message_in_log_property(self): + log_object = { + 'massage': 'some messages', + 'application_type': 'monasca-log-api', + 'dimensions': { + 'hostname': 'devstack' + } + } + common_service.Validations.validate_log_message(log_object) + + class LogsCreatorNewLog(unittest.TestCase): def setUp(self): self.instance = common_service.LogCreator() diff --git a/monasca_log_api/v2/common/service.py b/monasca_log_api/v2/common/service.py index 03cc3c79..24a77861 100644 --- a/monasca_log_api/v2/common/service.py +++ b/monasca_log_api/v2/common/service.py @@ -262,6 +262,20 @@ class Validations(object): description=u'Maximum allowed size is %d bytes' % max_size ) + @staticmethod + def validate_log_message(log_object): + """Validates log property. + + Log property should have message property. + + Args: + log_object (dict): log property + """ + if 'message' not in log_object: + raise exceptions.HTTPUnprocessableEntity( + 'Log property should have message' + ) + class LogCreator(object): """Transforms logs, @@ -343,6 +357,8 @@ class LogCreator(object): else: log_object.update({'message': payload}) + Validations.validate_log_message(log_object) + log_object.update({ 'application_type': application_type, 'dimensions': dimensions