Add --nested-depth option to event-list
Adds logic to mimic the resource-list nested-depth option for events. Note that this is pretty inefficient, and in future we should add an API for nested_depth to the events API, but I want this to work for kilo heat, so this interim implementation will work for kilo, then I'll look at an alternative (faster) API-side implementation for Liberty, which will maintain the same CLI interfaces. Change-Id: I76c60ab5b79af9c477af07d5690b8ca6ca4da388
This commit is contained in:
@@ -50,6 +50,15 @@ def link_formatter(links):
|
||||
return '\n'.join(format_link(l) for l in links or [])
|
||||
|
||||
|
||||
def resource_nested_identifier(rsrc):
|
||||
nested_link = [l for l in rsrc.links or []
|
||||
if l.get('rel') == 'nested']
|
||||
if nested_link:
|
||||
nested_href = nested_link[0].get('href')
|
||||
nested_identifier = nested_href.split("/")[-2:]
|
||||
return "/".join(nested_identifier)
|
||||
|
||||
|
||||
def json_formatter(js):
|
||||
return jsonutils.dumps(js, indent=2, ensure_ascii=False,
|
||||
separators=(', ', ': '))
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import fixtures
|
||||
import mock
|
||||
import os
|
||||
from oslotest import mockpatch
|
||||
import re
|
||||
@@ -39,6 +40,8 @@ from heatclient.common import utils
|
||||
from heatclient import exc
|
||||
import heatclient.shell
|
||||
from heatclient.tests import fakes
|
||||
from heatclient.v1 import events as hc_ev
|
||||
from heatclient.v1 import resources as hc_res
|
||||
import heatclient.v1.shell
|
||||
|
||||
load_tests = testscenarios.load_tests_apply_scenarios
|
||||
@@ -2204,6 +2207,218 @@ class ShellTestEvents(ShellBase):
|
||||
self.assertRegexpMatches(event_list_text, r)
|
||||
|
||||
|
||||
class ShellTestEventsNested(ShellBase):
|
||||
def setUp(self):
|
||||
super(ShellTestEventsNested, self).setUp()
|
||||
self.set_fake_env(FAKE_ENV_KEYSTONE_V2)
|
||||
|
||||
@staticmethod
|
||||
def _mock_resource(resource_id, nested_id=None):
|
||||
res_info = {"links": [{"href": "http://heat/foo", "rel": "self"},
|
||||
{"href": "http://heat/foo2", "rel": "resource"}],
|
||||
"logical_resource_id": resource_id,
|
||||
"physical_resource_id": resource_id,
|
||||
"resource_status": "CREATE_COMPLETE",
|
||||
"resource_status_reason": "state changed",
|
||||
"resource_type": "OS::Nested::Server",
|
||||
"updated_time": "2014-01-06T16:14:26Z"}
|
||||
if nested_id:
|
||||
nested_link = {"href": "http://heat/%s" % nested_id,
|
||||
"rel": "nested"}
|
||||
res_info["links"].append(nested_link)
|
||||
return hc_res.Resource(manager=None, info=res_info)
|
||||
|
||||
@staticmethod
|
||||
def _mock_event(event_id, resource_id):
|
||||
ev_info = {"links": [{"href": "http://heat/foo", "rel": "self"}],
|
||||
"logical_resource_id": resource_id,
|
||||
"physical_resource_id": resource_id,
|
||||
"resource_status": "CREATE_COMPLETE",
|
||||
"resource_status_reason": "state changed",
|
||||
"event_time": "2014-12-05T14:14:30Z",
|
||||
"id": event_id}
|
||||
return hc_ev.Event(manager=None, info=ev_info)
|
||||
|
||||
def test_get_nested_ids(self):
|
||||
def list_stub(stack_id):
|
||||
return [self._mock_resource('aresource', 'foo3/3id')]
|
||||
mock_client = mock.MagicMock()
|
||||
mock_client.resources.list.side_effect = list_stub
|
||||
ids = heatclient.v1.shell._get_nested_ids(hc=mock_client,
|
||||
stack_id='astack/123')
|
||||
mock_client.resources.list.assert_called_once_with(
|
||||
stack_id='astack/123')
|
||||
self.assertEqual(['foo3/3id'], ids)
|
||||
|
||||
def test_get_stack_events(self):
|
||||
def event_stub(stack_id, argfoo):
|
||||
return [self._mock_event('event1', 'aresource')]
|
||||
mock_client = mock.MagicMock()
|
||||
mock_client.events.list.side_effect = event_stub
|
||||
ev_args = {'argfoo': 123}
|
||||
evs = heatclient.v1.shell._get_stack_events(hc=mock_client,
|
||||
stack_id='astack/123',
|
||||
event_args=ev_args)
|
||||
mock_client.events.list.assert_called_once_with(
|
||||
stack_id='astack/123', argfoo=123)
|
||||
self.assertEqual(1, len(evs))
|
||||
self.assertEqual('event1', evs[0].id)
|
||||
self.assertEqual('astack', evs[0].stack_name)
|
||||
|
||||
def test_get_nested_events(self):
|
||||
resources = {'parent': self._mock_resource('resource1', 'foo/child1'),
|
||||
'foo/child1': self._mock_resource('res_child1',
|
||||
'foo/child2'),
|
||||
'foo/child2': self._mock_resource('res_child2',
|
||||
'foo/child3'),
|
||||
'foo/child3': self._mock_resource('res_child3',
|
||||
'foo/END')}
|
||||
|
||||
def resource_list_stub(stack_id):
|
||||
return [resources[stack_id]]
|
||||
mock_client = mock.MagicMock()
|
||||
mock_client.resources.list.side_effect = resource_list_stub
|
||||
|
||||
events = {'foo/child1': self._mock_event('event1', 'res_child1'),
|
||||
'foo/child2': self._mock_event('event2', 'res_child2'),
|
||||
'foo/child3': self._mock_event('event3', 'res_child3')}
|
||||
|
||||
def event_list_stub(stack_id, argfoo):
|
||||
return [events[stack_id]]
|
||||
mock_client.events.list.side_effect = event_list_stub
|
||||
|
||||
ev_args = {'argfoo': 123}
|
||||
# Check nested_depth=1 (non recursive)..
|
||||
evs = heatclient.v1.shell._get_nested_events(hc=mock_client,
|
||||
nested_depth=1,
|
||||
stack_id='parent',
|
||||
event_args=ev_args)
|
||||
|
||||
rsrc_calls = [mock.call(stack_id='parent')]
|
||||
mock_client.resources.list.assert_has_calls(rsrc_calls)
|
||||
ev_calls = [mock.call(stack_id='foo/child1', argfoo=123)]
|
||||
mock_client.events.list.assert_has_calls(ev_calls)
|
||||
self.assertEqual(1, len(evs))
|
||||
self.assertEqual('event1', evs[0].id)
|
||||
|
||||
# ..and the recursive case via nested_depth=3
|
||||
mock_client.resources.list.reset_mock()
|
||||
mock_client.events.list.reset_mock()
|
||||
evs = heatclient.v1.shell._get_nested_events(hc=mock_client,
|
||||
nested_depth=3,
|
||||
stack_id='parent',
|
||||
event_args=ev_args)
|
||||
|
||||
rsrc_calls = [mock.call(stack_id='parent'),
|
||||
mock.call(stack_id='foo/child1'),
|
||||
mock.call(stack_id='foo/child2')]
|
||||
mock_client.resources.list.assert_has_calls(rsrc_calls)
|
||||
ev_calls = [mock.call(stack_id='foo/child1', argfoo=123),
|
||||
mock.call(stack_id='foo/child2', argfoo=123),
|
||||
mock.call(stack_id='foo/child3', argfoo=123)]
|
||||
mock_client.events.list.assert_has_calls(ev_calls)
|
||||
self.assertEqual(3, len(evs))
|
||||
self.assertEqual('event1', evs[0].id)
|
||||
self.assertEqual('event2', evs[1].id)
|
||||
self.assertEqual('event3', evs[2].id)
|
||||
|
||||
def test_shell_nested_depth_invalid_xor(self):
|
||||
self.register_keystone_auth_fixture()
|
||||
stack_id = 'teststack/1'
|
||||
resource_name = 'aResource'
|
||||
|
||||
self.m.ReplayAll()
|
||||
|
||||
error = self.assertRaises(
|
||||
exc.CommandError, self.shell,
|
||||
'event-list {0} --resource {1} --nested-depth 5'.format(
|
||||
stack_id, resource_name))
|
||||
self.assertIn('--nested-depth cannot be specified with --resource',
|
||||
str(error))
|
||||
|
||||
def test_shell_nested_depth_invalid_value(self):
|
||||
self.register_keystone_auth_fixture()
|
||||
stack_id = 'teststack/1'
|
||||
resource_name = 'aResource'
|
||||
error = self.assertRaises(
|
||||
exc.CommandError, self.shell,
|
||||
'event-list {0} --nested-depth Z'.format(
|
||||
stack_id, resource_name))
|
||||
self.assertIn('--nested-depth invalid value Z', str(error))
|
||||
|
||||
def test_shell_nested_depth_zero(self):
|
||||
self.register_keystone_auth_fixture()
|
||||
resp_dict = {"events": [{"id": 'eventid1'},
|
||||
{"id": 'eventid2'}]}
|
||||
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()
|
||||
list_text = self.shell('event-list %s --nested-depth 0' % stack_id)
|
||||
required = ['id', 'eventid1', 'eventid2']
|
||||
for r in required:
|
||||
self.assertRegexpMatches(list_text, r)
|
||||
|
||||
def test_shell_nested_depth(self):
|
||||
self.register_keystone_auth_fixture()
|
||||
stack_id = 'teststack/1'
|
||||
nested_id = 'nested/2'
|
||||
|
||||
# Stub events for parent stack
|
||||
ev_resp_dict = {"events": [{"id": 'eventid1'},
|
||||
{"id": 'eventid2'}]}
|
||||
ev_resp = fakes.FakeHTTPResponse(
|
||||
200,
|
||||
'OK',
|
||||
{'content-type': 'application/json'},
|
||||
jsonutils.dumps(ev_resp_dict))
|
||||
http.HTTPClient.json_request(
|
||||
'GET', '/stacks/%s/events?sort_dir=asc' % (
|
||||
stack_id)).AndReturn((ev_resp, ev_resp_dict))
|
||||
|
||||
# Stub resources for parent, including one nested
|
||||
res_resp_dict = {"resources": [
|
||||
{"links": [{"href": "http://heat/foo", "rel": "self"},
|
||||
{"href": "http://heat/foo2",
|
||||
"rel": "resource"},
|
||||
{"href": "http://heat/%s" % nested_id,
|
||||
"rel": "nested"}],
|
||||
"resource_type": "OS::Nested::Foo"}]}
|
||||
res_resp = fakes.FakeHTTPResponse(
|
||||
200,
|
||||
'OK',
|
||||
{'content-type': 'application/json'},
|
||||
jsonutils.dumps(res_resp_dict))
|
||||
http.HTTPClient.json_request(
|
||||
'GET', '/stacks/%s/resources' % (
|
||||
stack_id)).AndReturn((res_resp, res_resp_dict))
|
||||
|
||||
# Stub the events for the nested stack
|
||||
nev_resp_dict = {"events": [{"id": 'n_eventid1'},
|
||||
{"id": 'n_eventid2'}]}
|
||||
nev_resp = fakes.FakeHTTPResponse(
|
||||
200,
|
||||
'OK',
|
||||
{'content-type': 'application/json'},
|
||||
jsonutils.dumps(nev_resp_dict))
|
||||
http.HTTPClient.json_request(
|
||||
'GET', '/stacks/%s/events?sort_dir=asc' % (
|
||||
nested_id)).AndReturn((nev_resp, nev_resp_dict))
|
||||
|
||||
self.m.ReplayAll()
|
||||
list_text = self.shell('event-list %s --nested-depth 1' % stack_id)
|
||||
required = ['id', 'eventid1', 'eventid2', 'n_eventid1', 'n_eventid2',
|
||||
'stack_name', 'teststack', 'nested']
|
||||
for r in required:
|
||||
self.assertRegexpMatches(list_text, r)
|
||||
|
||||
|
||||
class ShellTestResources(ShellBase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
# under the License.
|
||||
from heatclient.common import utils
|
||||
from heatclient import exc
|
||||
from heatclient.v1 import resources as hc_res
|
||||
import mock
|
||||
import os
|
||||
import testtools
|
||||
@@ -115,6 +116,26 @@ class ShellTest(testtools.TestCase):
|
||||
{'hrf': 'http://foo.example.com'},
|
||||
{}]))
|
||||
|
||||
def test_resource_nested_identifier(self):
|
||||
rsrc_info = {'resource_name': 'aresource',
|
||||
'links': [{'href': u'http://foo/name/id/resources/0',
|
||||
'rel': u'self'},
|
||||
{'href': u'http://foo/name/id',
|
||||
'rel': u'stack'},
|
||||
{'href': u'http://foo/n_name/n_id',
|
||||
'rel': u'nested'}]}
|
||||
rsrc = hc_res.Resource(manager=None, info=rsrc_info)
|
||||
self.assertEqual('n_name/n_id', utils.resource_nested_identifier(rsrc))
|
||||
|
||||
def test_resource_nested_identifier_none(self):
|
||||
rsrc_info = {'resource_name': 'aresource',
|
||||
'links': [{'href': u'http://foo/name/id/resources/0',
|
||||
'rel': u'self'},
|
||||
{'href': u'http://foo/name/id',
|
||||
'rel': u'stack'}]}
|
||||
rsrc = hc_res.Resource(manager=None, info=rsrc_info)
|
||||
self.assertIsNone(utils.resource_nested_identifier(rsrc))
|
||||
|
||||
def test_json_formatter(self):
|
||||
self.assertEqual('null', utils.json_formatter(None))
|
||||
self.assertEqual('{}', utils.json_formatter({}))
|
||||
|
||||
@@ -870,6 +870,51 @@ def do_hook_clear(hc, args):
|
||||
clear_wildcard_hooks(stack_id, hook[:-1])
|
||||
|
||||
|
||||
def _get_nested_ids(hc, stack_id):
|
||||
nested_ids = []
|
||||
try:
|
||||
resources = hc.resources.list(stack_id=stack_id)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError(_('Stack not found: %s') % stack_id)
|
||||
for r in resources:
|
||||
nested_id = utils.resource_nested_identifier(r)
|
||||
if nested_id:
|
||||
nested_ids.append(nested_id)
|
||||
return nested_ids
|
||||
|
||||
|
||||
def _get_nested_events(hc, nested_depth, stack_id, event_args):
|
||||
# FIXME(shardy): this is very inefficient, we should add nested_depth to
|
||||
# the event_list API in a future heat version, but this will be required
|
||||
# until kilo heat is EOL.
|
||||
nested_ids = _get_nested_ids(hc, stack_id)
|
||||
nested_events = []
|
||||
for n_id in nested_ids:
|
||||
stack_events = _get_stack_events(hc, n_id, event_args)
|
||||
if stack_events:
|
||||
nested_events.extend(stack_events)
|
||||
if nested_depth > 1:
|
||||
next_depth = nested_depth - 1
|
||||
nested_events.extend(_get_nested_events(
|
||||
hc, next_depth, n_id, event_args))
|
||||
return nested_events
|
||||
|
||||
|
||||
def _get_stack_events(hc, stack_id, event_args):
|
||||
event_args['stack_id'] = stack_id
|
||||
try:
|
||||
events = hc.events.list(**event_args)
|
||||
except exc.HTTPNotFound as ex:
|
||||
# it could be the stack or resource that is not found
|
||||
# just use the message that the server sent us.
|
||||
raise exc.CommandError(str(ex))
|
||||
else:
|
||||
# Show which stack the event comes from (for nested events)
|
||||
for e in events:
|
||||
e.stack_name = stack_id.split("/")[0]
|
||||
return events
|
||||
|
||||
|
||||
@utils.arg('id', metavar='<NAME or ID>',
|
||||
help=_('Name or ID of stack to show the events for.'))
|
||||
@utils.arg('-r', '--resource', metavar='<RESOURCE>',
|
||||
@@ -883,29 +928,48 @@ def do_hook_clear(hc, args):
|
||||
help=_('Limit the number of events returned.'))
|
||||
@utils.arg('-m', '--marker', metavar='<ID>',
|
||||
help=_('Only return events that appear after the given event ID.'))
|
||||
@utils.arg('-n', '--nested-depth', metavar='<DEPTH>',
|
||||
help=_('Depth of nested stacks from which to display events. '
|
||||
'Note this cannot be specified with --resource.'))
|
||||
def do_event_list(hc, args):
|
||||
'''List events for a stack.'''
|
||||
fields = {'stack_id': args.id,
|
||||
'resource_name': args.resource,
|
||||
'limit': args.limit,
|
||||
'marker': args.marker,
|
||||
'filters': utils.format_parameters(args.filters),
|
||||
'sort_dir': 'asc'}
|
||||
try:
|
||||
events = hc.events.list(**fields)
|
||||
except exc.HTTPNotFound as ex:
|
||||
# it could be the stack or resource that is not found
|
||||
# just use the message that the server sent us.
|
||||
raise exc.CommandError(str(ex))
|
||||
display_fields = ['id', 'resource_status_reason',
|
||||
'resource_status', 'event_time']
|
||||
event_args = {'resource_name': args.resource,
|
||||
'limit': args.limit,
|
||||
'marker': args.marker,
|
||||
'filters': utils.format_parameters(args.filters),
|
||||
'sort_dir': 'asc'}
|
||||
|
||||
# Specifying a resource in recursive mode makes no sense..
|
||||
if args.nested_depth and args.resource:
|
||||
msg = _("--nested-depth cannot be specified with --resource")
|
||||
raise exc.CommandError(msg)
|
||||
|
||||
if args.nested_depth:
|
||||
try:
|
||||
nested_depth = int(args.nested_depth)
|
||||
except ValueError:
|
||||
msg = _("--nested-depth invalid value %s") % args.nested_depth
|
||||
raise exc.CommandError(msg)
|
||||
else:
|
||||
fields = ['id', 'resource_status_reason',
|
||||
'resource_status', 'event_time']
|
||||
if len(events) >= 1:
|
||||
if hasattr(events[0], 'resource_name'):
|
||||
fields.insert(0, 'resource_name')
|
||||
else:
|
||||
fields.insert(0, 'logical_resource_id')
|
||||
utils.print_list(events, fields, sortby_index=None)
|
||||
nested_depth = 0
|
||||
|
||||
events = _get_stack_events(hc, stack_id=args.id, event_args=event_args)
|
||||
sortby_index = None
|
||||
|
||||
if nested_depth > 0:
|
||||
events.extend(_get_nested_events(hc, nested_depth,
|
||||
args.id, event_args))
|
||||
display_fields.append('stack_name')
|
||||
sortby_index = display_fields.index('event_time')
|
||||
|
||||
if len(events) >= 1:
|
||||
if hasattr(events[0], 'resource_name'):
|
||||
display_fields.insert(0, 'resource_name')
|
||||
else:
|
||||
display_fields.insert(0, 'logical_resource_id')
|
||||
utils.print_list(events, display_fields, sortby_index=sortby_index)
|
||||
|
||||
|
||||
@utils.arg('id', metavar='<NAME or ID>',
|
||||
|
||||
Reference in New Issue
Block a user