diff --git a/toscaparser/imports.py b/toscaparser/imports.py index e42e640a..9dbdb778 100644 --- a/toscaparser/imports.py +++ b/toscaparser/imports.py @@ -73,14 +73,19 @@ class ImportsLoader(object): custom_type = self._load_import_template(import_name, import_uri) - self._update_custom_def(custom_type) + namespace_prefix = None + if isinstance(import_uri, dict): + namespace_prefix = import_uri.get( + self.NAMESPACE_PREFIX) + + self._update_custom_def(custom_type, namespace_prefix) else: # old style of imports custom_type = self._load_import_template(None, import_def) if custom_type: - self._update_custom_def(custom_type) + self._update_custom_def(custom_type, None) - def _update_custom_def(self, custom_type): + def _update_custom_def(self, custom_type, namespace_prefix): outer_custom_types = {} for type_def in self.type_definition_list: outer_custom_types = custom_type.get(type_def) @@ -88,7 +93,16 @@ class ImportsLoader(object): if type_def == "imports": self.custom_defs.update({'imports': outer_custom_types}) else: - self.custom_defs.update(outer_custom_types) + if namespace_prefix: + prefix_custom_types = {} + for type_def_key in outer_custom_types.keys(): + namespace_prefix_to_key = (namespace_prefix + + "." + type_def_key) + prefix_custom_types[namespace_prefix_to_key] = \ + outer_custom_types[type_def_key] + self.custom_defs.update(prefix_custom_types) + else: + self.custom_defs.update(outer_custom_types) def _validate_import_keys(self, import_name, import_uri_def): if self.FILE not in import_uri_def.keys(): @@ -133,9 +147,6 @@ class ImportsLoader(object): file_name = import_uri_def.get(self.FILE) repository = import_uri_def.get(self.REPOSITORY) namespace_uri = import_uri_def.get(self.NAMESPACE_URI) - # TODO(anyone) : will be extended this namespace_prefix in - # the next patch after design discussion with PTL. - # namespace_prefix = import_uri_def.get(self.NAMESPACE_PREFIX) else: file_name = import_uri_def namespace_uri = None diff --git a/toscaparser/tests/data/custom_types/nested_rsyslog.yaml b/toscaparser/tests/data/custom_types/nested_rsyslog.yaml index 622acab8..8c04171f 100644 --- a/toscaparser/tests/data/custom_types/nested_rsyslog.yaml +++ b/toscaparser/tests/data/custom_types/nested_rsyslog.yaml @@ -8,7 +8,7 @@ imports: file: custom_types/logstash.yaml node_types: - tosca.nodes.SoftwareComponent.Rsyslog: + Rsyslog: derived_from: tosca.nodes.SoftwareComponent requirements: - log_endpoint: diff --git a/toscaparser/tests/data/custom_types/nested_test_wordpress.yaml b/toscaparser/tests/data/custom_types/nested_test_wordpress.yaml index 294bcc97..4df277db 100644 --- a/toscaparser/tests/data/custom_types/nested_test_wordpress.yaml +++ b/toscaparser/tests/data/custom_types/nested_test_wordpress.yaml @@ -1,9 +1,18 @@ tosca_definitions_version: tosca_simple_yaml_1_0 imports: - - rsyslog: custom_types/nested_rsyslog.yaml + - test_prefix_defs: + file: custom_types/nested_rsyslog.yaml + namespace_prefix: test_namespace_prefix + - test_second_time_with_another_prefix: + file: custom_types/nested_rsyslog.yaml + namespace_prefix: test_2nd_namespace_prefix + node_types: tosca.nodes.SoftwareComponent.Rsyslog.TestRsyslogType: - derived_from: tosca.nodes.SoftwareComponent.Rsyslog + derived_from: test_namespace_prefix.Rsyslog + + Test2ndRsyslogType: + derived_from: test_2nd_namespace_prefix.Rsyslog tosca.nodes.WebApplication.WordPress: derived_from: tosca.nodes.WebApplication diff --git a/toscaparser/tests/data/test_instance_nested_imports.yaml b/toscaparser/tests/data/test_instance_nested_imports.yaml index 6b6542c3..6aa93070 100644 --- a/toscaparser/tests/data/test_instance_nested_imports.yaml +++ b/toscaparser/tests/data/test_instance_nested_imports.yaml @@ -16,7 +16,7 @@ topology_template: type: tosca.nodes.SoftwareComponent.Rsyslog.TestRsyslogType rsyslog: - type: tosca.nodes.SoftwareComponent.Rsyslog + type: Test2ndRsyslogType logstash: type: tosca.nodes.SoftwareComponent.Logstash diff --git a/toscaparser/tests/test_toscatpl.py b/toscaparser/tests/test_toscatpl.py index 4372a83d..73bf6d43 100644 --- a/toscaparser/tests/test_toscatpl.py +++ b/toscaparser/tests/test_toscatpl.py @@ -443,7 +443,9 @@ class ToscaTemplateTest(TestCase): "data/test_instance_nested_imports.yaml") tosca = ToscaTemplate(tosca_tpl) expected_custom_types = ['tosca.nodes.WebApplication.WordPress', - 'tosca.nodes.SoftwareComponent.Rsyslog', + 'test_namespace_prefix.Rsyslog', + 'Test2ndRsyslogType', + 'test_2nd_namespace_prefix.Rsyslog', 'tosca.nodes.SoftwareComponent.Logstash', 'tosca.nodes.SoftwareComponent.Rsyslog.' 'TestRsyslogType'] diff --git a/toscaparser/tests/test_toscatplvalidation.py b/toscaparser/tests/test_toscatplvalidation.py index 714534ed..7c36cfd1 100644 --- a/toscaparser/tests/test_toscatplvalidation.py +++ b/toscaparser/tests/test_toscatplvalidation.py @@ -132,8 +132,21 @@ tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml custom_defs = self._imports_content_test(tpl_snippet, path, "node_types") - self.assertTrue(custom_defs.get("tosca.nodes." - "WebApplication.WordPress")) + self.assertTrue(custom_defs.get("single_instance_wordpress.tosca." + "nodes.WebApplication.WordPress")) + + def test_imports_wth_namespace_prefix(self): + tpl_snippet = ''' + imports: + - more_definitions: + file: custom_types/nested_rsyslog.yaml + namespace_prefix: testprefix + ''' + path = 'toscaparser/tests/data/tosca_elk.yaml' + custom_defs = self._imports_content_test(tpl_snippet, + path, + "node_types") + self.assertTrue(custom_defs.get("testprefix.Rsyslog")) def test_imports_with_no_main_template(self): tpl_snippet = ''' @@ -187,7 +200,6 @@ tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml - more_definitions: file: https://raw.githubusercontent.com/openstack/\ tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml - namespace_prefix: mycompany ''' path = 'https://raw.githubusercontent.com/openstack/\ tosca-parser/master/toscaparser/tests/data/\ @@ -211,7 +223,7 @@ custom_types/wordpress.yaml custom_defs = self._imports_content_test(tpl_snippet, path, "node_types") - self.assertTrue(custom_defs.get("tosca.nodes." + self.assertTrue(custom_defs.get("mycompany.tosca.nodes." "WebApplication.WordPress")) def test_import_error_namespace_uri(self):