From 3c53dc729fb71798c7e6a6b602b368cbb4b67991 Mon Sep 17 00:00:00 2001 From: doantungbk Date: Mon, 5 Jun 2017 09:23:10 -0700 Subject: [PATCH] Fix: nested resources cannot be given out In Tacker, we now are fixing Tacker scaling to fully leverage heat-translator. To make a best fit, Tacker need to have nested resources from heat-translator. Heat-translator already provided this by using translate_to_yaml_dict(). Unfortunately, this method always return to the main heat yaml file not to nested yaml file. Change-Id: I7144d9926141dc4fc70f3a3c73c7fd41e2ff288c --- translator/common/utils.py | 9 ++++++++- translator/hot/syntax/hot_template.py | 9 +++++---- translator/hot/tosca_translator.py | 5 +++-- translator/tests/test_tosca_hot_translation.py | 18 ++++++++++++++---- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/translator/common/utils.py b/translator/common/utils.py index 874c8ec9..85af60a5 100644 --- a/translator/common/utils.py +++ b/translator/common/utils.py @@ -216,7 +216,8 @@ class YamlUtils(object): class TranslationUtils(object): @staticmethod - def compare_tosca_translation_with_hot(tosca_file, hot_files, params): + def compare_tosca_translation_with_hot(tosca_file, hot_files, params, + nested_resources=False): '''Verify tosca translation against the given hot specification. inputs: @@ -247,6 +248,12 @@ class TranslationUtils(object): basename = os.path.basename(hot_files[0]) output_hot_templates = translate.translate_to_yaml_files_dict(basename) + + if nested_resources: + basename = os.path.basename(hot_files[0]) + output_hot_templates =\ + translate.translate_to_yaml_files_dict(basename, True) + output_dict = {} for output_hot_template_name in output_hot_templates: output_dict[output_hot_template_name] = \ diff --git a/translator/hot/syntax/hot_template.py b/translator/hot/syntax/hot_template.py index 7fae0220..f279997e 100644 --- a/translator/hot/syntax/hot_template.py +++ b/translator/hot/syntax/hot_template.py @@ -46,7 +46,8 @@ class HotTemplate(object): return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', nodes) def output_to_yaml_files_dict(self, base_filename, - hot_template_version=LATEST): + hot_template_version=LATEST, + embed_substack_templates=False): yaml_files_dict = {} base_filename, ext = os.path.splitext(base_filename) @@ -55,9 +56,9 @@ class HotTemplate(object): yaml_files_dict.update( resource.extract_substack_templates(base_filename, hot_template_version)) - - yaml_files_dict[base_filename + ext] = \ - self.output_to_yaml(hot_template_version, False) + if not embed_substack_templates: + yaml_files_dict[base_filename + ext] = \ + self.output_to_yaml(hot_template_version, False) return yaml_files_dict diff --git a/translator/hot/tosca_translator.py b/translator/hot/tosca_translator.py index b9d4c775..a637a8ca 100644 --- a/translator/hot/tosca_translator.py +++ b/translator/hot/tosca_translator.py @@ -71,7 +71,8 @@ class TOSCATranslator(object): return yaml_files["output.yaml"] - def translate_to_yaml_files_dict(self, base_filename): + def translate_to_yaml_files_dict(self, base_filename, + nested_resources=False): """Translate to HOT YAML This method produces a translated output containing main and @@ -82,7 +83,7 @@ class TOSCATranslator(object): self._translate_to_hot_yaml() return self.hot_template.output_to_yaml_files_dict( base_filename, - self.node_translator.hot_template_version) + self.node_translator.hot_template_version, nested_resources) def _translate_inputs(self): translator = TranslateInputs(self.tosca.inputs, self.parsed_params, diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index f67c86c6..95df72a7 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -26,14 +26,15 @@ from translator.tests.base import TestCase class ToscaHotTranslationTest(TestCase): - def _test_successful_translation(self, tosca_file, hot_files, params=None): + def _test_successful_translation(self, tosca_file, hot_files, params=None, + nested_resources=False): if not params: params = {} if not isinstance(hot_files, list): hot_files = [hot_files] - diff = TranslationUtils.compare_tosca_translation_with_hot(tosca_file, - hot_files, - params) + diff =\ + TranslationUtils.compare_tosca_translation_with_hot( + tosca_file, hot_files, params, nested_resources) self.assertEqual({}, diff, ' : ' + json.dumps(diff, indent=4, separators=(', ', ': '))) @@ -527,6 +528,15 @@ class ToscaHotTranslationTest(TestCase): params = {} self._test_successful_translation(tosca_file, hot_files, params) + def test_hot_translate_scaling_nested_plate(self): + tosca_file = '../tests/data/autoscaling/tosca_autoscaling.yaml' + hot_files = [ + '../tests/data/hot_output/autoscaling/asg_res.yaml' + ] + params = {} + self._test_successful_translation(tosca_file, hot_files, params, + nested_resources=True) + def test_translate_unsupported_tosca_type(self): tosca_file = '../tests/data/test_tosca_unsupported_type.yaml' tosca_tpl = os.path.normpath(os.path.join(