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
This commit is contained in:
Sirushti Murugesan 2016-06-03 14:37:52 +05:30
parent 98dec2c9de
commit 0c6236d776
3 changed files with 98 additions and 6 deletions

View File

@ -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):

View File

@ -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)

View File

@ -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.