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:
Sahdev Zala 2014-07-28 16:31:06 -05:00
parent 51e5797ceb
commit f2ba5fa317
8 changed files with 67 additions and 2 deletions

View File

@ -22,6 +22,10 @@ class CapabilityTypeDef(EntityType):
def __init__(self, name, ctype, ntype, properties):
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.nodetype = ntype
self.properties = properties

View File

@ -37,6 +37,11 @@ class EntityType(object):
'tosca.relationships.HostedOn',
'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):
'''Return a type this type is derived from.'''
return self.entity_value(defs, 'derived_from')

View File

@ -31,6 +31,8 @@ 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

View File

@ -33,6 +33,8 @@ class NodeType(StatefulEntityType):
def __init__(self, ntype, custom_def=None):
super(NodeType, self).__init__()
if self.NODE_PREFIX not in ntype:
ntype = self.NODE_PREFIX + ntype
if ntype in list(self.TOSCA_DEF.keys()):
self.defs = self.TOSCA_DEF[ntype]
elif custom_def and ntype in list(custom_def.keys()):

View File

@ -22,6 +22,8 @@ class RelationshipType(StatefulEntityType):
def __init__(self, type, capability_name):
super(RelationshipType, self).__init__()
self.defs = self.TOSCA_DEF[type]
if self.RELATIONSHIP_PREFIX not in type:
type = self.RELATIONSHIP_PREFIX + type
self.type = type
self.capability_name = capability_name

View File

@ -41,10 +41,13 @@ class NodeTemplate(object):
self.node_templates = node_templates
self._validate_field()
self.node_template = node_templates[self.name]
self.type = self.node_template[TYPE]
self.node_type = NodeType(self.type, custom_def)
self.node_type = NodeType(self.node_template[TYPE], custom_def)
self.related = {}
@property
def type(self):
return self.node_type.type
@property
def requirements(self):
return self.node_type.get_value(REQUIREMENTS, self.node_template)

View File

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

View File

@ -14,6 +14,7 @@
# under the License.
import os
from translator.toscalib.elements.nodetype import NodeType
from translator.toscalib.functions import GetRefProperty
from translator.toscalib.tests.base import TestCase
from translator.toscalib.tosca_template import ToscaTemplate
@ -138,3 +139,19 @@ class ToscaTemplateTest(TestCase):
else:
raise AssertionError(
'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])