summaryrefslogtreecommitdiff
path: root/heatclient/common/hook_utils.py
blob: 3e91ae3b600f8990247054a847b5ab944233ae02 (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
#    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 fnmatch
import logging

from heatclient._i18n import _
from heatclient import exc

logger = logging.getLogger(__name__)


def clear_hook(hc, stack_id, resource_name, hook_type):
    try:
        hc.resources.signal(
            stack_id=stack_id,
            resource_name=resource_name,
            data={'unset_hook': hook_type})
    except exc.HTTPNotFound:
        logger.error(
            "Stack %(stack)s or resource %(resource)s "
            "not found for hook %(hook_type)s",
            {'resource': resource_name, 'stack': stack_id,
             'hook_type': hook_type})


def clear_wildcard_hooks(hc, stack_id, stack_patterns, hook_type,
                         resource_pattern):
    if stack_patterns:
        for resource in hc.resources.list(stack_id):
            res_name = resource.resource_name
            if fnmatch.fnmatchcase(res_name, stack_patterns[0]):
                nested_stack = hc.resources.get(
                    stack_id=stack_id,
                    resource_name=res_name)
                clear_wildcard_hooks(
                    hc,
                    nested_stack.physical_resource_id,
                    stack_patterns[1:], hook_type, resource_pattern)
    else:
        for resource in hc.resources.list(stack_id):
            res_name = resource.resource_name
            if fnmatch.fnmatchcase(res_name, resource_pattern):
                clear_hook(hc, stack_id, res_name, hook_type)


def get_hook_type_via_status(hc, stack_id):
    # Figure out if the hook should be pre-create, pre-update or
    # pre-delete based on the stack status, also sanity assertions
    # that we're in-progress.
    try:
        stack = hc.stacks.get(stack_id=stack_id)
    except exc.HTTPNotFound:
        raise exc.CommandError(_('Stack not found: %s') % stack_id)
    else:
        if 'IN_PROGRESS' not in stack.stack_status:
            raise exc.CommandError(_('Stack status %s not IN_PROGRESS') %
                                   stack.stack_status)

    if 'CREATE' in stack.stack_status:
        hook_type = 'pre-create'
    elif 'UPDATE' in stack.stack_status:
        hook_type = 'pre-update'
    elif 'DELETE' in stack.stack_status:
        hook_type = 'pre-delete'
    else:
        raise exc.CommandError(_('Unexpected stack status %s, '
                                 'only create, update and delete supported')
                               % stack.stack_status)
    return hook_type