1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
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
60
:param attr_name: the name of the attribute
61
:description: the description of the attribute
62
:returns: This attribute as a template 'Output' entry
66
"Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
68
"Description": description
76
return ("Attribute %s: %s" % (self.name, self.value))
79
class Attributes(collections.Mapping):
80
"""Models a collection of Resource Attributes."""
82
def __init__(self, res_name, schema, resolver):
83
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())
101
def as_outputs(resource_name, resource_class):
103
:param resource_name: logical name of the resource
104
:param resource_class: resource implementation class
105
: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))
114
def schema_from_outputs(json_snippet):
115
return dict(("Outputs.%s" % k, v.get("Description"))
116
for k, v in json_snippet.items())
118
def __getitem__(self, key):
120
raise KeyError('%s: Invalid attribute %s' %
121
(self._resource_name, key))
122
return self._attributes[key]()
125
return len(self._attributes)
127
def __contains__(self, key):
128
return key in self._attributes
131
return iter(self._attributes)
134
return ("Attributes for %s:\n\t" % self._resource_name +
135
'\n\t'.join(self._attributes.values()))