~ubuntu-branches/ubuntu/saucy/heat/saucy-updates

« back to all changes in this revision

Viewing changes to heat/engine/attributes.py

  • Committer: Package Import Robot
  • Author(s): James Page, Chuck Short, James Page
  • Date: 2013-08-08 15:23:59 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130808152359-9jgqjp23kssvc3x9
Tags: 2013.2~b2.a186.g2b4b248-0ubuntu1
[ Chuck Short ]
* debian/patches/rename-quantumclient.patch: Dropped no longer needed. 
* debian/control: Add python-oslo.sphinx

[ James Page ]
* New upstream snapshot.
* d/watch: Updated to track releases on launchpad.
* d/control: Drop BD in pep8, no longer required.
* d/control,rules: Drop use of openstack-pkg-tools, revert use of xz
  compression for debs.
* d/control,*.config,*.templates,po: Drop use of debconf/dbconfig-common
  to configure heat.
* d/*.upstart: Add upstart configurations for Ubuntu.
* d/p/default-kombu.patch: Switch default messaging from qpid to
  kombu.
* d/p/default-sqlite.patch: Use sqlite as default database option.
* d/control: Add python-ceilometerclient to BD's.
* d/rules: Fail package build for unit test failures.
* d/*.install: Directly install configuration files to /etc/heat.
* d/control: Update VCS locations to ubuntu-server-dev branches.
* d/heat-common.{install,manpages}: Include new binaries and associated
  manpages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
class Attribute(object):
20
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
 
21
    An Attribute schema.
28
22
    """
29
23
 
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
 
24
    def __init__(self, attr_name, description):
 
25
        """
 
26
        Initialise with a name and description.
 
27
 
60
28
        :param attr_name: the name of the attribute
61
 
        :description: the description of the attribute
 
29
        :param description: attribute description
 
30
        """
 
31
        self.name = attr_name
 
32
        self.description = description
 
33
 
 
34
    def as_output(self, resource_name):
 
35
        """
 
36
        Return an Output schema entry for a provider template with the given
 
37
        resource name.
 
38
 
 
39
        :param resource_name: the logical name of the provider resource
62
40
        :returns: This attribute as a template 'Output' entry
63
41
        """
64
42
        return {
65
 
            attr_name: {
66
 
                "Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
67
 
                                                           attr_name),
68
 
                "Description": description
69
 
            }
 
43
            "Value": '{"Fn::GetAtt": ["%s", "%s"]}' % (resource_name,
 
44
                                                       self.name),
 
45
            "Description": self.description
70
46
        }
71
47
 
72
 
    def __call__(self):
73
 
        return self.value
74
 
 
75
 
    def __str__(self):
76
 
        return ("Attribute %s: %s" % (self.name, self.value))
77
 
 
78
48
 
79
49
class Attributes(collections.Mapping):
80
50
    """Models a collection of Resource Attributes."""
81
51
 
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())
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())
 
54
        self._resolver = resolver
 
55
        self._attributes = Attributes._make_attributes(schema)
 
56
 
 
57
    @staticmethod
 
58
    def _make_attributes(schema):
 
59
        return dict((n, Attribute(n, d)) for n, d in schema.items())
99
60
 
100
61
    @staticmethod
101
62
    def as_outputs(resource_name, resource_class):
105
66
        :returns: The attributes of the specified resource_class as a template
106
67
                  Output map
107
68
        """
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
 
69
        schema = resource_class.attributes_schema
 
70
        attribs = Attributes._make_attributes(schema).items()
 
71
 
 
72
        return dict((n, att.as_output(resource_name)) for n, att in attribs)
112
73
 
113
74
    @staticmethod
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())
 
76
        if json_snippet:
 
77
            return dict((k, v.get("Description"))
 
78
                        for k, v in json_snippet.items())
 
79
        return {}
117
80
 
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)
123
86
 
124
87
    def __len__(self):
125
88
        return len(self._attributes)