summaryrefslogtreecommitdiff
path: root/heat/engine/function.py
blob: 0dac9f86a53bbfa4f13bc35d5aee2394a0d75d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import abc
import collections
import itertools
import weakref

import six

from heat.common import exception
from heat.common.i18n import _


@six.add_metaclass(abc.ABCMeta)
class Function(object):
    """Abstract base class for template functions."""

    def __init__(self, stack, fn_name, args):
        """Initialise with a Stack, the function name and the arguments.

        All functions take the form of a single-item map in JSON::

            { <fn_name> : <args> }
        """
        super(Function, self).__init__()
        self._stackref = weakref.ref(stack) if stack is not None else None
        self.fn_name = fn_name
        self.args = args

    @property
    def stack(self):
        ref = self._stackref
        if ref is None:
            return None

        stack = ref()
        assert stack is not None, ("Need a reference to the "
                                   "StackDefinition object")
        return stack

    def validate(self):
        """Validate arguments without resolving the function.

        Function subclasses must override this method to validate their
        args.
        """
        validate(self.args)

    @abc.abstractmethod
    def result(self):
        """Return the result of resolving the function.

        Function subclasses must override this method to calculate their
        results.
        """
        return {self.fn_name: self.args}

    def dependencies(self, path):
        return dependencies(self.args, '.'.join([path, self.fn_name]))

    def dep_attrs(self, resource_name):
        """Return the attributes of the specified resource that are referenced.

        Return an iterator over any attributes of the specified resource that
        this function references.

        The special value heat.engine.attributes.ALL_ATTRIBUTES may be used to
        indicate that all attributes of the resource are required.
        """
        return dep_attrs(self.args, resource_name)

    def all_dep_attrs(self):
        """Return resource, attribute name pairs of all attributes referenced.

        Return an iterator over the resource name, attribute name tuples of
        all attributes that this function references.

        The special value heat.engine.attributes.ALL_ATTRIBUTES may be used to
        indicate that all attributes of the resource are required.

        By default this calls the dep_attrs() method, but subclasses can
        override to provide a more efficient implementation.
        """
        # If we are using the default dep_attrs method then it will only
        # return data from the args anyway
        if type(self).dep_attrs == Function.dep_attrs:
            return all_dep_attrs(self.args)

        def res_dep_attrs(resource_name):
            return six.moves.zip(itertools.repeat(resource_name),
                                 self.dep_attrs(resource_name))

        resource_names = self.stack.enabled_rsrc_names()

        return itertools.chain.from_iterable(six.moves.map(res_dep_attrs,
                                                           resource_names))

    def __reduce__(self):
        """Return a representation of the function suitable for pickling.

        This allows the copy module (which works by pickling and then
        unpickling objects) to copy a template. Functions in the copy will
        return to their original (JSON) form (i.e. a single-element map).
        """
        return dict, ([(self.fn_name, self.args)],)

    def _repr_result(self):
        try:
            return repr(self.result())
        except (TypeError, ValueError):
            return '???'

    def __repr__(self):
        """Return a string representation of the function.

        The representation includes the function name, arguments and result
        (if available), as well as the name of the function class.
        """
        fntype = type(self)
        classname = '.'.join(filter(None,
                                    (getattr(fntype,
                                             attr,
                                             '') for attr in ('__module__',
                                                              '__name__'))))
        return '<%s {%s: %r} -> %s>' % (classname,
                                        self.fn_name, self.args,
                                        self._repr_result())

    def __eq__(self, other):
        """Compare the result of this function for equality."""
        try:
            result = self.result()

            if isinstance(other, Function):
                return result == other.result()
            else:
                return result == other

        except (TypeError, ValueError):
            return NotImplemented

    def __ne__(self, other):
        """Compare the result of this function for inequality."""
        eq = self.__eq__(other)
        if eq is NotImplemented:
            return NotImplemented
        return not eq

    __hash__ = None


@six.add_metaclass(abc.ABCMeta)
class Macro(Function):
    """Abstract base class for template macros.

    A macro differs from a function in that it controls how the template is
    parsed. As such, it operates on the syntax tree itself, not on the parsed
    output.
    """
    def __init__(self, stack, fn_name, raw_args, parse_func, template):
        """Initialise with the argument syntax tree and parser function."""
        super(Macro, self).__init__(stack, fn_name, raw_args)
        self._tmplref = weakref.ref(template) if template is not None else None
        self.parsed = self.parse_args(parse_func)

    @property
    def template(self):
        ref = self._tmplref
        if ref is None:
            return None

        tmpl = ref()
        assert tmpl is not None, "Need a reference to the Template object"
        return tmpl

    @abc.abstractmethod
    def parse_args(self, parse_func):
        """Parse the macro using the supplied parsing function.

        Macro subclasses should override this method to control parsing of
        the arguments.
        """
        return parse_func(self.args)

    def validate(self):
        """Validate arguments without resolving the result."""
        validate(self.parsed)

    def result(self):
        """Return the resolved result of the macro contents."""
        return resolve(self.parsed)

    def dependencies(self, path):
        return dependencies(self.parsed, '.'.join([path, self.fn_name]))

    def dep_attrs(self, resource_name):
        """Return the attributes of the specified resource that are referenced.

        Return an iterator over any attributes of the specified resource that
        this function references.

        The special value heat.engine.attributes.ALL_ATTRIBUTES may be used to
        indicate that all attributes of the resource are required.
        """
        return dep_attrs(self.parsed, resource_name)

    def all_dep_attrs(self):
        """Return resource, attribute name pairs of all attributes referenced.

        Return an iterator over the resource name, attribute name tuples of
        all attributes that this function references.

        The special value heat.engine.attributes.ALL_ATTRIBUTES may be used to
        indicate that all attributes of the resource are required.

        By default this calls the dep_attrs() method, but subclasses can
        override to provide a more efficient implementation.
        """
        # If we are using the default dep_attrs method then it will only
        # return data from the transformed parsed args anyway
        if type(self).dep_attrs == Macro.dep_attrs:
            return all_dep_attrs(self.parsed)

        return super(Macro, self).all_dep_attrs()

    def __reduce__(self):
        """Return a representation of the macro result suitable for pickling.

        This allows the copy module (which works by pickling and then
        unpickling objects) to copy a template. Functions in the copy will
        return to their original (JSON) form (i.e. a single-element map).

        Unlike other functions, macros are *not* preserved during a copy. The
        the processed (but unparsed) output is returned in their place.
        """
        if isinstance(self.parsed, Function):
            return self.parsed.__reduce__()
        if self.parsed is None:
            return lambda x: None, (None,)
        return type(self.parsed), (self.parsed,)

    def _repr_result(self):
        return repr(self.parsed)


def resolve(snippet):
    if isinstance(snippet, Function):
        return snippet.result()

    if isinstance(snippet, collections.Mapping):
        return dict((k, resolve(v)) for k, v in snippet.items())
    elif (not isinstance(snippet, six.string_types) and
          isinstance(snippet, collections.Iterable)):
        return [resolve(v) for v in snippet]

    return snippet


def validate(snippet, path=''):
    if isinstance(snippet, Function):
        try:
            snippet.validate()
        except AssertionError:
            raise
        except Exception as e:
            path = '.'.join([path, snippet.fn_name])
            raise exception.StackValidationFailed(
                path=path, message=six.text_type(e))
    elif isinstance(snippet, collections.Mapping):
        def mkpath(key):
            return '.'.join([path, key])

        for k, v in six.iteritems(snippet):
            validate(v, mkpath(k))
    elif (not isinstance(snippet, six.string_types) and
          isinstance(snippet, collections.Iterable)):
        def mkpath(indx):
            return '.'.join([path, '[%d]' % indx])

        for i, v in enumerate(snippet):
            validate(v, mkpath(i))


def dependencies(snippet, path=''):
    """Return an iterator over Resource dependencies in a template snippet.

    The snippet should be already parsed to insert Function objects where
    appropriate.
    """

    if isinstance(snippet, Function):
        return snippet.dependencies(path)

    elif isinstance(snippet, collections.Mapping):
        def mkpath(key):
            return '.'.join([path, six.text_type(key)])

        deps = (dependencies(value,
                             mkpath(key)) for key, value in snippet.items())
        return itertools.chain.from_iterable(deps)

    elif (not isinstance(snippet, six.string_types) and
          isinstance(snippet, collections.Iterable)):
        def mkpath(idx):
            return ''.join([path, '[%d]' % idx])

        deps = (dependencies(value,
                             mkpath(i)) for i, value in enumerate(snippet))
        return itertools.chain.from_iterable(deps)

    else:
        return []


def dep_attrs(snippet, resource_name):
    """Iterator over dependent attrs of a resource in a template snippet.

    The snippet should be already parsed to insert Function objects where
    appropriate.

    :returns: an iterator over the attributes of the specified resource that
    are referenced in the template snippet.
    """

    if isinstance(snippet, Function):
        return snippet.dep_attrs(resource_name)

    elif isinstance(snippet, collections.Mapping):
        attrs = (dep_attrs(val, resource_name) for val in snippet.values())
        return itertools.chain.from_iterable(attrs)
    elif (not isinstance(snippet, six.string_types) and
          isinstance(snippet, collections.Iterable)):
        attrs = (dep_attrs(value, resource_name) for value in snippet)
        return itertools.chain.from_iterable(attrs)
    return []


def all_dep_attrs(snippet):
    """Iterator over resource, attribute name pairs referenced in a snippet.

    The snippet should be already parsed to insert Function objects where
    appropriate.

    :returns: an iterator over the resource name, attribute name tuples of all
    attributes that are referenced in the template snippet.
    """

    if isinstance(snippet, Function):
        return snippet.all_dep_attrs()

    elif isinstance(snippet, collections.Mapping):
        res_attrs = (all_dep_attrs(value) for value in snippet.values())
        return itertools.chain.from_iterable(res_attrs)
    elif (not isinstance(snippet, six.string_types) and
          isinstance(snippet, collections.Iterable)):
        res_attrs = (all_dep_attrs(value) for value in snippet)
        return itertools.chain.from_iterable(res_attrs)
    return []


class Invalid(Function):
    """A function for checking condition functions and to force failures.

    This function is used to force failures for functions that are not
    supported in condition definition.
    """

    def __init__(self, stack, fn_name, args):
        raise ValueError(_('The function "%s" '
                           'is invalid in this context') % fn_name)

    def result(self):
        return super(Invalid, self).result()