Support importing def from local instead of url
Sometimes importing a nested definition from remote server is failed for the server is busy. Although we usually can download the definition before and change the path as local then, cannot for a case if the contents of the template is fixed and it should not be changed. This update is avoid such a case as a remedy. The url can be replaced with any other path by using arg `local_defs` like as below. In this case, the definition is imported from local path instead of url. local_defs = {"https://example.org/orig": "/path/to/local/def"} tosca = ToscaTemplate(template, local_defs=local_defs} Closes-Bug: #2006796 Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com> Change-Id: Iac88def836970cd976396452ffb9c3d276263d3e
This commit is contained in:
parent
dcff68fd8a
commit
16932ad1d5
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue