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 = {}
|
||||
for prop in properties:
|
||||
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
|
||||
flavor = self._best_flavor(tosca_props)
|
||||
image = self._best_image(os_cap_props)
|
||||
|
@ -21,15 +21,25 @@ class Capability(object):
|
||||
self._properties = properties
|
||||
self.definition = definition
|
||||
|
||||
@property
|
||||
def properties(self):
|
||||
def get_properties_objects(self):
|
||||
'''Return a list of property objects.'''
|
||||
properties = []
|
||||
props = self._properties
|
||||
if props:
|
||||
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:
|
||||
properties.append(Property(name, value, p.schema))
|
||||
break
|
||||
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.parent_capabilities = self._get_parent_capabilities()
|
||||
|
||||
@property
|
||||
def properties_def(self):
|
||||
def get_properties_def_objects(self):
|
||||
'''Return a list of property definition objects.'''
|
||||
properties = []
|
||||
parent_properties = {}
|
||||
@ -44,6 +43,17 @@ class CapabilityTypeDef(StatefulEntityType):
|
||||
properties.append(PropertyDef(prop, None, schema))
|
||||
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):
|
||||
capabilities = {}
|
||||
parent_cap = self.parent_type
|
||||
|
@ -38,9 +38,9 @@ class DataType(StatefulEntityType):
|
||||
@property
|
||||
def all_properties(self):
|
||||
'''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
|
||||
while ptype:
|
||||
props_def.extend(ptype.properties_def)
|
||||
props_def.extend(ptype.get_properties_def_objects())
|
||||
ptype = ptype.parent_type
|
||||
return props_def
|
||||
|
@ -41,8 +41,7 @@ class StatefulEntityType(EntityType):
|
||||
raise InvalidTypeError(what=entitytype)
|
||||
self.type = entitytype
|
||||
|
||||
@property
|
||||
def properties_def(self):
|
||||
def get_properties_def_objects(self):
|
||||
'''Return a list of property definition objects.'''
|
||||
properties = []
|
||||
props = self.get_value(self.PROPERTIES)
|
||||
@ -51,6 +50,17 @@ class StatefulEntityType(EntityType):
|
||||
properties.append(PropertyDef(prop, None, schema))
|
||||
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
|
||||
def attributes_def(self):
|
||||
'''Return a list of attribute definition objects.'''
|
||||
|
@ -110,7 +110,7 @@ class EntityTemplate(object):
|
||||
props[self.PROPERTIES])
|
||||
|
||||
#validating capability properties values
|
||||
for prop in self.get_capability(cap).properties:
|
||||
for prop in self.get_capability(cap).get_properties_objects():
|
||||
prop.validate()
|
||||
|
||||
#TODO(srinivas_tadepalli): temporary work around to validate
|
||||
@ -131,7 +131,7 @@ class EntityTemplate(object):
|
||||
def _common_validate_properties(self, entitytype, properties):
|
||||
allowed_props = []
|
||||
required_props = []
|
||||
for p in entitytype.properties_def:
|
||||
for p in entitytype.get_properties_def_objects():
|
||||
allowed_props.append(p.name)
|
||||
if p.required:
|
||||
required_props.append(p.name)
|
||||
@ -176,11 +176,11 @@ class EntityTemplate(object):
|
||||
properties = self.type_definition.get_value(self.PROPERTIES,
|
||||
self.entity_tpl) or {}
|
||||
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:
|
||||
prop = Property(name, value, p.schema, self.custom_def)
|
||||
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():
|
||||
prop = Property(p.name, p.default, p.schema, self.custom_def)
|
||||
props.append(prop)
|
||||
|
@ -259,10 +259,9 @@ class GetProperty(Function):
|
||||
for cap in node_template.capabilities:
|
||||
if cap.name == capability_name:
|
||||
property = None
|
||||
for p in cap.properties:
|
||||
if property_name == p.name:
|
||||
property = p.value
|
||||
break
|
||||
props = cap.get_properties()
|
||||
if property_name in props:
|
||||
property = props[property_name]
|
||||
if not property:
|
||||
raise KeyError(_(
|
||||
"Property '{0}' not found in capability '{1}' of node"
|
||||
|
@ -46,33 +46,62 @@ class ToscaDefTest(TestCase):
|
||||
self.assertEqual(
|
||||
['tosca.capabilities.network.Linkable'],
|
||||
[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')
|
||||
self.assertEqual(
|
||||
['initiator', 'network_name', 'port',
|
||||
'port_name', 'ports', 'protocol',
|
||||
'secure', 'url_path'],
|
||||
sorted([p.name for p in endpoint_props]))
|
||||
os_props = self._get_capability_properties_def(
|
||||
sorted(endpoint_props_def.keys()))
|
||||
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')
|
||||
self.assertEqual(
|
||||
['architecture', 'distribution', 'type', 'version'],
|
||||
sorted([p.name for p in os_props]))
|
||||
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):
|
||||
properties_def = None
|
||||
for cap in caps:
|
||||
if cap.type == type:
|
||||
properties_def = cap.properties_def
|
||||
properties_def = cap.get_properties_def()
|
||||
break
|
||||
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):
|
||||
self.assertEqual(
|
||||
['disk_size', 'ip_address', 'mem_size',
|
||||
'num_cpus'],
|
||||
sorted([p.name for p in compute_type.properties_def]))
|
||||
sorted(compute_type.get_properties_def().keys()))
|
||||
|
||||
def test_attributes_def(self):
|
||||
self.assertEqual(
|
||||
|
@ -109,14 +109,22 @@ class ToscaTemplateTest(TestCase):
|
||||
self.assertEqual(property.value, 4096)
|
||||
'''Test capability'''
|
||||
self.assertIn('os', [cap.name for cap in tpl.capabilities])
|
||||
os_properties_objects = None
|
||||
os_properties = None
|
||||
os_type_property = None
|
||||
for capability in tpl.capabilities:
|
||||
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
|
||||
self.assertEqual(
|
||||
['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):
|
||||
self.assertEqual(
|
||||
|
Loading…
x
Reference in New Issue
Block a user