Use add_resource() to build autoscaling templates

Instead of building nested templates for Autoscaling manually, use the
Template.add_resource() method to build them up programmatically.

Change-Id: Ie4d6a869355cf0ddb6fce0edc6c7d49b8a2b7e75
This commit is contained in:
Zane Bitter 2014-04-28 18:35:48 -04:00
parent 69187601c7
commit 8998897425
3 changed files with 31 additions and 33 deletions

View File

@ -23,7 +23,6 @@ from heat.engine import constraints
from heat.engine import environment
from heat.engine import function
from heat.engine.notification import autoscaling as notification
from heat.engine import parser
from heat.engine import properties
from heat.engine.properties import Properties
from heat.engine import resource
@ -303,7 +302,9 @@ class InstanceGroup(stack_resource.StackResource):
return [(instance.name, instance.t)
for instance in self.get_instances()]
def _create_template(self, num_instances, num_replace=0):
def _create_template(self, num_instances, num_replace=0,
template_version=('HeatTemplateFormatVersion',
'2012-12-12')):
"""
Create a template to represent autoscaled instances.
@ -311,10 +312,10 @@ class InstanceGroup(stack_resource.StackResource):
"""
instance_definition = self._get_instance_definition()
old_resources = self._get_instance_templates()
templates = template.resource_templates(
definitions = template.resource_templates(
old_resources, instance_definition, num_instances, num_replace)
return {"HeatTemplateFormatVersion": "2012-12-12",
"Resources": dict((n, dict(d)) for n, d in templates)}
return template.make_template(definitions, version=template_version)
def _try_rolling_update(self, prop_diff):
if (self.update_policy[self.ROLLING_UPDATE] and
@ -330,7 +331,6 @@ class InstanceGroup(stack_resource.StackResource):
Replace the instances in the group using updated launch configuration
"""
def changing_instances(tmpl):
tmpl = parser.Template(tmpl)
instances = self.get_instances()
current = set((i.name, i.t) for i in instances)
updated = set(tmpl.resource_definitions(self.nested()).items())
@ -933,32 +933,13 @@ class AutoScalingResourceGroup(AutoScalingGroup):
policy[self.MAX_BATCH_SIZE],
policy[self.PAUSE_TIME])
def _create_template(self, *args, **kwargs):
def _create_template(self, num_instances, num_replace=0,
template_version=('heat_template_version',
'2013-05-23')):
"""Create a template in the HOT format for the nested stack."""
tpl = super(AutoScalingResourceGroup, self)._create_template(
*args, **kwargs)
CFN_TO_HOT_ATTRS = {'Type': 'type',
'Properties': 'properties',
'Metadata': 'metadata',
'DependsOn': 'depends_on',
'DeletionPolicy': 'deletion_policy',
'UpdatePolicy': 'update_policy'}
def to_hot(template):
hot_template = {}
for attr, attr_value in template.iteritems():
hot_attr = CFN_TO_HOT_ATTRS.get(attr, attr)
hot_template[hot_attr] = attr_value
return hot_template
tpl.pop('HeatTemplateFormatVersion', None)
tpl['heat_template_version'] = '2013-05-23'
rsrcs = tpl.pop('Resources')
tpl['resources'] = dict((n, to_hot(t)) for n, t in rsrcs.items())
return tpl
return super(AutoScalingResourceGroup,
self)._create_template(num_instances, num_replace,
template_version=template_version)
class ScalingPolicy(signal_responder.SignalResponder, CooldownMixin):

View File

@ -13,6 +13,8 @@
from heat.common import short_id
from heat.engine import template
def resource_templates(old_resources, resource_definition,
num_resources, num_replace):
@ -36,3 +38,19 @@ def resource_templates(old_resources, resource_definition,
yield old_name, old_template
else:
yield short_id.generate_id(), resource_definition
def make_template(resource_definitions,
version=('heat_template_version', '2013-05-23')):
"""
Return a Template object containing the given resource definitions.
By default, the template will be in the HOT format. A different format
can be specified by passing a (version_type, version_string) tuple matching
any of the available template format plugins.
"""
tmpl = template.Template(dict([version]))
for name, defn in resource_definitions:
tmpl.add_resource(defn, name)
return tmpl

View File

@ -24,7 +24,6 @@ from heat.engine import clients
from heat.engine import resource
from heat.engine import scheduler
from heat.engine import stack_resource
from heat.engine import template
from heat.openstack.common import timeutils
from heat.tests.common import HeatTestCase
from heat.tests import fakes
@ -427,7 +426,7 @@ class RollingUpdatesTest(HeatTestCase):
def update_with_template(tmpl, env):
# keep track of the new updates to resources _in creation order_.
definitions = template.Template(tmpl).resource_definitions(stack)
definitions = tmpl.resource_definitions(stack)
templates = [definitions[name] for name in created_order]
batches.append(templates)
patcher = mock.patch.object(