Included namespace_prefix logic in toscaparser

if namespace_prefix is provided in imports, the full name will be
namespace_prefix.type while importing type definition from namespace.

Partially Implements: blueprint tosca-namespaces

Change-Id: I8fb43a6db2fc040713f41c75a80e27abbe451ea1
This commit is contained in:
srinivas_tadepalli 2015-11-30 20:20:52 +05:30
parent 17feff6ca5
commit 0a4c6a0503
6 changed files with 50 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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