Debian Codegraph @

heatclient/common/hook_utils.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import fnmatch
14 import logging
15
16 from heatclient._i18n import _
17 from heatclient import exc
18
19 logger = logging.getLogger(__name__)
20
21
22 def clear_hook(hc, stack_id, resource_name, hook_type):
23 try:
24 hc.resources.signal(
25 stack_id=stack_id,
26 resource_name=resource_name,
27 data={'unset_hook': hook_type})
28 except exc.HTTPNotFound:
29 logger.error(
30 "Stack %(stack)s or resource %(resource)s "
31 "not found for hook %(hook_type)s",
32 {'resource': resource_name, 'stack': stack_id,
33 'hook_type': hook_type})
34
35
36 def clear_wildcard_hooks(hc, stack_id, stack_patterns, hook_type,
37 resource_pattern):
38 if stack_patterns:
39 for resource in hc.resources.list(stack_id):
40 res_name = resource.resource_name
41 if fnmatch.fnmatchcase(res_name, stack_patterns[0]):
42 nested_stack = hc.resources.get(
43 stack_id=stack_id,
44 resource_name=res_name)
45 clear_wildcard_hooks(
46 hc,
47 nested_stack.physical_resource_id,
48 stack_patterns[1:], hook_type, resource_pattern)
49 else:
50 for resource in hc.resources.list(stack_id):
51 res_name = resource.resource_name
52 if fnmatch.fnmatchcase(res_name, resource_pattern):
53 clear_hook(hc, stack_id, res_name, hook_type)
54
55
56 def get_hook_type_via_status(hc, stack_id):
57 # Figure out if the hook should be pre-create, pre-update or
58 # pre-delete based on the stack status, also sanity assertions
59 # that we're in-progress.
60 try:
61 stack = hc.stacks.get(stack_id=stack_id)
62 except exc.HTTPNotFound:
63 raise exc.CommandError(_('Stack not found: %s') % stack_id)
64 else:
65 if 'IN_PROGRESS' not in stack.stack_status:
66 raise exc.CommandError(_('Stack status %s not IN_PROGRESS') %
67 stack.stack_status)
68
69 if 'CREATE' in stack.stack_status:
70 hook_type = 'pre-create'
71 elif 'UPDATE' in stack.stack_status:
72 hook_type = 'pre-update'
73 elif 'DELETE' in stack.stack_status:
74 hook_type = 'pre-delete'
75 else:
76 raise exc.CommandError(_('Unexpected stack status %s, '
77 'only create, update and delete supported')
78 % stack.stack_status)
79 return hook_type