diff --git a/toscaparser/imports.py b/toscaparser/imports.py index 266d27e9..2caf7c60 100644 --- a/toscaparser/imports.py +++ b/toscaparser/imports.py @@ -34,11 +34,12 @@ class ImportsLoader(object): 'namespace_prefix') def __init__(self, importslist, path, type_definition_list=None, - tpl=None): + tpl=None, local_defs=None): self.importslist = importslist self.custom_defs = {} self.nested_tosca_tpls = [] self.nested_imports = {} + self.local_defs = local_defs if not path and not tpl: msg = _('Input tosca template is not provided.') log.warning(msg) @@ -197,7 +198,14 @@ class ImportsLoader(object): return None, None if toscaparser.utils.urlutils.UrlUtils.validate_url(file_name): - return file_name, YAML_LOADER(file_name, False) + has_file = False + if self.local_defs is not None: + for k in self.local_defs.keys(): + if k == file_name: + file_name = self.local_defs[k] + has_file = True + + return file_name, YAML_LOADER(file_name, a_file=has_file) elif not repository: import_template = None if self.path: diff --git a/toscaparser/tests/test_toscatpl.py b/toscaparser/tests/test_toscatpl.py index 8ddc0bd3..203305e4 100644 --- a/toscaparser/tests/test_toscatpl.py +++ b/toscaparser/tests/test_toscatpl.py @@ -1013,3 +1013,33 @@ class ToscaTemplateTest(TestCase): "data/policies/test_policies_without_required_property.yaml") self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl, None) + + def test_local_custom_defs(self): + """Compare if custom defs on local and remote the same.""" + + tosca_tpl = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "data/tosca_single_instance_wordpress_with_url_import.yaml") + + local_def = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "data/custom_types/wordpress.yaml") + remote_def = ( + "https://raw.githubusercontent.com/openstack/" + "tosca-parser/master/toscaparser/tests/data/custom_types/" + "wordpress.yaml") + + local_defs = {remote_def: local_def} + params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user', + 'db_root_pwd': '12345678'} + tosca = ToscaTemplate(tosca_tpl, parsed_params=params) + tosca_local = ToscaTemplate(tosca_tpl, parsed_params=params, + local_defs=local_defs) + + # Compare the name of input params defined in the custom defs + expected = ["wp_db_name", "wp_db_user", "wp_db_password"] + for t in [tosca, tosca_local]: + actual = list( + (t.tpl["topology_template"]["node_templates"]["wordpress"] + ["interfaces"]["Standard"]["configure"]["inputs"]).keys()) + self.assertEqual(expected, actual) diff --git a/toscaparser/tests/test_toscatplvalidation.py b/toscaparser/tests/test_toscatplvalidation.py index 1acfacd8..0b394756 100644 --- a/toscaparser/tests/test_toscatplvalidation.py +++ b/toscaparser/tests/test_toscatplvalidation.py @@ -449,6 +449,28 @@ heat-translator/master/translator/tests/data/custom_types/wordpress.yaml self.assertTrue(custom_defs.get("mycompany.tosca.nodes." "WebApplication.WordPress")) + def test_imports_with_local_defs(self): + """Compare custom types on local and remote.""" + + ctypes = { + "remote": ("https://raw.githubusercontent.com/openstack/" + "heat-translator/master/translator/tests/data/" + "custom_types/wordpress.yaml"), + "local": "../data/wordpress.yaml"} + + tpl_snippet = ''' + imports: + - {} + '''.format(ctypes["remote"]) + local_defs = {ctypes["remote"]: ctypes["local"]} + + path = 'toscaparser/tests/data/tosca_elk.yaml' + imports = (toscaparser.utils.yamlparser. + simple_parse(tpl_snippet)['imports']) + ld1 = ImportsLoader(imports, path, "node_types") + ld2 = ImportsLoader(imports, path, "node_types", local_defs) + self.assertEqual(ld1.get_custom_defs(), ld2.get_custom_defs()) + def test_imports_file_with_suffix_yml(self): tpl_snippet = ''' imports: diff --git a/toscaparser/tosca_template.py b/toscaparser/tosca_template.py index 1671f7e4..8cb5e633 100644 --- a/toscaparser/tosca_template.py +++ b/toscaparser/tosca_template.py @@ -67,7 +67,7 @@ class ToscaTemplate(object): '''Load the template data.''' def __init__(self, path=None, parsed_params=None, a_file=True, - yaml_dict_tpl=None): + yaml_dict_tpl=None, local_defs=None): ExceptionCollector.start() self.a_file = a_file @@ -76,6 +76,8 @@ class ToscaTemplate(object): self.tpl = None self.nested_tosca_tpls_with_topology = {} self.nested_tosca_templates_with_topology = [] + self.local_defs = local_defs + if path: self.input_path = path self.path = self._get_path(path) @@ -209,7 +211,8 @@ class ToscaTemplate(object): if imports: custom_service = toscaparser.imports.\ - ImportsLoader(imports, path, type_defs, self.tpl) + ImportsLoader(imports, path, type_defs, self.tpl, + self.local_defs) nested_tosca_tpls = custom_service.get_nested_tosca_tpls() self._update_nested_tosca_tpls_with_topology(nested_tosca_tpls)