From 664f70383bfdd704322e136de226e9627c48d1a7 Mon Sep 17 00:00:00 2001 From: Peter Razumovsky Date: Mon, 23 Mar 2015 13:06:59 +0300 Subject: [PATCH] Add extra check for signal during DELETE or SUSPEND All resources, which have handle_signal, check condition if action in DELETE or SUSPEND state. Move this check to Resource class. Change-Id: Ifa9e97c19cc35c4b2aa40dbed37296d9d4a38d5b --- heat/engine/resource.py | 6 ++++++ .../resources/aws/autoscaling/scaling_policy.py | 4 ---- .../engine/resources/openstack/heat/ha_restarter.py | 4 ---- .../resources/openstack/heat/scaling_policy.py | 4 ---- heat/tests/generic_resource.py | 5 ----- heat/tests/test_resource.py | 13 +++++++++++++ 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/heat/engine/resource.py b/heat/engine/resource.py index f802fe94a..b9d796878 100644 --- a/heat/engine/resource.py +++ b/heat/engine/resource.py @@ -1104,6 +1104,12 @@ class Resource(object): to implement the signal, the base-class raise an exception if no handler is implemented. ''' + if self.action in (self.SUSPEND, self.DELETE): + LOG.exception(_LE('Cannot signal resource ' + 'during %s'), self.action) + ex = Exception(_('Cannot signal resource during %s') % self.action) + raise exception.ResourceFailure(ex, self) + def get_string_details(): if details is None: return 'No signal details provided' diff --git a/heat/engine/resources/aws/autoscaling/scaling_policy.py b/heat/engine/resources/aws/autoscaling/scaling_policy.py index 57f370709..59b2396ec 100644 --- a/heat/engine/resources/aws/autoscaling/scaling_policy.py +++ b/heat/engine/resources/aws/autoscaling/scaling_policy.py @@ -98,10 +98,6 @@ class AWSScalingPolicy(signal_responder.SignalResponder, return self.properties[self.ADJUSTMENT_TYPE] def handle_signal(self, details=None): - if self.action in (self.SUSPEND, self.DELETE): - msg = _('Cannot signal resource during %s') % self.action - raise Exception(msg) - # ceilometer sends details like this: # {u'alarm_id': ID, u'previous': u'ok', u'current': u'alarm', # u'reason': u'...'}) diff --git a/heat/engine/resources/openstack/heat/ha_restarter.py b/heat/engine/resources/openstack/heat/ha_restarter.py index 190d1352a..0a062c0cf 100644 --- a/heat/engine/resources/openstack/heat/ha_restarter.py +++ b/heat/engine/resources/openstack/heat/ha_restarter.py @@ -71,10 +71,6 @@ class Restarter(signal_responder.SignalResponder): self.resource_id_set(self._get_user_id()) def handle_signal(self, details=None): - if self.action in (self.SUSPEND, self.DELETE): - msg = _('Cannot signal resource during %s') % self.action - raise Exception(msg) - if details is None: alarm_state = 'alarm' else: diff --git a/heat/engine/resources/openstack/heat/scaling_policy.py b/heat/engine/resources/openstack/heat/scaling_policy.py index b267f7e04..d8ad84066 100644 --- a/heat/engine/resources/openstack/heat/scaling_policy.py +++ b/heat/engine/resources/openstack/heat/scaling_policy.py @@ -107,10 +107,6 @@ class AutoScalingPolicy(signal_responder.SignalResponder, return ''.join([t.capitalize() for t in adjustment_type.split('_')]) def handle_signal(self, details=None): - if self.action in (self.SUSPEND, self.DELETE): - msg = _('Cannot signal resource during %s') % self.action - raise Exception(msg) - # ceilometer sends details like this: # {u'alarm_id': ID, u'previous': u'ok', u'current': u'alarm', # u'reason': u'...'}) diff --git a/heat/tests/generic_resource.py b/heat/tests/generic_resource.py index a78d330fa..598b3eb1b 100644 --- a/heat/tests/generic_resource.py +++ b/heat/tests/generic_resource.py @@ -14,7 +14,6 @@ from oslo_log import log as logging import six -from heat.common.i18n import _ from heat.common.i18n import _LW from heat.engine import attributes from heat.engine import constraints @@ -139,10 +138,6 @@ class SignalResource(signal_responder.SignalResponder): self.resource_id_set(self._get_user_id()) def handle_signal(self, details=None): - if self.action in (self.SUSPEND, self.DELETE): - msg = _('Cannot signal resource during %s') % self.action - raise Exception(msg) - LOG.warn(_LW('Signaled resource (Type "%(type)s") %(details)s'), {'type': self.type(), 'details': details}) diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index ae9dec26f..746293135 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -123,6 +123,19 @@ class ResourceTest(common.HeatTestCase): self.assertEqual((res.INIT, res.COMPLETE), res.state) self.assertEqual('', res.status_reason) + def test_signal_wrong_action_state(self): + snippet = rsrc_defn.ResourceDefinition('res', + 'GenericResourceType') + res = resource.Resource('res', snippet, self.stack) + actions = [res.SUSPEND, res.DELETE] + for action in actions: + for status in res.STATUSES: + res.state_set(action, status) + ex = self.assertRaises(exception.ResourceFailure, + res.signal) + self.assertEqual('Exception: Cannot signal resource during ' + '%s' % action, six.text_type(ex)) + def test_resource_str_repr_stack_id_resource_id(self): tmpl = rsrc_defn.ResourceDefinition('test_res_str_repr', 'Foo') res = generic_rsrc.GenericResource('test_res_str_repr', tmpl,