Merge "Adds --format=log option to event-list"

This commit is contained in:
Jenkins
2015-05-29 09:20:52 +00:00
committed by Gerrit Code Review
3 changed files with 85 additions and 1 deletions

View File

@@ -86,6 +86,32 @@ def print_dict(d, formatters=None):
print(pt.get_string(sortby='Property'))
def event_log_formatter(events):
"""Return the events in log format."""
event_log = []
log_format = _("%(event_date)s %(event_time)s %(event_id)s "
"[%(rsrc_name)s]: %(rsrc_status)s %(rsrc_status_reason)s")
for event in events:
event_time = getattr(event, 'event_time', '')
time_date = event_time.split('T')
try:
event_time = time_date[0]
event_date = time_date[1][:-1]
except IndexError:
event_time = event_date = ''
log = log_format % {
'event_date': event_date, 'event_time': event_time,
'event_id': getattr(event, 'id', ''),
'rsrc_name': getattr(event, 'resource_name', ''),
'rsrc_status': getattr(event, 'resource_status', ''),
'rsrc_status_reason': getattr(event, 'resource_status_reason', '')
}
event_log.append(log)
return "\n".join(event_log)
def find_resource(manager, name_or_id):
"""Helper for the _find_* methods."""
# first try to get entity as integer id

View File

@@ -2285,6 +2285,58 @@ class ShellTestEvents(ShellBase):
for r in required:
self.assertRegexpMatches(event_list_text, r)
def test_stack_event_list_log(self):
self.register_keystone_auth_fixture()
resp_dict = {"events": [
{"event_time": "2013-12-05T14:14:30Z",
"id": self.event_id_one,
"links": [{"href": "http://heat.example.com:8004/foo",
"rel": "self"},
{"href": "http://heat.example.com:8004/foo2",
"rel": "resource"},
{"href": "http://heat.example.com:8004/foo3",
"rel": "stack"}],
"logical_resource_id": "aResource",
"physical_resource_id": None,
"resource_name": "aResource",
"resource_status": "CREATE_IN_PROGRESS",
"resource_status_reason": "state changed"},
{"event_time": "2013-12-05T14:14:30Z",
"id": self.event_id_two,
"links": [{"href": "http://heat.example.com:8004/foo",
"rel": "self"},
{"href": "http://heat.example.com:8004/foo2",
"rel": "resource"},
{"href": "http://heat.example.com:8004/foo3",
"rel": "stack"}],
"logical_resource_id": "aResource",
"physical_resource_id":
"bce15ec4-8919-4a02-8a90-680960fb3731",
"resource_name": "aResource",
"resource_status": "CREATE_COMPLETE",
"resource_status_reason": "state changed"}]}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
stack_id = 'teststack/1'
http.HTTPClient.json_request('GET',
'/stacks/%s/events?sort_dir=asc' %
stack_id).AndReturn((resp, resp_dict))
self.m.ReplayAll()
event_list_text = self.shell('event-list {0} --format log'.format(
stack_id))
expected = '14:14:30 2013-12-05 %s [aResource]: ' \
'CREATE_IN_PROGRESS state changed\n' \
'14:14:30 2013-12-05 %s [aResource]: CREATE_COMPLETE ' \
'state changed\n' % (self.event_id_one, self.event_id_two)
self.assertEqual(expected, event_list_text)
def test_event_show(self):
self.register_keystone_auth_fixture()
resp_dict = {"event":

View File

@@ -921,6 +921,9 @@ def do_hook_clear(hc, args):
@utils.arg('-n', '--nested-depth', metavar='<DEPTH>',
help=_('Depth of nested stacks from which to display events. '
'Note this cannot be specified with --resource.'))
@utils.arg('-F', '--format', metavar='<FORMAT>',
help=_('The output value format, one of: log, table'),
default='table')
def do_event_list(hc, args):
'''List events for a stack.'''
display_fields = ['id', 'resource_status_reason',
@@ -961,7 +964,10 @@ def do_event_list(hc, args):
else:
display_fields.insert(0, 'logical_resource_id')
utils.print_list(events, display_fields, sortby_index=None)
if args.format == 'log':
print(utils.event_log_formatter(events))
else:
utils.print_list(events, display_fields, sortby_index=None)
def _get_hook_type_via_status(hc, stack_id):