diff --git a/translator/common/utils.py b/translator/common/utils.py index 0ecfec1e..016a4eb3 100644 --- a/translator/common/utils.py +++ b/translator/common/utils.py @@ -246,7 +246,7 @@ class TranslationUtils(object): translate = TOSCATranslator(tosca, params) basename = os.path.basename(hot_files[0]) - output_hot_templates = translate.output_to_yaml_files_dict(basename) + output_hot_templates = translate.translate_to_yaml_files_dict(basename) output_dict = {} for output_hot_template_name in output_hot_templates: output_dict[output_hot_template_name] = \ diff --git a/translator/hot/tests/test_translate_outputs.py b/translator/hot/tests/test_translate_outputs.py index f571ce1a..955150e5 100644 --- a/translator/hot/tests/test_translate_outputs.py +++ b/translator/hot/tests/test_translate_outputs.py @@ -26,7 +26,7 @@ class ToscaTemplateOutputTest(TestCase): "tosca_nodejs_mongodb_two_instances.yaml") tosca = ToscaTemplate(tosca_tpl) translate = TOSCATranslator(tosca, []) - hot_translation = translate.output_to_yaml() + hot_translation = translate.translate() expected_output = {'nodejs_url': {'description': 'URL for the nodejs ' diff --git a/translator/hot/tosca/tosca_policies_scaling.py b/translator/hot/tosca/tosca_policies_scaling.py index f2370419..9b380e61 100644 --- a/translator/hot/tosca/tosca_policies_scaling.py +++ b/translator/hot/tosca/tosca_policies_scaling.py @@ -79,6 +79,10 @@ class ToscaAutoscaling(HotResource): dict_res[res_name] yaml.add_representer(OrderedDict, self.represent_ordereddict) yaml.add_representer(dict, self.represent_ordereddict) + # TODO(mvelten) (spzala) remove writing the yaml file here, + # once embed_substack_templates is correctly implemented + with open(self.policy.name + '_res.yaml', 'w') as nested_tpl: + yaml.dump(template_dict, nested_tpl, default_flow_style=False) self.nested_template = { self.policy.name + '_res.yaml': yaml.dump(template_dict, default_flow_style=False) diff --git a/translator/hot/tosca_translator.py b/translator/hot/tosca_translator.py index 85a31ca9..ba66ea88 100644 --- a/translator/hot/tosca_translator.py +++ b/translator/hot/tosca_translator.py @@ -34,7 +34,7 @@ class TOSCATranslator(object): self.node_translator = None log.info(_('Initialized parmaters for translation.')) - def _translate(self): + def _translate_to_hot_yaml(self): self._resolve_input() self.hot_template.description = self.tosca.description self.hot_template.parameters = self._translate_inputs() @@ -47,13 +47,26 @@ class TOSCATranslator(object): if self.node_translator.hot_template_version is None: self.node_translator.hot_template_version = HotTemplate.LATEST - def output_to_yaml(self): - self._translate() + def translate(self): + """Translate to HOT YAML + + This method produces a translated output for main template. + The nested template, if any referenced by main, will be created + as a separate file. + """ + self._translate_to_hot_yaml() return self.hot_template.output_to_yaml( self.node_translator.hot_template_version) - def output_to_yaml_files_dict(self, base_filename): - self._translate() + def translate_to_yaml_files_dict(self, base_filename): + """Translate to HOT YAML + + This method produces a translated output containing main and + any nested templates referenced by main. This output can be + programmatically stored into different files by using key as + template name and value as template content. + """ + self._translate_to_hot_yaml() return self.hot_template.output_to_yaml_files_dict( base_filename, self.node_translator.hot_template_version) diff --git a/translator/osc/v1/translate.py b/translator/osc/v1/translate.py index ceb82c32..afe3ba26 100644 --- a/translator/osc/v1/translate.py +++ b/translator/osc/v1/translate.py @@ -98,7 +98,7 @@ class TranslateTemplate(command.Command): else: tosca = ToscaTemplate(path, parsed_params, a_file) translator = TOSCATranslator(tosca, parsed_params) - output = translator.output_to_yaml() + output = translator.translate() else: msg = _('Could not find template file.\n') log.error(msg) diff --git a/translator/shell.py b/translator/shell.py index b9bd3830..b10da922 100644 --- a/translator/shell.py +++ b/translator/shell.py @@ -214,7 +214,7 @@ class TranslatorShell(object): msg = _('Deploy the generated template, the stack name is %(name)s.')\ % {'name': heat_stack_name} log.debug(msg) - tpl = yaml.load(translator.output_to_yaml()) + tpl = yaml.load(translator.translate()) # get all the values for get_file from a translated template get_files = [] @@ -286,20 +286,12 @@ class TranslatorShell(object): def _write_output(self, translator, output_file=None): if output_file: path, filename = os.path.split(output_file) - yaml_files = translator.output_to_yaml_files_dict(filename) + yaml_files = translator.translate_to_yaml_files_dict(filename) for name, content in six.iteritems(yaml_files): with open(os.path.join(path, name), 'w+') as f: f.write(content) else: - # TODO(mvelten) go back to calling output_to_yaml instead for - # stdout once embed_substack_templates is correctly implemented - # print(translator.output_to_yaml()) - yaml_files = translator.output_to_yaml_files_dict('output.yaml') - for name, content in six.iteritems(yaml_files): - if name != "output.yaml": - with open(name, 'w+') as f: - f.write(content) - print(yaml_files['output.yaml']) + print(translator.translate()) def main(args=None): diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index 64f4ce7b..baabfffb 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -529,7 +529,7 @@ class ToscaHotTranslationTest(TestCase): tosca = ToscaTemplate(tosca_tpl, params, True) err = self.assertRaises(UnsupportedTypeError, TOSCATranslator(tosca, params) - .output_to_yaml) + .translate) self.assertEqual(expected_msg, err.__str__()) def test_hot_translate_cluster_scaling_policy(self):