Affinity and anti-affinity translate to heat resource type
[parser.git] / tosca2heat / heat-translator / translator / hot / syntax / hot_template.py
1 #
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
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
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
12 # under the License.
13
14 from collections import OrderedDict
15 import logging
16 import textwrap
17 from toscaparser.utils.gettextutils import _
18 import yaml
19
20 log = logging.getLogger('heat-translator')
21
22
23 class HotTemplate(object):
24     '''Container for full Heat Orchestration template.'''
25
26     SECTIONS = (VERSION, DESCRIPTION, PARAMETER_GROUPS, PARAMETERS,
27                 RESOURCES, OUTPUTS, MAPPINGS) = \
28                ('heat_template_version', 'description', 'parameter_groups',
29                 'parameters', 'resources', 'outputs', '__undefined__')
30
31     VERSIONS = (LATEST,) = ('2013-05-23',)
32
33     def __init__(self):
34         self.resources = []
35         self.outputs = []
36         self.parameters = []
37         self.description = ""
38
39     def represent_ordereddict(self, dumper, data):
40         nodes = []
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)
46
47     def output_to_yaml(self):
48         log.debug(_('Converting translated output to yaml format.'))
49         dict_output = OrderedDict()
50         # Version
51         version_string = self.VERSION + ": " + self.LATEST + "\n\n"
52
53         # Description
54         desc_str = ""
55         if self.description:
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"
59
60         # Parameters
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})
65
66         # Resources
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})
72
73         # Outputs
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})
78
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