From 0c6236d7767b4599b3583f1a6a9642eb06d0c2f8 Mon Sep 17 00:00:00 2001 From: Sirushti Murugesan Date: Fri, 3 Jun 2016 14:37:52 +0530 Subject: [PATCH] Add period property to Monasca Notification resource Currently, when one of Monasca's alarm transitions to the ALARM state, the webhook associated with the Alarm is only invoked once. This is inconvenient when the user wants to create a stack which will automatically scale up or scale down more than once if the alarm state continues to be in the same state. The new added property 'PERIOD' will now allow the user to tell Monasca to periodically invoke the webhook until the ALARM state transitions back to an OK state. To conform to the existing Heat autoscaling behaviour, we manually create the monasca notification resource in Heat with a default value of 60 only for the webhook notification type. Change-Id: I2466911efa8d05dfe497f521885464ab90b0ae8e --- .../openstack/monasca/notification.py | 35 ++++++++++- .../openstack/monasca/test_notification.py | 59 ++++++++++++++++++- .../monasca-period-f150cdb134f1e036.yaml | 10 ++++ 3 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/monasca-period-f150cdb134f1e036.yaml diff --git a/heat/engine/resources/openstack/monasca/notification.py b/heat/engine/resources/openstack/monasca/notification.py index 2430bd43e6..fe868b6d17 100644 --- a/heat/engine/resources/openstack/monasca/notification.py +++ b/heat/engine/resources/openstack/monasca/notification.py @@ -11,6 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. +from heat.common import exception from heat.common.i18n import _ from heat.engine import constraints from heat.engine import properties @@ -45,9 +46,9 @@ class MonascaNotification(resource.Resource): ) PROPERTIES = ( - NAME, TYPE, ADDRESS + NAME, TYPE, ADDRESS, PERIOD ) = ( - 'name', 'type', 'address' + 'name', 'type', 'address', 'period' ) properties_schema = { @@ -72,16 +73,39 @@ class MonascaNotification(resource.Resource): 'address, url or service key based on notification type.'), update_allowed=True, required=True, + ), + PERIOD: properties.Schema( + properties.Schema.INTEGER, + _('Interval in seconds to invoke webhooks if the alarm state ' + 'does not transition away from the defined trigger state. The ' + 'default value is a period interval of 60 seconds. A ' + 'value of 0 will disable continuous notifications. This ' + 'property is only applicable for the webhook notification ' + 'type.'), + support_status=support.SupportStatus(version='7.0.0'), + update_allowed=True, + constraints=[constraints.AllowedValues([0, 60])], + default=60, ) } + def validate(self): + super(MonascaNotification, self).validate() + if self.properties[self.PERIOD] is not None and ( + self.properties[self.TYPE] != self.WEBHOOK): + msg = _('The period property can only be specified against a ' + 'Webhook Notification type.') + raise exception.StackValidationFailed(message=msg) + def handle_create(self): args = dict( name=(self.properties[self.NAME] or self.physical_resource_name()), type=self.properties[self.TYPE], - address=self.properties[self.ADDRESS] + address=self.properties[self.ADDRESS], ) + if args['type'] == self.WEBHOOK: + args['period'] = self.properties[self.PERIOD] notification = self.client().notifications.create(**args) self.resource_id_set(notification['id']) @@ -98,6 +122,11 @@ class MonascaNotification(resource.Resource): args['address'] = (prop_diff.get(self.ADDRESS) or self.properties[self.ADDRESS]) + if args['type'] == self.WEBHOOK: + updated_period = prop_diff.get(self.PERIOD) + args['period'] = (updated_period if updated_period is not None + else self.properties[self.PERIOD]) + self.client().notifications.update(**args) def handle_delete(self): diff --git a/heat/tests/openstack/monasca/test_notification.py b/heat/tests/openstack/monasca/test_notification.py index be54c2fbd3..d3f4f482e0 100644 --- a/heat/tests/openstack/monasca/test_notification.py +++ b/heat/tests/openstack/monasca/test_notification.py @@ -13,6 +13,7 @@ import mock +from heat.common import exception from heat.engine.clients.os import monasca as client_plugin from heat.engine.resources.openstack.monasca import notification from heat.engine import stack @@ -29,7 +30,8 @@ sample_template = { 'properties': { 'name': 'test-notification', 'type': 'webhook', - 'address': 'http://localhost:80/' + 'address': 'http://localhost:80/', + 'period': 60 } } } @@ -69,6 +71,15 @@ class MonascaNotificationTest(common.HeatTestCase): return value + def test_validate_success_no_period(self): + self.test_resource.properties.data.pop('period') + self.test_resource.validate() + + def test_validate_invalid_type_with_period(self): + self.test_resource.properties.data['type'] = self.test_resource.EMAIL + self.assertRaises(exception.StackValidationFailed, + self.test_resource.validate) + def test_resource_handle_create(self): mock_notification_create = self.test_client.notifications.create mock_resource = self._get_mock_resource() @@ -87,6 +98,9 @@ class MonascaNotificationTest(common.HeatTestCase): 'http://localhost:80/', self.test_resource.properties.get( notification.MonascaNotification.ADDRESS)) + self.assertEqual( + 60, self.test_resource.properties.get( + notification.MonascaNotification.PERIOD)) self.test_resource.data_set = mock.Mock() self.test_resource.handle_create() @@ -94,7 +108,8 @@ class MonascaNotificationTest(common.HeatTestCase): args = dict( name='test-notification', type='webhook', - address='http://localhost:80/' + address='http://localhost:80/', + period=60 ) mock_notification_create.assert_called_once_with(**args) @@ -102,10 +117,47 @@ class MonascaNotificationTest(common.HeatTestCase): # validate physical resource id self.assertEqual(mock_resource['id'], self.test_resource.resource_id) + def test_resource_handle_create_default_period(self): + self.test_resource.properties.data.pop('period') + mock_notification_create = self.test_client.notifications.create + self.test_resource.handle_create() + + args = dict( + name='test-notification', + type='webhook', + address='http://localhost:80/', + period=60 + ) + mock_notification_create.assert_called_once_with(**args) + def test_resource_handle_update(self): mock_notification_update = self.test_client.notifications.update self.test_resource.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151' + prop_diff = {notification.MonascaNotification.ADDRESS: + 'http://localhost:1234/', + notification.MonascaNotification.NAME: 'name-updated', + notification.MonascaNotification.TYPE: 'webhook', + notification.MonascaNotification.PERIOD: 0} + + self.test_resource.handle_update(json_snippet=None, + tmpl_diff=None, + prop_diff=prop_diff) + + args = dict( + notification_id=self.test_resource.resource_id, + name='name-updated', + type='webhook', + address='http://localhost:1234/', + period=0 + ) + mock_notification_update.assert_called_once_with(**args) + + def test_resource_handle_update_default_period(self): + mock_notification_update = self.test_client.notifications.update + self.test_resource.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151' + self.test_resource.properties.data.pop('period') + prop_diff = {notification.MonascaNotification.ADDRESS: 'http://localhost:1234/', notification.MonascaNotification.NAME: 'name-updated', @@ -119,7 +171,8 @@ class MonascaNotificationTest(common.HeatTestCase): notification_id=self.test_resource.resource_id, name='name-updated', type='webhook', - address='http://localhost:1234/' + address='http://localhost:1234/', + period=60 ) mock_notification_update.assert_called_once_with(**args) diff --git a/releasenotes/notes/monasca-period-f150cdb134f1e036.yaml b/releasenotes/notes/monasca-period-f150cdb134f1e036.yaml new file mode 100644 index 0000000000..0d183b2ef4 --- /dev/null +++ b/releasenotes/notes/monasca-period-f150cdb134f1e036.yaml @@ -0,0 +1,10 @@ +--- +features: + - Add optional 'period' property for Monasca Notification resource. The new + added property will now allow the user to tell Monasca the interval in + seconds to periodically invoke a webhook until the ALARM state transitions + back to an OK state or vice versa. This is useful when the user wants to + create a stack which will automatically scale up or scale down more than + once if the alarm state continues to be in the same state. To conform to + the existing Heat autoscaling behaviour, we manually create the monasca + notification resource in Heat with a default interval value of 60.