diff --git a/toscaparser/imports.py b/toscaparser/imports.py index ab11adb5..5149382b 100644 --- a/toscaparser/imports.py +++ b/toscaparser/imports.py @@ -41,6 +41,9 @@ class ImportsLoader(object): log.warning(msg) ExceptionCollector.appendException(ValidationError(message=msg)) self.path = path + self.repositories = {} + if tpl and tpl.get('repositories'): + self.repositories = tpl.get('repositories') self.type_definition_list = [] if type_definition_list: if isinstance(type_definition_list, list): @@ -153,10 +156,9 @@ class ImportsLoader(object): self._validate_import_keys(import_name, import_uri_def) file_name = import_uri_def.get(self.FILE) repository = import_uri_def.get(self.REPOSITORY) - namespace_uri = import_uri_def.get(self.NAMESPACE_URI) else: file_name = import_uri_def - namespace_uri = None + repository = None short_import_notation = True if not file_name: @@ -169,7 +171,7 @@ class ImportsLoader(object): if toscaparser.utils.urlutils.UrlUtils.validate_url(file_name): return YAML_LOADER(file_name, False) - elif not namespace_uri: + elif not repository: import_template = None if self.path: if toscaparser.utils.urlutils.UrlUtils.validate_url(self.path): @@ -238,21 +240,31 @@ class ImportsLoader(object): log.error(_('Import "%(name)s" is not valid.') % import_uri_def) ExceptionCollector.appendException( ImportError(_('Import "%s" is not valid.') % import_uri_def)) + return - # Remove leading, ending spaces and strip the last character if "/" - namespace_uri = ((namespace_uri).strip()).rstrip("//") + full_url = "" + if repository: + if self.repositories: + for repo_name, repo_def in self.repositories.items(): + if repo_name == repository: + # Remove leading, ending spaces and strip + # the last character if "/" + repo_url = ((repo_def['url']).strip()).rstrip("//") + full_url = repo_url + "/" + file_name - if toscaparser.utils.urlutils.UrlUtils.validate_url(namespace_uri): - full_url = None - if repository: - repository = ((repository).strip()).rstrip("//") - full_url = namespace_uri + "/" + repository + "/" + file_name - else: - full_url = namespace_uri + "/" + file_name + if not full_url: + msg = (_('referenced repository "%(n_uri)s" in import ' + 'definition "%(tpl)s" not found.') + % {'n_uri': repository, 'tpl': import_name}) + log.error(msg) + ExceptionCollector.appendException(ImportError(msg)) + return + + if toscaparser.utils.urlutils.UrlUtils.validate_url(full_url): return YAML_LOADER(full_url, False) else: - msg = (_('namespace_uri "%(n_uri)s" is not valid in import ' + msg = (_('repository url "%(n_uri)s" is not valid in import ' 'definition "%(tpl)s".') - % {'n_uri': namespace_uri, 'tpl': import_name}) + % {'n_uri': repo_url, 'tpl': import_name}) log.error(msg) ExceptionCollector.appendException(ImportError(msg)) diff --git a/toscaparser/tests/data/test_repositories_definition.yaml b/toscaparser/tests/data/test_repositories_definition.yaml index 09271a2f..2145d8f1 100644 --- a/toscaparser/tests/data/test_repositories_definition.yaml +++ b/toscaparser/tests/data/test_repositories_definition.yaml @@ -4,6 +4,13 @@ repositories: some_repository: description: Some repo url: https://raw.githubusercontent.com/openstack/tosca-parser/master/toscaparser/tests/data/custom_types/ + namespace_uri: http://docs.oasis-open.org/tosca/ns/simple/yaml/1.0a + namespace_prefix: oasis_tosca + +imports: + - some_import: + file: compute_with_prop.yaml + repository: some_repository description: > TOSCA test for testing repositories definition @@ -11,4 +18,6 @@ description: > node_templates: server: - type: tosca.nodes.Compute + type: tosca.nodes.ComputeWithProp + properties: + test: yes diff --git a/toscaparser/tests/test_toscatplvalidation.py b/toscaparser/tests/test_toscatplvalidation.py index c83a809e..4d2cd3f4 100644 --- a/toscaparser/tests/test_toscatplvalidation.py +++ b/toscaparser/tests/test_toscatplvalidation.py @@ -167,9 +167,8 @@ tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml imports: - some_definitions: custom_types/paypalpizzastore_nodejs_app.yaml - more_definitions: - file: toscaparser/tests/data/custom_types/wordpress.yaml - repository: tosca-parser/master - namespace_uri: https://raw.githubusercontent.com/openstack + file: 'https://raw.githubusercontent.com/openstack/tosca-parser\ +/master/toscaparser/tests/data/custom_types/wordpress.yaml' namespace_prefix: single_instance_wordpress ''' path = 'toscaparser/tests/data/tosca_elk.yaml' @@ -258,10 +257,10 @@ tosca_single_instance_wordpress_with_url_import.yaml' tpl_snippet = ''' imports: - more_definitions: - file: heat-translator/master/translator/tests/data/\ -custom_types/wordpress.yaml - namespace_uri: https://raw.githubusercontent.com/openstack/ - namespace_prefix: mycompany + file: https://raw.githubusercontent.com/openstack/\ +heat-translator/master/translator/tests/data/custom_types/wordpress.yaml + namespace_prefix: mycompany + namespace_uri: http://docs.oasis-open.org/tosca/ns/simple/yaml/1.0 ''' path = 'toscaparser/tests/data/tosca_elk.yaml' custom_defs = self._imports_content_test(tpl_snippet, @@ -270,21 +269,19 @@ custom_types/wordpress.yaml self.assertTrue(custom_defs.get("mycompany.tosca.nodes." "WebApplication.WordPress")) - def test_import_error_namespace_uri(self): + def test_import_error_file_uri(self): tpl_snippet = ''' imports: - more_definitions: - file: toscaparser/tests/data/tosca_elk.yaml - namespace_uri: mycompany.com/ns/tosca/2.0 - namespace_prefix: mycompany + file: mycompany.com/ns/tosca/2.0/toscaparser/tests/data\ +/tosca_elk.yaml + namespace_prefix: mycompany + namespace_uri: http://docs.oasis-open.org/tosca/ns/simple/yaml/1.0 ''' - errormsg = _('namespace_uri "mycompany.com/ns/tosca/2.0" is not ' - 'valid in import definition "more_definitions".') path = 'toscaparser/tests/data/tosca_elk.yaml' - err = self.assertRaises(ImportError, - self._imports_content_test, - tpl_snippet, path, None) - self.assertEqual(errormsg, err.__str__()) + self.assertRaises(ImportError, + self._imports_content_test, + tpl_snippet, path, None) def test_import_single_line_error(self): tpl_snippet = '''