2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
14 from collections import OrderedDict
17 from toscaparser.utils.gettextutils import _
20 log = logging.getLogger('heat-translator')
23 class HotTemplate(object):
24 '''Container for full Heat Orchestration template.'''
26 SECTIONS = (VERSION, DESCRIPTION, PARAMETER_GROUPS, PARAMETERS,
27 RESOURCES, OUTPUTS, MAPPINGS) = \
28 ('heat_template_version', 'description', 'parameter_groups',
29 'parameters', 'resources', 'outputs', '__undefined__')
31 VERSIONS = (LATEST,) = ('2013-05-23',)
39 def represent_ordereddict(self, dumper, data):
41 for key, value in data.items():
42 node_key = dumper.represent_data(key)
43 node_value = dumper.represent_data(value)
44 nodes.append((node_key, node_value))
45 return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', nodes)
47 def output_to_yaml(self):
48 log.debug(_('Converting translated output to yaml format.'))
49 dict_output = OrderedDict()
51 version_string = self.VERSION + ": " + self.LATEST + "\n\n"
56 # Wrap the text to a new line if the line exceeds 80 characters.
57 wrapped_txt = "\n ".join(textwrap.wrap(self.description, 80))
58 desc_str = self.DESCRIPTION + ": >\n " + wrapped_txt + "\n\n"
61 all_params = OrderedDict()
62 for parameter in self.parameters:
63 all_params.update(parameter.get_dict_output())
64 dict_output.update({self.PARAMETERS: all_params})
67 all_resources = OrderedDict()
68 for resource in self.resources:
69 if not resource.hide_resource:
70 all_resources.update(resource.get_dict_output())
71 dict_output.update({self.RESOURCES: all_resources})
74 all_outputs = OrderedDict()
75 for output in self.outputs:
76 all_outputs.update(output.get_dict_output())
77 dict_output.update({self.OUTPUTS: all_outputs})
79 yaml.add_representer(OrderedDict, self.represent_ordereddict)
80 yaml_string = yaml.dump(dict_output, default_flow_style=False)
81 # get rid of the '' from yaml.dump around numbers
82 yaml_string = yaml_string.replace('\'', '')
83 return version_string + desc_str + yaml_string