From 92fbb930915401016a0f68fd7040d6771106a6e8 Mon Sep 17 00:00:00 2001 From: Doug Szumski Date: Tue, 12 Nov 2019 11:10:42 +0000 Subject: [PATCH] Allow users to set periodic notifications on all notification types In some cases, users may want to send periodic notifications for notification types other than webhooks. Story: 2006837 Task: 37417 Depends-On: https://review.opendev.org/#/c/694596 Change-Id: Ia2c50e623aa79e06d2d35df4735fb2805fbf40ed --- docs/monasca-api-spec.md | 6 ++--- monasca_api/tests/test_validation.py | 22 +++++++++---------- .../notifications_request_body_schema.py | 5 +---- ...riodic-notifications-68f6c2ed6f89ebc0.yaml | 4 ++++ 4 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 releasenotes/notes/relax-constraints-for-setting-periodic-notifications-68f6c2ed6f89ebc0.yaml diff --git a/docs/monasca-api-spec.md b/docs/monasca-api-spec.md index 1e6fa31e3..88056e03b 100644 --- a/docs/monasca-api-spec.md +++ b/docs/monasca-api-spec.md @@ -1673,7 +1673,7 @@ None. * name (string(250), required) - A descriptive name of the notification method. * type (string(100), required) - The type of notification method (see [List supported Notification Method Types](#list-supported-notification-method-types) for supported types). * address (string(100), required) - The email/url address to notify. -* period (integer, optional) - The interval in seconds to periodically send the notification. Only can be set as a non zero value for WEBHOOK methods. Allowed periods for Webhooks by default are 0, 60. You can change allow periods for webhooks in the api config. The notification will continue to be sent at the defined interval until the alarm it is associated with changes state. +* period (integer, optional) - The interval in seconds to periodically send the notification. Supported periods are defined in the Monasca API and Notification service config. The notification will continue to be sent at the defined interval until the alarm it is associated with changes state. #### Request Examples ``` @@ -1889,7 +1889,7 @@ None. * name (string(250), required) - A descriptive name of the notification method. * type (string(100), required) - The type of notification method (see [List supported Notification Method Types](#list-supported-notification-method-types) for supported types). * address (string(100), required) - The email/url address to notify. -* period (integer, required) - The interval in seconds to periodically send the notification. Only can be set as a non zero value for WEBHOOK methods. Allowed periods for Webhooks by default are 0, 60. You can change allow periods for webhooks in the api config. The notification will continue to be sent at the defined interval until the alarm it is associated with changes state. +* period (integer, required) - The interval in seconds to periodically send the notification. Supported periods are defined in the Monasca API and Notification service config. The notification will continue to be sent at the defined interval until the alarm it is associated with changes state. #### Request Examples ```` @@ -1960,7 +1960,7 @@ None. * name (string(250), optional) - A descriptive name of the notification method. * type (string(100), optional) - The type of notification method (see [List supported Notification Method Types](#list-supported-notification-method-types) for supported types). * address (string(100), optional) - The email/url address to notify. -* period (integer, optional) - The interval in seconds to periodically send the notification. Only can be set as a non zero value for WEBHOOK methods. Allowed periods for Webhooks by default are 0, 60. You can change allow periods for webhooks in the api config. The notification will continue to be sent at the defined interval until the alarm it is associated with changes state. +* period (integer, optional) - The interval in seconds to periodically send the notification. Supported periods are defined in the Monasca API and Notification service config. The notification will continue to be sent at the defined interval until the alarm it is associated with changes state. #### Request Examples ```` diff --git a/monasca_api/tests/test_validation.py b/monasca_api/tests/test_validation.py index bef80e07e..aa6992117 100644 --- a/monasca_api/tests/test_validation.py +++ b/monasca_api/tests/test_validation.py @@ -175,16 +175,16 @@ class TestNotificationValidation(base.BaseTestCase): notification, valid_periods) self.assertEqual("Address name@ is not of correct format", str(ex)) - def test_validation_exception_for_invalid_period_for_email(self): + def test_validation_for_email_non_zero_period(self): notification = { "name": "MyEmail", "type": "EMAIL", "address": "name@domain.com", "period": "60"} - ex = self.assertRaises(schemas_exceptions.ValidationException, - schemas_notifications.parse_and_validate, - notification, valid_periods) - self.assertEqual("Period can only be set with webhooks", str(ex)) + try: + schemas_notifications.parse_and_validate(notification, valid_periods) + except schemas_exceptions.ValidationException: + self.fail("shouldn't happen") def test_validation_for_webhook(self): notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://somedomain.com"} @@ -228,7 +228,7 @@ class TestNotificationValidation(base.BaseTestCase): ex = self.assertRaises(schemas_exceptions.ValidationException, schemas_notifications.parse_and_validate, notification, valid_periods) - self.assertEqual("10 is not a valid period, not in [0, 60]", str(ex)) + self.assertEqual("10 is not in the configured list of valid periods: [0, 60]", str(ex)) def test_validation_for_pagerduty(self): notification = {"name": "MyPagerduty", "type": "PAGERDUTY", @@ -238,13 +238,13 @@ class TestNotificationValidation(base.BaseTestCase): except schemas_exceptions.ValidationException: self.fail("shouldn't happen") - def test_validation_exception_for_invalid_period_for_pagerduty(self): + def test_validation_for_pagerduty_non_zero_period(self): notification = {"name": "MyPagerduty", "type": "PAGERDUTY", "address": "nzH2LVRdMzun11HNC2oD", "period": 60} - ex = self.assertRaises(schemas_exceptions.ValidationException, - schemas_notifications.parse_and_validate, - notification, valid_periods) - self.assertEqual("Period can only be set with webhooks", str(ex)) + try: + schemas_notifications.parse_and_validate(notification, valid_periods) + except schemas_exceptions.ValidationException: + self.fail("shouldn't happen") def test_validation_for_max_name_address(self): name = "A" * 250 diff --git a/monasca_api/v2/common/schemas/notifications_request_body_schema.py b/monasca_api/v2/common/schemas/notifications_request_body_schema.py index d0347fa5f..e108ce369 100644 --- a/monasca_api/v2/common/schemas/notifications_request_body_schema.py +++ b/monasca_api/v2/common/schemas/notifications_request_body_schema.py @@ -60,9 +60,6 @@ def parse_and_validate(msg, valid_periods, require_all=False): elif notification_type == 'WEBHOOK': _validate_url(msg['address']) - if notification_type != 'WEBHOOK' and msg['period'] != 0: - raise exceptions.ValidationException("Period can only be set with webhooks") - def _validate_email(address): if not validation.validate_email_address(address): @@ -92,5 +89,5 @@ def _parse_and_validate_period(period, valid_periods): raise exceptions.ValidationException("Period {} must be a valid integer".format(period)) if period != 0 and period not in valid_periods: raise exceptions.ValidationException( - "{} is not a valid period, not in {}".format(period, valid_periods)) + "{} is not in the configured list of valid periods: {}".format(period, valid_periods)) return period diff --git a/releasenotes/notes/relax-constraints-for-setting-periodic-notifications-68f6c2ed6f89ebc0.yaml b/releasenotes/notes/relax-constraints-for-setting-periodic-notifications-68f6c2ed6f89ebc0.yaml new file mode 100644 index 000000000..e65efdbe4 --- /dev/null +++ b/releasenotes/notes/relax-constraints-for-setting-periodic-notifications-68f6c2ed6f89ebc0.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Adds support for configuring periodic notifications for all notification types.