Clean up the attributes module
Get rid of a lot of boilerplate and dead code. Change-Id: I3d7c1b0fc1e3754335d05bfc2e1a8e3e6c88ca8c
This commit is contained in:
parent
0c366c6929
commit
fba9a329a6
|
@ -18,84 +18,45 @@ import collections
|
|||
|
||||
class Attribute(object):
|
||||
"""
|
||||
An attribute description and resolved value.
|
||||
|
||||
:param resource_name: the logical name of the resource having this
|
||||
attribute
|
||||
:param attr_name: the name of the attribute
|
||||
:param description: attribute description
|
||||
:param resolver: a function that will resolve the value of this attribute
|
||||
An Attribute schema.
|
||||
"""
|
||||
|
||||
def __init__(self, attr_name, description, resolver):
|
||||
self._name = attr_name
|
||||
self._description = description
|
||||
self._resolve = resolver
|
||||
def __init__(self, attr_name, description):
|
||||
"""
|
||||
Initialise with a name and description.
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""
|
||||
:returns: The attribute name
|
||||
"""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
"""
|
||||
:returns: The resolved attribute value
|
||||
"""
|
||||
return self._resolve(self._name)
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
"""
|
||||
:returns: A description of the attribute
|
||||
"""
|
||||
return self._description
|
||||
|
||||
@staticmethod
|
||||
def as_output(resource_name, attr_name, description):
|
||||
"""
|
||||
:param resource_name: the logical name of a resource
|
||||
:param attr_name: the name of the attribute
|
||||
:description: the description of the attribute
|
||||
:param description: attribute description
|
||||
"""
|
||||
self.name = attr_name
|
||||
self.description = description
|
||||
|
||||
def as_output(self, resource_name):
|
||||
"""
|
||||
Return an Output schema entry for a provider template with the given
|
||||
resource name.
|
||||
|
||||
:param resource_name: the logical name of the provider resource
|
||||
:returns: This attribute as a template 'Output' entry
|
||||
"""
|
||||
return {
|
||||
attr_name: {
|
||||
"Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
|
||||
attr_name),
|
||||
"Description": description
|
||||
}
|
||||
"Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
|
||||
self.name),
|
||||
"Description": self.description
|
||||
}
|
||||
|
||||
def __call__(self):
|
||||
return self.value
|
||||
|
||||
def __str__(self):
|
||||
return ("Attribute %s: %s" % (self.name, self.value))
|
||||
|
||||
|
||||
class Attributes(collections.Mapping):
|
||||
"""Models a collection of Resource Attributes."""
|
||||
|
||||
def __init__(self, res_name, schema, resolver):
|
||||
self._resource_name = res_name
|
||||
self._attributes = dict((k, Attribute(k, v, resolver))
|
||||
for k, v in schema.items())
|
||||
self._resolver = resolver
|
||||
self._attributes = Attributes._make_attributes(schema)
|
||||
|
||||
@property
|
||||
def attributes(self):
|
||||
"""
|
||||
Get a copy of the attribute definitions in this collection
|
||||
(as opposed to attribute values); useful for doc and
|
||||
template format generation
|
||||
|
||||
:returns: attribute definitions
|
||||
"""
|
||||
# return a deep copy to avoid modification
|
||||
return dict((k, Attribute(k, v.description, v._resolve)) for k, v
|
||||
in self._attributes.items())
|
||||
@staticmethod
|
||||
def _make_attributes(schema):
|
||||
return dict((n, Attribute(n, d)) for n, d in schema.items())
|
||||
|
||||
@staticmethod
|
||||
def as_outputs(resource_name, resource_class):
|
||||
|
@ -105,10 +66,10 @@ class Attributes(collections.Mapping):
|
|||
:returns: The attributes of the specified resource_class as a template
|
||||
Output map
|
||||
"""
|
||||
outputs = {}
|
||||
for name, descr in resource_class.attributes_schema.items():
|
||||
outputs.update(Attribute.as_output(resource_name, name, descr))
|
||||
return outputs
|
||||
schema = resource_class.attributes_schema
|
||||
attribs = Attributes._make_attributes(schema).items()
|
||||
|
||||
return dict((n, att.as_output(resource_name)) for n, att in attribs)
|
||||
|
||||
@staticmethod
|
||||
def schema_from_outputs(json_snippet):
|
||||
|
@ -121,7 +82,7 @@ class Attributes(collections.Mapping):
|
|||
if key not in self:
|
||||
raise KeyError('%s: Invalid attribute %s' %
|
||||
(self._resource_name, key))
|
||||
return self._attributes[key]()
|
||||
return self._resolver(key)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._attributes)
|
||||
|
|
|
@ -12,55 +12,21 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mox
|
||||
|
||||
from heat.engine import attributes
|
||||
from heat.tests import common
|
||||
|
||||
test_attribute_schema = {
|
||||
"attribute1": "A description for attribute 1",
|
||||
"attribute2": "A description for attribute 2",
|
||||
"another attribute": "The last attribute"
|
||||
}
|
||||
|
||||
|
||||
class AttributeTest(common.HeatTestCase):
|
||||
"""Test the Attribute class."""
|
||||
|
||||
def setUp(self):
|
||||
common.HeatTestCase.setUp(self)
|
||||
self.test_resolver = self.m.CreateMockAnything()
|
||||
|
||||
def test_resolve_attribute(self):
|
||||
"""Test that an Attribute returns a good value based on resolver."""
|
||||
test_val = "test value"
|
||||
# resolved with a good value first
|
||||
self.test_resolver('test').AndReturn('test value')
|
||||
# second call resolves to None
|
||||
self.test_resolver(mox.IgnoreArg()).AndReturn(None)
|
||||
self.m.ReplayAll()
|
||||
test_attr = attributes.Attribute("test", "A test attribute",
|
||||
self.test_resolver)
|
||||
self.assertEqual(test_val, test_attr.value,
|
||||
"Unexpected attribute value")
|
||||
self.assertIsNone(test_attr.value,
|
||||
"Second attrib value should be None")
|
||||
self.m.VerifyAll()
|
||||
|
||||
def test_as_output(self):
|
||||
"""Test that Attribute looks right when viewed as an Output."""
|
||||
expected = {
|
||||
"test1": {
|
||||
"Value": '{"Fn::GetAtt": ["test_resource", "test1"]}',
|
||||
"Description": "The first test attribute"
|
||||
}
|
||||
"Value": '{"Fn::GetAtt": ["test_resource", "test1"]}',
|
||||
"Description": "The first test attribute"
|
||||
}
|
||||
self.assertEqual(expected,
|
||||
attributes.Attribute.as_output(
|
||||
"test_resource",
|
||||
"test1",
|
||||
"The first test attribute"),
|
||||
'Attribute as Output mismatch')
|
||||
attr = attributes.Attribute("test1", "The first test attribute")
|
||||
self.assertEqual(expected, attr.as_output("test_resource"))
|
||||
|
||||
|
||||
class AttributesTest(common.HeatTestCase):
|
||||
|
@ -84,6 +50,23 @@ class AttributesTest(common.HeatTestCase):
|
|||
self.attributes_schema,
|
||||
test_resolver)
|
||||
self.assertEqual("value1", attribs['test1'])
|
||||
|
||||
def test_get_attribute_none(self):
|
||||
"""Test that we get the attribute values we expect."""
|
||||
test_resolver = lambda x: None
|
||||
self.m.ReplayAll()
|
||||
attribs = attributes.Attributes('test resource',
|
||||
self.attributes_schema,
|
||||
test_resolver)
|
||||
self.assertEqual(None, attribs['test1'])
|
||||
|
||||
def test_get_attribute_nonexist(self):
|
||||
"""Test that we get the attribute values we expect."""
|
||||
test_resolver = lambda x: "value1"
|
||||
self.m.ReplayAll()
|
||||
attribs = attributes.Attributes('test resource',
|
||||
self.attributes_schema,
|
||||
test_resolver)
|
||||
self.assertRaises(KeyError, attribs.__getitem__, 'not there')
|
||||
|
||||
def test_as_outputs(self):
|
||||
|
|
Loading…
Reference in New Issue