diff options
author | Mark Vanderwiel <vanderwl@us.ibm.com> | 2015-12-02 12:07:57 -0600 |
---|---|---|
committer | Mark Vanderwiel <vanderwl@us.ibm.com> | 2016-02-18 09:27:19 -0600 |
commit | 69f41bceae632253d52cc38c3d4d0adff7ea55e5 (patch) | |
tree | 15fc51403256bd1ce5ca11d0b188a9eb2b25076a /heatclient/common/hook_utils.py | |
parent | a5fdf2318e350e89ad5b74d1bdad81450800a355 (diff) | |
download | python-heatclient-69f41bceae632253d52cc38c3d4d0adff7ea55e5.tar.gz |
Add stack hook poll and clear to openstack client
Refactor existing hook helper fuctions into utilites.
based upon heat clis:
heat hook-poll
hest hook-clear
Change-Id: Ib46634cc62369fb5932dcd0967ae492446c79a88
Blueprint: heat-support-python-openstackclient
Diffstat (limited to 'heatclient/common/hook_utils.py')
-rw-r--r-- | heatclient/common/hook_utils.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/heatclient/common/hook_utils.py b/heatclient/common/hook_utils.py new file mode 100644 index 0000000..5cef434 --- /dev/null +++ b/heatclient/common/hook_utils.py @@ -0,0 +1,78 @@ +# 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 + +import heatclient.exc as exc + +from heatclient.openstack.common._i18n import _ +from heatclient.openstack.common._i18n import _LE + +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( + _LE("Stack %(stack)s or resource %(resource)s" + "not found for hook %(hook_type)"), + {'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 or pre-update 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' + else: + raise exc.CommandError(_('Unexpected stack status %s, ' + 'only create/update supported') + % stack.stack_status) + return hook_type |