19
19
class Attribute(object):
21
An attribute description and resolved value.
23
:param resource_name: the logical name of the resource having this
25
:param attr_name: the name of the attribute
26
:param description: attribute description
27
:param resolver: a function that will resolve the value of this attribute
30
def __init__(self, attr_name, description, resolver):
31
self._name = attr_name
32
self._description = description
33
self._resolve = resolver
38
:returns: The attribute name
45
:returns: The resolved attribute value
47
return self._resolve(self._name)
50
def description(self):
52
:returns: A description of the attribute
54
return self._description
57
def as_output(resource_name, attr_name, description):
59
:param resource_name: the logical name of a resource
24
def __init__(self, attr_name, description):
26
Initialise with a name and description.
60
28
:param attr_name: the name of the attribute
61
:description: the description of the attribute
29
:param description: attribute description
32
self.description = description
34
def as_output(self, resource_name):
36
Return an Output schema entry for a provider template with the given
39
:param resource_name: the logical name of the provider resource
62
40
:returns: This attribute as a template 'Output' entry
66
"Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
68
"Description": description
43
"Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
45
"Description": self.description
76
return ("Attribute %s: %s" % (self.name, self.value))
79
49
class Attributes(collections.Mapping):
80
50
"""Models a collection of Resource Attributes."""
82
52
def __init__(self, res_name, schema, resolver):
83
53
self._resource_name = res_name
84
self._attributes = dict((k, Attribute(k, v, resolver))
85
for k, v in schema.items())
90
Get a copy of the attribute definitions in this collection
91
(as opposed to attribute values); useful for doc and
92
template format generation
94
:returns: attribute definitions
96
# return a deep copy to avoid modification
97
return dict((k, Attribute(k, v.description, v._resolve)) for k, v
98
in self._attributes.items())
54
self._resolver = resolver
55
self._attributes = Attributes._make_attributes(schema)
58
def _make_attributes(schema):
59
return dict((n, Attribute(n, d)) for n, d in schema.items())
101
62
def as_outputs(resource_name, resource_class):
105
66
:returns: The attributes of the specified resource_class as a template
109
for name, descr in resource_class.attributes_schema.items():
110
outputs.update(Attribute.as_output(resource_name, name, descr))
69
schema = resource_class.attributes_schema
70
attribs = Attributes._make_attributes(schema).items()
72
return dict((n, att.as_output(resource_name)) for n, att in attribs)
114
75
def schema_from_outputs(json_snippet):
115
return dict(("Outputs.%s" % k, v.get("Description"))
116
for k, v in json_snippet.items())
77
return dict((k, v.get("Description"))
78
for k, v in json_snippet.items())
118
81
def __getitem__(self, key):
119
82
if key not in self:
120
83
raise KeyError('%s: Invalid attribute %s' %
121
84
(self._resource_name, key))
122
return self._attributes[key]()
85
return self._resolver(key)
124
87
def __len__(self):
125
88
return len(self._attributes)