Add support for properties_def as a dictionary
Replace the properties_def function in capabilitytype.py and statefulentitytype.py with a get_properties_def_objects() function and add a get_properties_def() function that returns a name-value dictionary, and a get_property_def(name) function that returns the value associated with the property name. Also, replace calls to properties_def with calls to get_properties_def_objects() and make use of get_properties_def() dictionary when applicable. Also, replace the properties function in capabilities.py with a get_properties_objects() function and add a get_properties() function that returns a name-value dictionary, and a get_property(name) function that reurns the value of a property name. Also, replace calls to properties with calls to get_properties_objects() and make use of get_properties() dictionary when applicable. Change-Id: I9a2c1352868e6d2d50e1257e75a889c8675d0c89 Closes-Bug: #1316275
This commit is contained in:
parent
8761ac5631
commit
01f85e17b8
@ -76,7 +76,7 @@ class ToscaCompute(HotResource):
|
|||||||
os_cap_props = {}
|
os_cap_props = {}
|
||||||
for prop in properties:
|
for prop in properties:
|
||||||
tosca_props[prop.name] = prop.value
|
tosca_props[prop.name] = prop.value
|
||||||
for prop in os_capability.properties:
|
for prop in os_capability.get_properties_objects():
|
||||||
os_cap_props[prop.name] = prop.value
|
os_cap_props[prop.name] = prop.value
|
||||||
flavor = self._best_flavor(tosca_props)
|
flavor = self._best_flavor(tosca_props)
|
||||||
image = self._best_image(os_cap_props)
|
image = self._best_image(os_cap_props)
|
||||||
|
@ -21,15 +21,25 @@ class Capability(object):
|
|||||||
self._properties = properties
|
self._properties = properties
|
||||||
self.definition = definition
|
self.definition = definition
|
||||||
|
|
||||||
@property
|
def get_properties_objects(self):
|
||||||
def properties(self):
|
|
||||||
'''Return a list of property objects.'''
|
'''Return a list of property objects.'''
|
||||||
properties = []
|
properties = []
|
||||||
props = self._properties
|
props = self._properties
|
||||||
if props:
|
if props:
|
||||||
for name, value in props.items():
|
for name, value in props.items():
|
||||||
for p in self.definition.properties_def:
|
for p in self.definition.get_properties_def_objects():
|
||||||
if p.name == name:
|
if p.name == name:
|
||||||
properties.append(Property(name, value, p.schema))
|
properties.append(Property(name, value, p.schema))
|
||||||
break
|
break
|
||||||
return properties
|
return properties
|
||||||
|
|
||||||
|
def get_properties(self):
|
||||||
|
'''Return a dictionary of property name-value pairs.'''
|
||||||
|
return {prop.name: prop.value
|
||||||
|
for prop in self.get_properties_objects()}
|
||||||
|
|
||||||
|
def get_property(self, name):
|
||||||
|
'''Return the value of a given property name.'''
|
||||||
|
props = self.get_properties()
|
||||||
|
if name in props:
|
||||||
|
return props[name]
|
||||||
|
@ -27,8 +27,7 @@ class CapabilityTypeDef(StatefulEntityType):
|
|||||||
self.properties = self.defs[self.PROPERTIES]
|
self.properties = self.defs[self.PROPERTIES]
|
||||||
self.parent_capabilities = self._get_parent_capabilities()
|
self.parent_capabilities = self._get_parent_capabilities()
|
||||||
|
|
||||||
@property
|
def get_properties_def_objects(self):
|
||||||
def properties_def(self):
|
|
||||||
'''Return a list of property definition objects.'''
|
'''Return a list of property definition objects.'''
|
||||||
properties = []
|
properties = []
|
||||||
parent_properties = {}
|
parent_properties = {}
|
||||||
@ -44,6 +43,17 @@ class CapabilityTypeDef(StatefulEntityType):
|
|||||||
properties.append(PropertyDef(prop, None, schema))
|
properties.append(PropertyDef(prop, None, schema))
|
||||||
return properties
|
return properties
|
||||||
|
|
||||||
|
def get_properties_def(self):
|
||||||
|
'''Return a dictionary of property definition objects.'''
|
||||||
|
return {prop.name: prop.value
|
||||||
|
for prop in self.get_properties_def_objects()}
|
||||||
|
|
||||||
|
def get_property_def(self, name):
|
||||||
|
'''Return the definition of a given property.'''
|
||||||
|
props_def = self.get_properties_def()
|
||||||
|
if name in props_def:
|
||||||
|
return props_def[name]
|
||||||
|
|
||||||
def _get_parent_capabilities(self):
|
def _get_parent_capabilities(self):
|
||||||
capabilities = {}
|
capabilities = {}
|
||||||
parent_cap = self.parent_type
|
parent_cap = self.parent_type
|
||||||
|
@ -38,9 +38,9 @@ class DataType(StatefulEntityType):
|
|||||||
@property
|
@property
|
||||||
def all_properties(self):
|
def all_properties(self):
|
||||||
'''Return all properties defined in this type and its parent type.'''
|
'''Return all properties defined in this type and its parent type.'''
|
||||||
props_def = self.properties_def
|
props_def = self.get_properties_def_objects()
|
||||||
ptype = self.parent_type
|
ptype = self.parent_type
|
||||||
while ptype:
|
while ptype:
|
||||||
props_def.extend(ptype.properties_def)
|
props_def.extend(ptype.get_properties_def_objects())
|
||||||
ptype = ptype.parent_type
|
ptype = ptype.parent_type
|
||||||
return props_def
|
return props_def
|
||||||
|
@ -41,8 +41,7 @@ class StatefulEntityType(EntityType):
|
|||||||
raise InvalidTypeError(what=entitytype)
|
raise InvalidTypeError(what=entitytype)
|
||||||
self.type = entitytype
|
self.type = entitytype
|
||||||
|
|
||||||
@property
|
def get_properties_def_objects(self):
|
||||||
def properties_def(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_value(self.PROPERTIES)
|
||||||
@ -51,6 +50,17 @@ class StatefulEntityType(EntityType):
|
|||||||
properties.append(PropertyDef(prop, None, schema))
|
properties.append(PropertyDef(prop, None, schema))
|
||||||
return properties
|
return properties
|
||||||
|
|
||||||
|
def get_properties_def(self):
|
||||||
|
'''Return a dictionary of property definition objects.'''
|
||||||
|
return {prop.name: prop.value
|
||||||
|
for prop in self.get_properties_def_objects()}
|
||||||
|
|
||||||
|
def get_property_def(self, name):
|
||||||
|
'''Return the property definition associated with a given name.'''
|
||||||
|
props_def = self.get_properties_def()
|
||||||
|
if name in props_def:
|
||||||
|
return props_def[name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def attributes_def(self):
|
def attributes_def(self):
|
||||||
'''Return a list of attribute definition objects.'''
|
'''Return a list of attribute definition objects.'''
|
||||||
|
@ -110,7 +110,7 @@ class EntityTemplate(object):
|
|||||||
props[self.PROPERTIES])
|
props[self.PROPERTIES])
|
||||||
|
|
||||||
#validating capability properties values
|
#validating capability properties values
|
||||||
for prop in self.get_capability(cap).properties:
|
for prop in self.get_capability(cap).get_properties_objects():
|
||||||
prop.validate()
|
prop.validate()
|
||||||
|
|
||||||
#TODO(srinivas_tadepalli): temporary work around to validate
|
#TODO(srinivas_tadepalli): temporary work around to validate
|
||||||
@ -131,7 +131,7 @@ class EntityTemplate(object):
|
|||||||
def _common_validate_properties(self, entitytype, properties):
|
def _common_validate_properties(self, entitytype, properties):
|
||||||
allowed_props = []
|
allowed_props = []
|
||||||
required_props = []
|
required_props = []
|
||||||
for p in entitytype.properties_def:
|
for p in entitytype.get_properties_def_objects():
|
||||||
allowed_props.append(p.name)
|
allowed_props.append(p.name)
|
||||||
if p.required:
|
if p.required:
|
||||||
required_props.append(p.name)
|
required_props.append(p.name)
|
||||||
@ -176,11 +176,11 @@ class EntityTemplate(object):
|
|||||||
properties = self.type_definition.get_value(self.PROPERTIES,
|
properties = self.type_definition.get_value(self.PROPERTIES,
|
||||||
self.entity_tpl) or {}
|
self.entity_tpl) or {}
|
||||||
for name, value in properties.items():
|
for name, value in properties.items():
|
||||||
for p in self.type_definition.properties_def:
|
for p in self.type_definition.get_properties_def_objects():
|
||||||
if p.name == name:
|
if p.name == name:
|
||||||
prop = Property(name, value, p.schema, self.custom_def)
|
prop = Property(name, value, p.schema, self.custom_def)
|
||||||
props.append(prop)
|
props.append(prop)
|
||||||
for p in self.type_definition.properties_def:
|
for p in self.type_definition.get_properties_def_objects():
|
||||||
if p.default is not None and p.name not in properties.keys():
|
if p.default is not None and p.name not in properties.keys():
|
||||||
prop = Property(p.name, p.default, p.schema, self.custom_def)
|
prop = Property(p.name, p.default, p.schema, self.custom_def)
|
||||||
props.append(prop)
|
props.append(prop)
|
||||||
|
@ -259,10 +259,9 @@ class GetProperty(Function):
|
|||||||
for cap in node_template.capabilities:
|
for cap in node_template.capabilities:
|
||||||
if cap.name == capability_name:
|
if cap.name == capability_name:
|
||||||
property = None
|
property = None
|
||||||
for p in cap.properties:
|
props = cap.get_properties()
|
||||||
if property_name == p.name:
|
if property_name in props:
|
||||||
property = p.value
|
property = props[property_name]
|
||||||
break
|
|
||||||
if not property:
|
if not property:
|
||||||
raise KeyError(_(
|
raise KeyError(_(
|
||||||
"Property '{0}' not found in capability '{1}' of node"
|
"Property '{0}' not found in capability '{1}' of node"
|
||||||
|
@ -46,33 +46,62 @@ class ToscaDefTest(TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['tosca.capabilities.network.Linkable'],
|
['tosca.capabilities.network.Linkable'],
|
||||||
[c.type for c in network_type.capabilities])
|
[c.type for c in network_type.capabilities])
|
||||||
endpoint_props = self._get_capability_properties_def(
|
endpoint_props_def_objects = \
|
||||||
|
self._get_capability_properties_def_objects(
|
||||||
|
webserver_type.capabilities,
|
||||||
|
'tosca.capabilities.Endpoint')
|
||||||
|
self.assertEqual(
|
||||||
|
['initiator', 'network_name', 'port',
|
||||||
|
'port_name', 'ports', 'protocol',
|
||||||
|
'secure', 'url_path'],
|
||||||
|
sorted([p.name for p in endpoint_props_def_objects]))
|
||||||
|
endpoint_props_def = self._get_capability_properties_def(
|
||||||
webserver_type.capabilities, 'tosca.capabilities.Endpoint')
|
webserver_type.capabilities, 'tosca.capabilities.Endpoint')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['initiator', 'network_name', 'port',
|
['initiator', 'network_name', 'port',
|
||||||
'port_name', 'ports', 'protocol',
|
'port_name', 'ports', 'protocol',
|
||||||
'secure', 'url_path'],
|
'secure', 'url_path'],
|
||||||
sorted([p.name for p in endpoint_props]))
|
sorted(endpoint_props_def.keys()))
|
||||||
os_props = self._get_capability_properties_def(
|
endpoint_prop_def = self._get_capability_property_def(
|
||||||
|
webserver_type.capabilities, 'tosca.capabilities.Endpoint',
|
||||||
|
'initiator')
|
||||||
|
self.assertEqual(None, endpoint_prop_def)
|
||||||
|
os_props = self._get_capability_properties_def_objects(
|
||||||
compute_type.capabilities, 'tosca.capabilities.OperatingSystem')
|
compute_type.capabilities, 'tosca.capabilities.OperatingSystem')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['architecture', 'distribution', 'type', 'version'],
|
['architecture', 'distribution', 'type', 'version'],
|
||||||
sorted([p.name for p in os_props]))
|
sorted([p.name for p in os_props]))
|
||||||
self.assertTrue([p.required for p in os_props if p.name == 'type'])
|
self.assertTrue([p.required for p in os_props if p.name == 'type'])
|
||||||
|
|
||||||
|
def _get_capability_properties_def_objects(self, caps, type):
|
||||||
|
properties_def = None
|
||||||
|
for cap in caps:
|
||||||
|
if cap.type == type:
|
||||||
|
properties_def = cap.get_properties_def_objects()
|
||||||
|
break
|
||||||
|
return properties_def
|
||||||
|
|
||||||
def _get_capability_properties_def(self, caps, type):
|
def _get_capability_properties_def(self, caps, type):
|
||||||
properties_def = None
|
properties_def = None
|
||||||
for cap in caps:
|
for cap in caps:
|
||||||
if cap.type == type:
|
if cap.type == type:
|
||||||
properties_def = cap.properties_def
|
properties_def = cap.get_properties_def()
|
||||||
break
|
break
|
||||||
return properties_def
|
return properties_def
|
||||||
|
|
||||||
|
def _get_capability_property_def(self, caps, type, property):
|
||||||
|
property_def = None
|
||||||
|
for cap in caps:
|
||||||
|
if cap.type == type:
|
||||||
|
property_def = cap.get_property_def(property)
|
||||||
|
break
|
||||||
|
return property_def
|
||||||
|
|
||||||
def test_properties_def(self):
|
def test_properties_def(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['disk_size', 'ip_address', 'mem_size',
|
['disk_size', 'ip_address', 'mem_size',
|
||||||
'num_cpus'],
|
'num_cpus'],
|
||||||
sorted([p.name for p in compute_type.properties_def]))
|
sorted(compute_type.get_properties_def().keys()))
|
||||||
|
|
||||||
def test_attributes_def(self):
|
def test_attributes_def(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
@ -109,14 +109,22 @@ class ToscaTemplateTest(TestCase):
|
|||||||
self.assertEqual(property.value, 4096)
|
self.assertEqual(property.value, 4096)
|
||||||
'''Test capability'''
|
'''Test capability'''
|
||||||
self.assertIn('os', [cap.name for cap in tpl.capabilities])
|
self.assertIn('os', [cap.name for cap in tpl.capabilities])
|
||||||
|
os_properties_objects = None
|
||||||
os_properties = None
|
os_properties = None
|
||||||
|
os_type_property = None
|
||||||
for capability in tpl.capabilities:
|
for capability in tpl.capabilities:
|
||||||
if capability.name == 'os':
|
if capability.name == 'os':
|
||||||
os_properties = capability.properties
|
os_properties_objects = \
|
||||||
|
capability.get_properties_objects()
|
||||||
|
os_properties = capability.get_properties()
|
||||||
|
os_type_property = capability.get_property('type')
|
||||||
break
|
break
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
['Linux'],
|
['Linux'],
|
||||||
[p.value for p in os_properties if p.name == 'type'])
|
[p.value for p in os_properties_objects
|
||||||
|
if p.name == 'type'])
|
||||||
|
self.assertEqual('Linux', os_properties['type'])
|
||||||
|
self.assertEqual('Linux', os_type_property)
|
||||||
|
|
||||||
def test_outputs(self):
|
def test_outputs(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user