]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Teach ScalingPolicy and Restarter to create signed urls
authorAngus Salkeld <asalkeld@redhat.com>
Tue, 23 Jul 2013 12:55:43 +0000 (22:55 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Thu, 25 Jul 2013 00:59:10 +0000 (10:59 +1000)
This is so they will be able to support ceilometer webhooks

blueprint watch-ceilometer
Change-Id: I18b0dc22f95957a66f2287a7bd70613c8c8db3d6

heat/engine/resources/autoscaling.py
heat/engine/resources/instance.py
heat/tests/test_autoscaling.py

index 26618b416ca939fc6525b58e5e81b162de8624e6..d634b69e5b8dd5908bfe2a1ccb3607d9f44052f6 100644 (file)
@@ -15,6 +15,7 @@
 
 from heat.common import exception
 from heat.engine import resource
+from heat.engine import signal_responder
 from heat.engine import scheduler
 
 from heat.openstack.common import log as logging
@@ -434,7 +435,7 @@ class LaunchConfiguration(resource.Resource):
     }
 
 
-class ScalingPolicy(resource.Resource, CooldownMixin):
+class ScalingPolicy(signal_responder.SignalResponder, CooldownMixin):
     properties_schema = {
         'AutoScalingGroupName': {'Type': 'String',
                                  'Required': True},
@@ -451,6 +452,10 @@ class ScalingPolicy(resource.Resource, CooldownMixin):
     update_allowed_keys = ('Properties',)
     update_allowed_properties = ('ScalingAdjustment', 'AdjustmentType',
                                  'Cooldown',)
+    attributes_schema = {
+        "AlarmUrl": ("A signed url to handle the alarm. "
+                     "(Heat extension)")
+    }
 
     def handle_update(self, json_snippet, tmpl_diff, prop_diff):
         # If Properties has changed, update self.properties, so we
@@ -467,7 +472,7 @@ class ScalingPolicy(resource.Resource, CooldownMixin):
                         (self.name, self.properties['Cooldown']))
             return
 
-        group = self.stack.resources[self.properties['AutoScalingGroupName']]
+        group = self.stack[self.properties['AutoScalingGroupName']]
 
         logger.info('%s Alarm, adjusting Group %s by %s' %
                     (self.name, group.name,
@@ -479,6 +484,17 @@ class ScalingPolicy(resource.Resource, CooldownMixin):
                                  (self.properties['AdjustmentType'],
                                   self.properties['ScalingAdjustment']))
 
+    def _resolve_attribute(self, name):
+        '''
+        heat extension: "AlarmUrl" returns the url to post to the policy
+        when there is an alarm.
+        '''
+        if name == 'AlarmUrl' and self.resource_id is not None:
+            return unicode(self._get_signed_url())
+
+    def FnGetRefId(self):
+        return unicode(self.name)
+
 
 def resource_mapping():
     return {
index db56d0a8441632db821c6c9e5a35d826d91836dc..2bc4bc0e28058f36bb361bd93c856386b6f23de7 100644 (file)
@@ -22,6 +22,7 @@ from urlparse import urlparse
 
 from oslo.config import cfg
 
+from heat.engine import signal_responder
 from heat.engine import clients
 from heat.engine import resource
 from heat.engine import scheduler
@@ -37,9 +38,13 @@ from heat.openstack.common import uuidutils
 logger = logging.getLogger(__name__)
 
 
-class Restarter(resource.Resource):
+class Restarter(signal_responder.SignalResponder):
     properties_schema = {'InstanceId': {'Type': 'String',
                                         'Required': True}}
+    attributes_schema = {
+        "AlarmUrl": ("A signed url to handle the alarm. "
+                     "(Heat extension)")
+    }
 
     def _find_resource(self, resource_id):
         '''
@@ -63,6 +68,14 @@ class Restarter(resource.Resource):
                     (self.name, victim.name))
         self.stack.restart_resource(victim.name)
 
+    def _resolve_attribute(self, name):
+        '''
+        heat extension: "AlarmUrl" returns the url to post to the policy
+        when there is an alarm.
+        '''
+        if name == 'AlarmUrl' and self.resource_id is not None:
+            return unicode(self._get_signed_url())
+
 
 class Instance(resource.Resource):
     # AWS does not require InstanceType but Heat does because the nova
index 5b7bc201bef88af2594c5ee9c1b3403863f35dfc..73caffa93916bdddb3177f1f91e89b30f3935a83 100644 (file)
@@ -17,6 +17,8 @@ import copy
 
 import mox
 
+from oslo.config import cfg
+
 from heat.common import template_format
 from heat.common import exception
 from heat.engine.resources import autoscaling as asc
@@ -28,6 +30,7 @@ from heat.engine import scheduler
 from heat.engine.resource import Metadata
 from heat.openstack.common import timeutils
 from heat.tests.common import HeatTestCase
+from heat.tests import fakes
 from heat.tests.utils import setup_dummy_db
 from heat.tests.utils import parse_stack
 
@@ -99,6 +102,9 @@ class AutoScalingTest(HeatTestCase):
     def setUp(self):
         super(AutoScalingTest, self).setUp()
         setup_dummy_db()
+        cfg.CONF.set_default('heat_waitcondition_server_url',
+                             'http://127.0.0.1:8000/v1/waitcondition')
+        self.fc = fakes.FakeKeystoneClient()
 
     def create_scaling_group(self, t, stack, resource_name):
         rsrc = asc.AutoScalingGroup(resource_name,
@@ -113,7 +119,6 @@ class AutoScalingTest(HeatTestCase):
         rsrc = asc.ScalingPolicy(resource_name,
                                  t['Resources'][resource_name],
                                  stack)
-
         self.assertEqual(None, rsrc.validate())
         scheduler.TaskRunner(rsrc.create)()
         self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
@@ -930,6 +935,7 @@ class AutoScalingTest(HeatTestCase):
         now = timeutils.utcnow()
         self._stub_meta_expected(now, 'ExactCapacity : 1')
         self._stub_create(1)
+
         self.m.ReplayAll()
         rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
         stack.resources['WebServerGroup'] = rsrc
@@ -939,9 +945,17 @@ class AutoScalingTest(HeatTestCase):
         self._stub_lb_reload(2)
         self._stub_meta_expected(now, 'ChangeInCapacity : 1', 2)
         self._stub_create(1)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         up_policy = self.create_scaling_policy(t, stack,
                                                'WebServerScaleUpPolicy')
+
+        alarm_url = up_policy.FnGetAtt('AlarmUrl')
+        self.assertNotEqual(None, alarm_url)
         up_policy.signal()
         self.assertEqual('WebServerGroup-0,WebServerGroup-1',
                          rsrc.resource_id)
@@ -969,6 +983,11 @@ class AutoScalingTest(HeatTestCase):
         # Scale down one
         self._stub_lb_reload(1)
         self._stub_meta_expected(now, 'ChangeInCapacity : -1', 2)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         down_policy = self.create_scaling_policy(t, stack,
                                                  'WebServerScaleDownPolicy')
@@ -996,6 +1015,11 @@ class AutoScalingTest(HeatTestCase):
         self._stub_lb_reload(2)
         self._stub_meta_expected(now, 'ChangeInCapacity : 1', 2)
         self._stub_create(1)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         up_policy = self.create_scaling_policy(t, stack,
                                                'WebServerScaleUpPolicy')
@@ -1047,6 +1071,11 @@ class AutoScalingTest(HeatTestCase):
         self._stub_lb_reload(2)
         self._stub_meta_expected(now, 'ChangeInCapacity : 1', 2)
         self._stub_create(1)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         up_policy = self.create_scaling_policy(t, stack,
                                                'WebServerScaleUpPolicy')
@@ -1099,6 +1128,11 @@ class AutoScalingTest(HeatTestCase):
         self._stub_lb_reload(2)
         self._stub_meta_expected(now, 'ChangeInCapacity : 1', 2)
         self._stub_create(1)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         up_policy = self.create_scaling_policy(t, stack,
                                                'WebServerScaleUpPolicy')
@@ -1151,6 +1185,11 @@ class AutoScalingTest(HeatTestCase):
         now = timeutils.utcnow()
         self._stub_meta_expected(now, 'ChangeInCapacity : 1', 2)
         self._stub_create(1)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         up_policy = self.create_scaling_policy(t, stack,
                                                'WebServerScaleUpPolicy')
@@ -1190,6 +1229,11 @@ class AutoScalingTest(HeatTestCase):
         now = timeutils.utcnow()
         self._stub_meta_expected(now, 'ExactCapacity : 1')
         self._stub_create(1)
+
+        self.m.StubOutWithMock(asc.ScalingPolicy, 'keystone')
+        asc.ScalingPolicy.keystone().MultipleTimes().AndReturn(
+            self.fc)
+
         self.m.ReplayAll()
         rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
         stack.resources['WebServerGroup'] = rsrc