~ubuntu-branches/ubuntu/trusty/heat/trusty-security

« back to all changes in this revision

Viewing changes to heat/engine/attributes.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Yolanda Robla, Chuck Short
  • Date: 2013-07-22 16:22:29 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130722162229-zzvfu40id94ii0hc
Tags: 2013.2~b2-0ubuntu1
[ Yolanda Robla ]
* debian/tests: added autopkg tests

[ Chuck Short ]
* New upstream release
* debian/control:
  - Add python-pbr to build-depends.
  - Add python-d2to to build-depends.
  - Dropped python-argparse.
  - Add python-six to build-depends.
  - Dropped python-sendfile.
  - Dropped python-nose.
  - Added testrepository.
  - Added python-testtools.
* debian/rules: Run testrepository instead of nosetets.
* debian/patches/removes-lxml-version-limitation-from-pip-requires.patch: Dropped
  no longer needed.
* debian/patches/fix-package-version-detection-when-building-doc.patch: Dropped
  no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
#
 
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
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
#    under the License.
 
15
 
 
16
import collections
 
17
 
 
18
 
 
19
class Attribute(object):
 
20
    """
 
21
    An attribute description and resolved value.
 
22
 
 
23
    :param resource_name: the logical name of the resource having this
 
24
                          attribute
 
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
 
28
    """
 
29
 
 
30
    def __init__(self, attr_name, description, resolver):
 
31
        self._name = attr_name
 
32
        self._description = description
 
33
        self._resolve = resolver
 
34
 
 
35
    @property
 
36
    def name(self):
 
37
        """
 
38
        :returns: The attribute name
 
39
        """
 
40
        return self._name
 
41
 
 
42
    @property
 
43
    def value(self):
 
44
        """
 
45
        :returns: The resolved attribute value
 
46
        """
 
47
        return self._resolve(self._name)
 
48
 
 
49
    @property
 
50
    def description(self):
 
51
        """
 
52
        :returns: A description of the attribute
 
53
        """
 
54
        return self._description
 
55
 
 
56
    @staticmethod
 
57
    def as_output(resource_name, attr_name, description):
 
58
        """
 
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
 
63
        """
 
64
        return {
 
65
            attr_name: {
 
66
                "Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
 
67
                                                           attr_name),
 
68
                "Description": description
 
69
            }
 
70
        }
 
71
 
 
72
    def __call__(self):
 
73
        return self.value
 
74
 
 
75
    def __str__(self):
 
76
        return ("Attribute %s: %s" % (self.name, self.value))
 
77
 
 
78
 
 
79
class Attributes(collections.Mapping):
 
80
    """Models a collection of Resource Attributes."""
 
81
 
 
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())
 
86
 
 
87
    @property
 
88
    def attributes(self):
 
89
        """
 
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
 
93
 
 
94
        :returns: attribute definitions
 
95
        """
 
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())
 
99
 
 
100
    @staticmethod
 
101
    def as_outputs(resource_name, resource_class):
 
102
        """
 
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
 
106
                  Output map
 
107
        """
 
108
        outputs = {}
 
109
        for name, descr in resource_class.attributes_schema.items():
 
110
            outputs.update(Attribute.as_output(resource_name, name, descr))
 
111
        return outputs
 
112
 
 
113
    @staticmethod
 
114
    def schema_from_outputs(json_snippet):
 
115
        return dict(("Outputs.%s" % k, v.get("Description"))
 
116
                    for k, v in json_snippet.items())
 
117
 
 
118
    def __getitem__(self, key):
 
119
        if key not in self:
 
120
            raise KeyError('%s: Invalid attribute %s' %
 
121
                           (self._resource_name, key))
 
122
        return self._attributes[key]()
 
123
 
 
124
    def __len__(self):
 
125
        return len(self._attributes)
 
126
 
 
127
    def __contains__(self, key):
 
128
        return key in self._attributes
 
129
 
 
130
    def __iter__(self):
 
131
        return iter(self._attributes)
 
132
 
 
133
    def __repr__(self):
 
134
        return ("Attributes for %s:\n\t" % self._resource_name +
 
135
                '\n\t'.join(self._attributes.values()))