From e2143adbf88576b86b18e0674d291911da62a92e Mon Sep 17 00:00:00 2001 From: spzala Date: Thu, 14 May 2015 04:33:35 -0700 Subject: [PATCH] TOSCA: interfaces for relationship templates TOSCA relationship interfaces are provided for various configurations to run on the host or target machine for pre, post configuration operations. Make such interfaces available via relationship templates. Change-Id: I6626e5baaa0ebc369d8e4ab74720e880d52926a7 --- translator/toscalib/elements/interfaces.py | 6 ++++-- translator/toscalib/entity_template.py | 15 +++++++++++++-- translator/toscalib/tests/test_toscatpl.py | 22 ++++++++++++---------- translator/toscalib/topology_template.py | 11 +++++++++++ 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/translator/toscalib/elements/interfaces.py b/translator/toscalib/elements/interfaces.py index ddebfcfa..1cd53341 100644 --- a/translator/toscalib/elements/interfaces.py +++ b/translator/toscalib/elements/interfaces.py @@ -29,14 +29,16 @@ class InterfacesDef(StatefulEntityType): node_template=None, name=None, value=None): self.ntype = node_type self.node_template = node_template - if self.INTERFACE_PREFIX not in interfacetype: - interfacetype = self.INTERFACE_PREFIX + interfacetype self.type = interfacetype self.name = name self.value = value self.implementation = None self.inputs = None self.defs = {} + if interfacetype == LIFECYCLE_SHORTNAME: + interfacetype = LIFECYCLE + if interfacetype == CONFIGURE_SHORTNAME: + interfacetype = CONFIGURE if node_type: self.defs = self.TOSCA_DEF[interfacetype] if value: diff --git a/translator/toscalib/entity_template.py b/translator/toscalib/entity_template.py index a25362c0..cc923ffa 100644 --- a/translator/toscalib/entity_template.py +++ b/translator/toscalib/entity_template.py @@ -218,8 +218,19 @@ class EntityTemplate(object): def _create_interfaces(self): interfaces = [] - type_interfaces = self.type_definition.get_value(self.INTERFACES, - self.entity_tpl) + 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 = self.entity_tpl.get(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) if type_interfaces: for interface_type, value in type_interfaces.items(): for op, op_def in value.items(): diff --git a/translator/toscalib/tests/test_toscatpl.py b/translator/toscalib/tests/test_toscatpl.py index 8d1d461e..d3094022 100644 --- a/translator/toscalib/tests/test_toscatpl.py +++ b/translator/toscalib/tests/test_toscatpl.py @@ -202,19 +202,21 @@ class ToscaTemplateTest(TestCase): def test_relationship_interface(self): template = ToscaTemplate(self.tosca_elk_tpl) for node_tpl in template.nodetemplates: - if node_tpl.name == 'nodejs': - config_interface = 'tosca.interfaces.relationship.Configure' + if node_tpl.name == 'logstash': + config_interface = 'Configure' + artifact = 'logstash/configure_elasticsearch.py' relation = node_tpl.relationships for key in relation.keys(): rel_tpl = relation.get(key).get_relationship_template() - interfaces = rel_tpl[0].interfaces - for interface in interfaces: - self.assertEqual(config_interface, - interface.type) - self.assertEqual('pre_configure_source', - interface.name) - self.assertEqual('nodejs/pre_configure_source.sh', - interface.implementation) + if rel_tpl: + interfaces = rel_tpl[0].interfaces + for interface in interfaces: + self.assertEqual(config_interface, + interface.type) + self.assertEqual('pre_configure_source', + interface.name) + self.assertEqual(artifact, + interface.implementation) def test_template_macro(self): template = ToscaTemplate(self.tosca_elk_tpl) diff --git a/translator/toscalib/topology_template.py b/translator/toscalib/topology_template.py index f40727ac..363f4c94 100644 --- a/translator/toscalib/topology_template.py +++ b/translator/toscalib/topology_template.py @@ -193,6 +193,17 @@ class TopologyTemplate(object): for p, v in cap._properties.items(): if p == prop.name: cap._properties[p] = propvalue + for rel, node in node_template.relationships.items(): + rel_tpls = node.relationship_tpl + if rel_tpls: + for rel_tpl in rel_tpls: + for interface in rel_tpl.interfaces: + if interface.inputs: + for name, value in interface.inputs.items(): + interface.inputs[name] = \ + functions.get_function(self, + rel_tpl, + value) for output in self.outputs: func = functions.get_function(self, self.outputs, output.value) if isinstance(func, functions.GetAttribute):