From 820f9c8ec4d1d56ecabe2e94dccd7d9dc92bee2a Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 1 Mar 2016 09:31:35 +0100 Subject: [PATCH] Solves an error in the _create_interfaces function in case of custom relationships defined in TOSCA document. Change-Id: Id780d8a3ddea6f85bc555644b933c1782d7d98e2 Related-Bug: 1551608 --- toscaparser/entity_template.py | 23 +++++++++++-------- .../test_tosca_custom_rel_with_script.yaml | 23 +++++++++++++++++++ toscaparser/tests/test_toscatpl.py | 9 ++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 toscaparser/tests/data/test_tosca_custom_rel_with_script.yaml diff --git a/toscaparser/entity_template.py b/toscaparser/entity_template.py index e23cdfa..281012b 100644 --- a/toscaparser/entity_template.py +++ b/toscaparser/entity_template.py @@ -283,16 +283,19 @@ class EntityTemplate(object): type_interfaces = None if isinstance(self.type_definition, RelationshipType): if isinstance(self.entity_tpl, dict): - for rel_def, value in self.entity_tpl.items(): - if rel_def != 'type': - rel_def = self.entity_tpl.get(rel_def) - rel = None - if isinstance(rel_def, dict): - rel = rel_def.get('relationship') - if rel: - if self.INTERFACES in rel: - type_interfaces = rel[self.INTERFACES] - break + if self.INTERFACES in self.entity_tpl: + type_interfaces = self.entity_tpl[self.INTERFACES] + else: + for rel_def, value in self.entity_tpl.items(): + if rel_def != 'type': + rel_def = self.entity_tpl.get(rel_def) + rel = None + if isinstance(rel_def, dict): + rel = rel_def.get('relationship') + if rel: + if self.INTERFACES in rel: + type_interfaces = rel[self.INTERFACES] + break else: type_interfaces = self.type_definition.get_value(self.INTERFACES, self.entity_tpl) diff --git a/toscaparser/tests/data/test_tosca_custom_rel_with_script.yaml b/toscaparser/tests/data/test_tosca_custom_rel_with_script.yaml new file mode 100644 index 0000000..18a94a3 --- /dev/null +++ b/toscaparser/tests/data/test_tosca_custom_rel_with_script.yaml @@ -0,0 +1,23 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: Test template of a custom relationship with a configure script + +topology_template: + + node_templates: + apache: + type: tosca.nodes.WebServer + requirements: + - host: + node: web_server + relationship: my_custom_rel + + web_server: + type: tosca.nodes.Compute + + relationship_templates: + my_custom_rel: + type: HostedOn + interfaces: + Configure: + pre_configure_source: scripts/wp_db_configure.sh diff --git a/toscaparser/tests/test_toscatpl.py b/toscaparser/tests/test_toscatpl.py index 683d171..bd70320 100644 --- a/toscaparser/tests/test_toscatpl.py +++ b/toscaparser/tests/test_toscatpl.py @@ -716,3 +716,12 @@ class ToscaTemplateTest(TestCase): os.path.dirname(os.path.abspath(__file__)), "data/test_custom_caps_def.yaml") ToscaTemplate(tosca_tpl) + + def test_custom_rel_with_script(self): + tosca_tpl = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "data/test_tosca_custom_rel_with_script.yaml") + tosca = ToscaTemplate(tosca_tpl) + rel = tosca.relationship_templates[0] + self.assertEqual(len(rel.interfaces), 1) + self.assertEqual(rel.interfaces[0].type, "Configure")