Fix property inheritance for node templates
Fix issues with property inheritance that cause a derived node not inherit all its parent(s) property definitions. Add proper unit tests as well. Change-Id: Ia27af7e3fef742ccf71beecfc47e56483e9ebc37 Closes-Bug: #1483896
This commit is contained in:
@@ -93,3 +93,20 @@ class EntityType(object):
|
|||||||
value = p.get_value(ndtype)
|
value = p.get_value(ndtype)
|
||||||
p = p.parent_type
|
p = p.parent_type
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def get_definition(self, ndtype):
|
||||||
|
value = None
|
||||||
|
defs = self.defs
|
||||||
|
if ndtype in defs:
|
||||||
|
value = defs[ndtype]
|
||||||
|
p = self.parent_type
|
||||||
|
if p:
|
||||||
|
inherited = p.get_definition(ndtype)
|
||||||
|
if inherited:
|
||||||
|
inherited = dict(inherited)
|
||||||
|
if not value:
|
||||||
|
value = inherited
|
||||||
|
else:
|
||||||
|
inherited.update(value)
|
||||||
|
value.update(inherited)
|
||||||
|
return value
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class StatefulEntityType(EntityType):
|
|||||||
def get_properties_def_objects(self):
|
def get_properties_def_objects(self):
|
||||||
'''Return a list of property definition objects.'''
|
'''Return a list of property definition objects.'''
|
||||||
properties = []
|
properties = []
|
||||||
props = self.get_value(self.PROPERTIES)
|
props = self.get_definition(self.PROPERTIES)
|
||||||
if props:
|
if props:
|
||||||
for prop, schema in props.items():
|
for prop, schema in props.items():
|
||||||
properties.append(PropertyDef(prop, None, schema))
|
properties.append(PropertyDef(prop, None, schema))
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ resources:
|
|||||||
get_resource: my_server
|
get_resource: my_server
|
||||||
volume_id:
|
volume_id:
|
||||||
get_resource: my_storage
|
get_resource: my_storage
|
||||||
|
mountpoint:
|
||||||
|
get_param: storage_location
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
private_ip:
|
private_ip:
|
||||||
|
|||||||
@@ -188,3 +188,36 @@ class PropertyTest(TestCase):
|
|||||||
propertyInstance = Property('test_property', 'Foo',
|
propertyInstance = Property('test_property', 'Foo',
|
||||||
test_property_schema)
|
test_property_schema)
|
||||||
self.assertEqual(True, propertyInstance.required)
|
self.assertEqual(True, propertyInstance.required)
|
||||||
|
|
||||||
|
def test_proprety_inheritance(self):
|
||||||
|
from toscaparser.nodetemplate import NodeTemplate
|
||||||
|
|
||||||
|
tosca_custom_def = '''
|
||||||
|
tosca.nodes.SoftwareComponent.MySoftware:
|
||||||
|
derived_from: SoftwareComponent
|
||||||
|
properties:
|
||||||
|
install_path:
|
||||||
|
required: false
|
||||||
|
type: string
|
||||||
|
default: /opt/mysoftware
|
||||||
|
'''
|
||||||
|
|
||||||
|
tosca_node_template = '''
|
||||||
|
node_templates:
|
||||||
|
mysoftware_instance:
|
||||||
|
type: tosca.nodes.SoftwareComponent.MySoftware
|
||||||
|
properties:
|
||||||
|
component_version: 3.1
|
||||||
|
'''
|
||||||
|
|
||||||
|
expected_properties = ['component_version',
|
||||||
|
'install_path']
|
||||||
|
|
||||||
|
nodetemplates = yamlparser.\
|
||||||
|
simple_parse(tosca_node_template)['node_templates']
|
||||||
|
custom_def = yamlparser.simple_parse(tosca_custom_def)
|
||||||
|
name = list(nodetemplates.keys())[0]
|
||||||
|
tpl = NodeTemplate(name, nodetemplates, custom_def)
|
||||||
|
self.assertIsNone(tpl.validate())
|
||||||
|
self.assertEqual(expected_properties,
|
||||||
|
sorted(tpl.get_properties().keys()))
|
||||||
|
|||||||
Reference in New Issue
Block a user