Clean up the attributes module

Get rid of a lot of boilerplate and dead code.

Change-Id: I3d7c1b0fc1e3754335d05bfc2e1a8e3e6c88ca8c
Zane Bitter 9 years ago
parent 0c366c6929
commit fba9a329a6
  1. 87
  2. 59

@ -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
: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 name(self):
def __init__(self, attr_name, description):
:returns: The attribute name
return self._name
Initialise with a name and description.
def value(self):
:returns: The resolved attribute value
:param attr_name: the name of the attribute
:param description: attribute description
return self._resolve(self._name) = attr_name
self.description = description
def description(self):
:returns: A description of the attribute
def as_output(self, resource_name):
return self._description
Return an Output schema entry for a provider template with the given
resource name.
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 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,
"Description": description
"Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,,
"Description": self.description
def __call__(self):
return self.value
def __str__(self):
return ("Attribute %s: %s" % (, 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())
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
self._resolver = resolver
self._attributes = Attributes._make_attributes(schema)
: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())
def _make_attributes(schema):
return dict((n, Attribute(n, d)) for n, d in schema.items())
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)
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):
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
test_attr = attributes.Attribute("test", "A test attribute",
self.assertEqual(test_val, test_attr.value,
"Unexpected attribute value")
"Second attrib value should be None")
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"
"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.assertEqual("value1", attribs['test1'])
def test_get_attribute_none(self):
"""Test that we get the attribute values we expect."""
test_resolver = lambda x: None
attribs = attributes.Attributes('test resource',
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"
attribs = attributes.Attributes('test resource',
self.assertRaises(KeyError, attribs.__getitem__, 'not there')
def test_as_outputs(self):