/
template.py
281 lines (234 loc) · 11.1 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
import six
from heat.common import exception
from heat.common.i18n import _
from heat.engine.cfn import functions as cfn_funcs
from heat.engine.cfn import template as cfn_template
from heat.engine import function
from heat.engine.hot import functions as hot_funcs
from heat.engine.hot import parameters
from heat.engine import rsrc_defn
from heat.engine import template
_RESOURCE_KEYS = (
RES_TYPE, RES_PROPERTIES, RES_METADATA, RES_DEPENDS_ON,
RES_DELETION_POLICY, RES_UPDATE_POLICY,
) = (
'type', 'properties', 'metadata', 'depends_on',
'deletion_policy', 'update_policy',
)
class HOTemplate20130523(template.Template):
"""
A Heat Orchestration Template format stack template.
"""
SECTIONS = (VERSION, DESCRIPTION, PARAMETER_GROUPS, PARAMETERS,
RESOURCES, OUTPUTS, MAPPINGS) = \
('heat_template_version', 'description', 'parameter_groups',
'parameters', 'resources', 'outputs', '__undefined__')
SECTIONS_NO_DIRECT_ACCESS = set([PARAMETERS, VERSION])
_CFN_TO_HOT_SECTIONS = {cfn_template.CfnTemplate.VERSION: VERSION,
cfn_template.CfnTemplate.DESCRIPTION: DESCRIPTION,
cfn_template.CfnTemplate.PARAMETERS: PARAMETERS,
cfn_template.CfnTemplate.MAPPINGS: MAPPINGS,
cfn_template.CfnTemplate.RESOURCES: RESOURCES,
cfn_template.CfnTemplate.OUTPUTS: OUTPUTS}
functions = {
'Fn::GetAZs': cfn_funcs.GetAZs,
'get_param': hot_funcs.GetParam,
'get_resource': cfn_funcs.ResourceRef,
'Ref': cfn_funcs.Ref,
'get_attr': hot_funcs.GetAttThenSelect,
'Fn::Select': cfn_funcs.Select,
'Fn::Join': cfn_funcs.Join,
'list_join': hot_funcs.Join,
'Fn::Split': cfn_funcs.Split,
'str_replace': hot_funcs.Replace,
'Fn::Replace': cfn_funcs.Replace,
'Fn::Base64': cfn_funcs.Base64,
'Fn::MemberListToMap': cfn_funcs.MemberListToMap,
'resource_facade': hot_funcs.ResourceFacade,
'Fn::ResourceFacade': cfn_funcs.ResourceFacade,
'get_file': hot_funcs.GetFile,
}
def __getitem__(self, section):
""""Get the relevant section in the template."""
#first translate from CFN into HOT terminology if necessary
if section not in self.SECTIONS:
section = HOTemplate20130523._translate(
section, self._CFN_TO_HOT_SECTIONS,
_('"%s" is not a valid template section'))
if section not in self.SECTIONS:
raise KeyError(_('"%s" is not a valid template section') % section)
if section in self.SECTIONS_NO_DIRECT_ACCESS:
raise KeyError(
_('Section %s can not be accessed directly.') % section)
if section == self.MAPPINGS:
return {}
if section == self.DESCRIPTION:
default = 'No description'
else:
default = {}
# if a section is None (empty yaml section) return {}
# to be consistent with an empty json section.
the_section = self.t.get(section) or default
# In some cases (e.g. parameters), also translate each entry of
# a section into CFN format (case, naming, etc) so the rest of the
# engine can cope with it.
# This is a shortcut for now and might be changed in the future.
if section == self.RESOURCES:
return self._translate_resources(the_section)
if section == self.OUTPUTS:
return self._translate_outputs(the_section)
return the_section
@staticmethod
def _translate(value, mapping, err_msg=None):
try:
return mapping[value]
except KeyError as ke:
if err_msg:
raise KeyError(err_msg % value)
else:
raise ke
def _translate_section(self, section, sub_section, data, mapping):
cfn_objects = {}
obj_name = section[:-1]
err_msg = _('"%%s" is not a valid keyword inside a %s '
'definition') % obj_name
for name, attrs in six.iteritems(data):
cfn_object = {}
if not attrs:
args = {'object_name': obj_name, 'sub_section': sub_section}
message = _('Each %(object_name)s must contain a '
'%(sub_section)s key.') % args
raise exception.StackValidationFailed(message=message)
try:
for attr, attr_value in six.iteritems(attrs):
cfn_attr = self._translate(attr, mapping, err_msg)
cfn_object[cfn_attr] = attr_value
cfn_objects[name] = cfn_object
except AttributeError:
message = _('"%(section)s" must contain a map of '
'%(obj_name)s maps. Found a [%(_type)s] '
'instead') % {'section': section,
'_type': type(attrs),
'obj_name': obj_name}
raise exception.StackValidationFailed(message=message)
return cfn_objects
def _translate_resources(self, resources):
"""Get the resources of the template translated into CFN format."""
HOT_TO_CFN_ATTRS = {'type': 'Type',
'properties': 'Properties',
'metadata': 'Metadata',
'depends_on': 'DependsOn',
'deletion_policy': 'DeletionPolicy',
'update_policy': 'UpdatePolicy'}
return self._translate_section('resources', 'type', resources,
HOT_TO_CFN_ATTRS)
def _translate_outputs(self, outputs):
"""Get the outputs of the template translated into CFN format."""
HOT_TO_CFN_ATTRS = {'description': 'Description',
'value': 'Value'}
return self._translate_section('outputs', 'value', outputs,
HOT_TO_CFN_ATTRS)
def param_schemata(self):
parameter_section = self.t.get(self.PARAMETERS)
if parameter_section is None:
parameter_section = {}
params = six.iteritems(parameter_section)
return dict((name, parameters.HOTParamSchema.from_dict(name, schema))
for name, schema in params)
def parameters(self, stack_identifier, user_params):
return parameters.HOTParameters(stack_identifier, self,
user_params=user_params)
def resource_definitions(self, stack):
allowed_keys = set(_RESOURCE_KEYS)
def rsrc_defn_item(name, snippet):
data = self.parse(stack, snippet)
def get_check_type(key, valid_types, typename, default=None):
if key in data:
field = data[key]
if not isinstance(field, valid_types):
args = {'name': name, 'key': key, 'typename': typename}
msg = _('Resource %(name)s %(key)s type '
'must be %(typename)s') % args
raise TypeError(msg)
return field
else:
return default
resource_type = get_check_type(RES_TYPE,
six.string_types,
'string')
if resource_type is None:
args = {'name': name, 'type_key': RES_TYPE}
msg = _('Resource %(name)s is missing "%(type_key)s"') % args
raise KeyError(msg)
properties = get_check_type(RES_PROPERTIES,
(collections.Mapping,
function.Function),
'object')
metadata = get_check_type(RES_METADATA,
(collections.Mapping,
function.Function),
'object')
depends = get_check_type(RES_DEPENDS_ON,
collections.Sequence,
'list or string',
default=[])
if isinstance(depends, six.string_types):
depends = [depends]
deletion_policy = get_check_type(RES_DELETION_POLICY,
six.string_types,
'string')
update_policy = get_check_type(RES_UPDATE_POLICY,
(collections.Mapping,
function.Function),
'object')
for key in data:
if key not in allowed_keys:
raise ValueError(_('"%s" is not a valid keyword '
'inside a resource definition') % key)
defn = rsrc_defn.ResourceDefinition(name, resource_type,
properties, metadata,
depends,
deletion_policy,
update_policy)
return name, defn
resources = self.t.get(self.RESOURCES) or {}
return dict(rsrc_defn_item(name, data)
for name, data in resources.items())
def add_resource(self, definition, name=None):
if name is None:
name = definition.name
if self.t.get(self.RESOURCES) is None:
self.t[self.RESOURCES] = {}
self.t[self.RESOURCES][name] = definition.render_hot()
class HOTemplate20141016(HOTemplate20130523):
functions = {
'get_attr': hot_funcs.GetAtt,
'get_file': hot_funcs.GetFile,
'get_param': hot_funcs.GetParam,
'get_resource': cfn_funcs.ResourceRef,
'list_join': hot_funcs.Join,
'resource_facade': hot_funcs.ResourceFacade,
'str_replace': hot_funcs.Replace,
'Fn::Select': cfn_funcs.Select,
# functions removed from 20130523
'Fn::GetAZs': hot_funcs.Removed,
'Fn::Join': hot_funcs.Removed,
'Fn::Split': hot_funcs.Removed,
'Fn::Replace': hot_funcs.Removed,
'Fn::Base64': hot_funcs.Removed,
'Fn::MemberListToMap': hot_funcs.Removed,
'Fn::ResourceFacade': hot_funcs.Removed,
'Ref': hot_funcs.Removed,
}