Allow use of TOSCA types by short name in the TOSCA template
Let user access TOSCA normative types by referencing it's short name in the TOSCA service template. For example, tosca.nodes.Compute should be accessible by specifying just Compute. Change-Id: I448699895c2cd533835f94bca9fbc34a3712fbcf Closes-Bug: #1349588
This commit is contained in:
parent
51e5797ceb
commit
f2ba5fa317
@ -22,6 +22,10 @@ class CapabilityTypeDef(EntityType):
|
|||||||
|
|
||||||
def __init__(self, name, ctype, ntype, properties):
|
def __init__(self, name, ctype, ntype, properties):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
if self.CAPABILITY_PREFIX not in ctype:
|
||||||
|
ctype = self.CAPABILITY_PREFIX + ctype
|
||||||
|
if self.NODE_PREFIX not in ntype:
|
||||||
|
ntype = self.NODE_PREFIX + ntype
|
||||||
self.type = ctype
|
self.type = ctype
|
||||||
self.nodetype = ntype
|
self.nodetype = ntype
|
||||||
self.properties = properties
|
self.properties = properties
|
||||||
|
@ -37,6 +37,11 @@ class EntityType(object):
|
|||||||
'tosca.relationships.HostedOn',
|
'tosca.relationships.HostedOn',
|
||||||
'tosca.relationships.ConnectsTo')
|
'tosca.relationships.ConnectsTo')
|
||||||
|
|
||||||
|
NODE_PREFIX = 'tosca.nodes.'
|
||||||
|
RELATIONSHIP_PREFIX = 'tosca.relationships.'
|
||||||
|
CAPABILITY_PREFIX = 'tosca.capabilities.'
|
||||||
|
INTERFACE_PREFIX = 'tosca.interfaces.node.'
|
||||||
|
|
||||||
def derived_from(self, defs):
|
def derived_from(self, defs):
|
||||||
'''Return a type this type is derived from.'''
|
'''Return a type this type is derived from.'''
|
||||||
return self.entity_value(defs, 'derived_from')
|
return self.entity_value(defs, 'derived_from')
|
||||||
|
@ -31,6 +31,8 @@ class InterfacesDef(StatefulEntityType):
|
|||||||
node_template=None, name=None, value=None):
|
node_template=None, name=None, value=None):
|
||||||
self.ntype = node_type
|
self.ntype = node_type
|
||||||
self.node_template = node_template
|
self.node_template = node_template
|
||||||
|
if self.INTERFACE_PREFIX not in interfacetype:
|
||||||
|
interfacetype = self.INTERFACE_PREFIX + interfacetype
|
||||||
self.type = interfacetype
|
self.type = interfacetype
|
||||||
self.name = name
|
self.name = name
|
||||||
self.value = value
|
self.value = value
|
||||||
|
@ -33,6 +33,8 @@ class NodeType(StatefulEntityType):
|
|||||||
|
|
||||||
def __init__(self, ntype, custom_def=None):
|
def __init__(self, ntype, custom_def=None):
|
||||||
super(NodeType, self).__init__()
|
super(NodeType, self).__init__()
|
||||||
|
if self.NODE_PREFIX not in ntype:
|
||||||
|
ntype = self.NODE_PREFIX + ntype
|
||||||
if ntype in list(self.TOSCA_DEF.keys()):
|
if ntype in list(self.TOSCA_DEF.keys()):
|
||||||
self.defs = self.TOSCA_DEF[ntype]
|
self.defs = self.TOSCA_DEF[ntype]
|
||||||
elif custom_def and ntype in list(custom_def.keys()):
|
elif custom_def and ntype in list(custom_def.keys()):
|
||||||
|
@ -22,6 +22,8 @@ class RelationshipType(StatefulEntityType):
|
|||||||
def __init__(self, type, capability_name):
|
def __init__(self, type, capability_name):
|
||||||
super(RelationshipType, self).__init__()
|
super(RelationshipType, self).__init__()
|
||||||
self.defs = self.TOSCA_DEF[type]
|
self.defs = self.TOSCA_DEF[type]
|
||||||
|
if self.RELATIONSHIP_PREFIX not in type:
|
||||||
|
type = self.RELATIONSHIP_PREFIX + type
|
||||||
self.type = type
|
self.type = type
|
||||||
self.capability_name = capability_name
|
self.capability_name = capability_name
|
||||||
|
|
||||||
|
@ -41,10 +41,13 @@ class NodeTemplate(object):
|
|||||||
self.node_templates = node_templates
|
self.node_templates = node_templates
|
||||||
self._validate_field()
|
self._validate_field()
|
||||||
self.node_template = node_templates[self.name]
|
self.node_template = node_templates[self.name]
|
||||||
self.type = self.node_template[TYPE]
|
self.node_type = NodeType(self.node_template[TYPE], custom_def)
|
||||||
self.node_type = NodeType(self.type, custom_def)
|
|
||||||
self.related = {}
|
self.related = {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self):
|
||||||
|
return self.node_type.type
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def requirements(self):
|
def requirements(self):
|
||||||
return self.node_type.get_value(REQUIREMENTS, self.node_template)
|
return self.node_type.get_value(REQUIREMENTS, self.node_template)
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
tosca_definitions_version: tosca_simple_1.0
|
||||||
|
|
||||||
|
description: >
|
||||||
|
TOSCA simple profile with short type name for Compute.
|
||||||
|
|
||||||
|
inputs:
|
||||||
|
cpus:
|
||||||
|
type: integer
|
||||||
|
description: Number of CPUs for the server.
|
||||||
|
constraints:
|
||||||
|
- valid_values: [ 1, 2, 4, 8 ]
|
||||||
|
|
||||||
|
node_templates:
|
||||||
|
server:
|
||||||
|
type: Compute
|
||||||
|
properties:
|
||||||
|
# compute properties (flavor)
|
||||||
|
disk_size: 10
|
||||||
|
num_cpus: { get_input: cpus }
|
||||||
|
mem_size: 4096
|
||||||
|
# host image properties
|
||||||
|
os_arch: x86_64
|
||||||
|
os_type: Linux
|
||||||
|
os_distribution: Fedora
|
||||||
|
os_version: 18
|
||||||
|
|
||||||
|
outputs:
|
||||||
|
server_address:
|
||||||
|
description: IP address of server instance.
|
||||||
|
value: { get_property: [server, ip_address] }
|
@ -14,6 +14,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from translator.toscalib.elements.nodetype import NodeType
|
||||||
from translator.toscalib.functions import GetRefProperty
|
from translator.toscalib.functions import GetRefProperty
|
||||||
from translator.toscalib.tests.base import TestCase
|
from translator.toscalib.tests.base import TestCase
|
||||||
from translator.toscalib.tosca_template import ToscaTemplate
|
from translator.toscalib.tosca_template import ToscaTemplate
|
||||||
@ -138,3 +139,19 @@ class ToscaTemplateTest(TestCase):
|
|||||||
else:
|
else:
|
||||||
raise AssertionError(
|
raise AssertionError(
|
||||||
'Unexpected interface: {0}'.format(interface.name))
|
'Unexpected interface: {0}'.format(interface.name))
|
||||||
|
|
||||||
|
def test_normative_type_by_short_name(self):
|
||||||
|
#test template with a short name Compute
|
||||||
|
template = os.path.join(
|
||||||
|
os.path.dirname(os.path.abspath(__file__)),
|
||||||
|
"data/test_tosca_normative_type_by_shortname.yaml")
|
||||||
|
|
||||||
|
tosca_tpl = ToscaTemplate(template)
|
||||||
|
expected_type = "tosca.nodes.Compute"
|
||||||
|
for tpl in tosca_tpl.nodetemplates:
|
||||||
|
self.assertEqual(tpl.type, expected_type)
|
||||||
|
for tpl in tosca_tpl.nodetemplates:
|
||||||
|
compute_type = NodeType(tpl.type)
|
||||||
|
self.assertEqual(
|
||||||
|
['tosca.capabilities.Container'],
|
||||||
|
[c.type for c in compute_type.capabilities])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user