Add nested-depth option to resource-list

Add `--nested-depth` option to allow listing of nested resources up to
`<DEPTH>` levels deep.

Implements: blueprint explode-nested-resources
Change-Id: I401f68d8c2e14179972928920003c75c851ad9cc
This commit is contained in:
Anderson Mesquita
2014-08-12 14:11:03 -04:00
parent 3985302f67
commit ec3fa70a81
4 changed files with 61 additions and 2 deletions

View File

@@ -89,6 +89,27 @@ class ResourceManagerTest(testtools.TestCase):
manager.list(**fields)
def test_list_nested(self):
fields = {'stack_id': 'teststack', 'nested_depth': '99'}
expect = ('/stacks/teststack/resources?nested_depth=99')
key = 'resources'
class FakeResponse(object):
def json(self):
return {key: {}}
class FakeClient(object):
def get(self, *args, **kwargs):
assert args[0] == expect
return FakeResponse()
manager = ResourceManager(FakeClient())
self.m.StubOutWithMock(manager, '_resolve_stack_id')
manager._resolve_stack_id('teststack').AndReturn('teststack/abcd1234')
self.m.ReplayAll()
manager.list(**fields)
def test_metadata(self):
fields = {'stack_id': 'teststack',
'resource_name': 'testresource'}

View File

@@ -1448,6 +1448,34 @@ class ShellTestResources(ShellBase):
--------------+
''', resource_list_text)
def test_resource_list_nested(self):
self._script_keystone_client()
resp_dict = {"resources": [{
"resource_name": "foobar",
"parent_resource": "my_parent_resource",
}]}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
stack_id = 'teststack/1'
http.HTTPClient.json_request(
'GET', '/stacks/%s/resources?nested_depth=99' % (
stack_id)).AndReturn((resp, resp_dict))
self.m.ReplayAll()
shell_cmd = 'resource-list {0} --nested-depth {1}'.format(stack_id, 99)
resource_list_text = self.shell(shell_cmd)
required = [
'resource_name', 'foobar',
'parent_resource', 'my_parent_resource',
]
for field in required:
self.assertRegexpMatches(resource_list_text, field)
def test_resource_show(self):
self._script_keystone_client()
resp_dict = {"resource":

View File

@@ -39,11 +39,13 @@ class Resource(base.Resource):
class ResourceManager(stacks.StackChildManager):
resource_class = Resource
def list(self, stack_id):
def list(self, stack_id, nested_depth=0):
"""Get a list of resources.
:rtype: list of :class:`Resource`
"""
url = '/stacks/%s/resources' % stack_id
if nested_depth:
url += '?nested_depth=%s' % nested_depth
return self._list(url, "resources")
def get(self, stack_id, resource_name):

View File

@@ -581,9 +581,14 @@ def do_template_validate(hc, args):
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the resources for.')
@utils.arg('-n', '--nested-depth', metavar='<DEPTH>',
help='Depth of nested stacks from which to display resources.')
def do_resource_list(hc, args):
'''Show list of resources belonging to a stack.'''
fields = {'stack_id': args.id}
fields = {
'stack_id': args.id,
'nested_depth': args.nested_depth,
}
try:
resources = hc.resources.list(**fields)
except exc.HTTPNotFound:
@@ -596,6 +601,9 @@ def do_resource_list(hc, args):
else:
fields.insert(0, 'resource_name')
if args.nested_depth:
fields.append('parent_resource')
utils.print_list(resources, fields, sortby_index=4)