diff options
author | Jenkins <jenkins@review.openstack.org> | 2016-12-27 04:15:02 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2016-12-27 04:15:02 +0000 |
commit | 87b03ceac619202a2bca8cc96355f91106112e84 (patch) | |
tree | 4dec115603fa53737cae055bec39ef2a93e5410a /heatclient/common | |
parent | c6bde958db37362d5a91dab43c1b1f7f98d2344a (diff) | |
parent | 6f94cfd791e434e9932da18d785feb934d802a44 (diff) | |
download | python-heatclient-87b03ceac619202a2bca8cc96355f91106112e84.tar.gz |
Merge "Add convenience function for events over websocket"
Diffstat (limited to 'heatclient/common')
-rw-r--r-- | heatclient/common/event_utils.py | 25 | ||||
-rw-r--r-- | heatclient/common/utils.py | 6 |
2 files changed, 29 insertions, 2 deletions
diff --git a/heatclient/common/event_utils.py b/heatclient/common/event_utils.py index 7711554..309a193 100644 --- a/heatclient/common/event_utils.py +++ b/heatclient/common/event_utils.py @@ -18,6 +18,7 @@ import time from heatclient._i18n import _ from heatclient.common import utils import heatclient.exc as exc +from heatclient.v1 import events as events_mod def get_hook_events(hc, stack_id, event_args, nested_depth=0, @@ -216,3 +217,27 @@ def poll_for_events(hc, stack_name, action=None, poll_period=5, marker=None, no_event_polls = 0 time.sleep(poll_period) + + +def wait_for_events(ws, stack_name, out=None): + """Receive events over the passed websocket and wait for final status.""" + msg_template = _("\n Stack %(name)s %(status)s \n") + if not out: + out = sys.stdout + event_log_context = utils.EventLogContext() + while True: + data = ws.recv()['body'] + event = events_mod.Event(None, data['payload'], True) + # Keep compatibility with the HTTP API + event.event_time = data['timestamp'] + event.resource_status = '%s_%s' % (event.resource_action, + event.resource_status) + events_log = utils.event_log_formatter([event], event_log_context) + out.write(events_log) + out.write('\n') + if data['payload']['resource_name'] == stack_name: + stack_status = data['payload']['resource_status'] + if stack_status in ('COMPLETE', 'FAILED'): + msg = msg_template % dict( + name=stack_name, status=event.resource_status) + return '%s_%s' % (event.resource_action, stack_status), msg diff --git a/heatclient/common/utils.py b/heatclient/common/utils.py index bc6f716..2408ab2 100644 --- a/heatclient/common/utils.py +++ b/heatclient/common/utils.py @@ -208,6 +208,8 @@ class EventLogContext(object): # future calls to build_resource_name def get_stack_id(): + if getattr(event, 'stack_id', None) is not None: + return event.stack_id for l in getattr(event, 'links', []): if l.get('rel') == 'stack': if 'href' not in l: @@ -218,8 +220,8 @@ class EventLogContext(object): stack_id = get_stack_id() if not stack_id: return res_name - phys_id = getattr(event, 'physical_resource_id') - status = getattr(event, 'resource_status') + phys_id = getattr(event, 'physical_resource_id', None) + status = getattr(event, 'resource_status', None) is_stack_event = stack_id == phys_id if is_stack_event: |