Provide support for legacy translate api

We should continue supporting translate api for backward compatibility.
For example, Tacker will need to use translate api.

Change-Id: I446d8bcc580b4bca5b56cc0e964745681ad10f1c
This commit is contained in:
Sahdev Zala 2017-01-05 13:49:12 -08:00
parent 7f02af1603
commit 45db426acb
7 changed files with 29 additions and 20 deletions

View File

@ -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] = \

View File

@ -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 '

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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):

View File

@ -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):