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
-
- @property
- def name(self):
+ def __init__(self, attr_name, description):
"""
- :returns: The attribute name
- """
- return self._name
+ Initialise with a name and description.
- @property
- 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)
+ self.name = attr_name
+ self.description = description
- @property
- 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.
- @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 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())
-
- @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
+ 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())
+ @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):
: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):
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)
# 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):
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):