From 2cae5183fef485eb4224d8a3c957bc335cefc034 Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Fri, 3 Jul 2015 18:00:23 +0800 Subject: [PATCH] Support 'with_attr' parameter for resource-show Support the 'with_attr' parameter when showing resource. Closes-Bug: #1428436 Change-Id: I430bf27ace4fedd55ee158f9f4fa3edc737892a4 --- heatclient/tests/unit/test_resources.py | 13 +++++++++++++ heatclient/tests/unit/test_shell.py | 16 +++++++++++----- heatclient/v1/resources.py | 7 ++++++- heatclient/v1/shell.py | 6 ++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/heatclient/tests/unit/test_resources.py b/heatclient/tests/unit/test_resources.py index 12a7ccc4..1444af90 100644 --- a/heatclient/tests/unit/test_resources.py +++ b/heatclient/tests/unit/test_resources.py @@ -80,6 +80,19 @@ class ResourceManagerTest(testtools.TestCase): manager.get(**fields) self.m.VerifyAll() + def test_get_with_attr(self): + fields = {'stack_id': 'teststack', + 'resource_name': 'testresource', + 'with_attr': ['attr_a', 'attr_b']} + expect = ('GET', + '/stacks/teststack%2Fabcd1234/resources' + '/testresource?with_attr=attr_a&with_attr=attr_b') + key = 'resource' + + manager = self._base_test(expect, key) + manager.get(**fields) + self.m.VerifyAll() + def test_get_with_unicode_resource_name(self): fields = {'stack_id': 'teststack', 'resource_name': u'\u5de5\u4f5c'} diff --git a/heatclient/tests/unit/test_shell.py b/heatclient/tests/unit/test_shell.py index b4350532..c79af84f 100644 --- a/heatclient/tests/unit/test_shell.py +++ b/heatclient/tests/unit/test_shell.py @@ -3271,7 +3271,7 @@ class ShellTestResources(ShellBase): for field in required: self.assertRegexpMatches(resource_list_text, field) - def test_resource_show(self): + def test_resource_show_with_attrs(self): self.register_keystone_auth_fixture() resp_dict = {"resource": {"description": "", @@ -3287,7 +3287,11 @@ class ShellTestResources(ShellBase): "resource_status": "CREATE_COMPLETE", "resource_status_reason": "state changed", "resource_type": "OS::Nova::Server", - "updated_time": "2014-01-06T16:14:26Z"}} + "updated_time": "2014-01-06T16:14:26Z", + "creation_time": "2014-01-06T16:14:26Z", + "attributes": { + "attr_a": "value_of_attr_a", + "attr_b": "value_of_attr_b"}}} resp = fakes.FakeHTTPResponse( 200, 'OK', @@ -3296,7 +3300,7 @@ class ShellTestResources(ShellBase): stack_id = 'teststack/1' resource_name = 'aResource' http.SessionClient.request( - '/stacks/%s/resources/%s' % + '/stacks/%s/resources/%s?with_attr=attr_a&with_attr=attr_b' % ( parse.quote(stack_id, ''), parse.quote(encodeutils.safe_encode( @@ -3305,8 +3309,10 @@ class ShellTestResources(ShellBase): self.m.ReplayAll() - resource_show_text = self.shell('resource-show {0} {1}'.format( - stack_id, resource_name)) + resource_show_text = self.shell( + 'resource-show {0} {1} --with-attr attr_a ' + '--with-attr attr_b'.format( + stack_id, resource_name)) required = [ 'description', diff --git a/heatclient/v1/resources.py b/heatclient/v1/resources.py index e84c4912..74987b11 100644 --- a/heatclient/v1/resources.py +++ b/heatclient/v1/resources.py @@ -57,16 +57,21 @@ class ResourceManager(stacks.StackChildManager): url += '?nested_depth=%s' % nested_depth return self._list(url, "resources") - def get(self, stack_id, resource_name): + def get(self, stack_id, resource_name, with_attr=None): """Get the details for a specific resource. :param stack_id: ID of stack containing the resource :param resource_name: ID of resource to get the details for + :param with_attr: Attributes to show """ stack_id = self._resolve_stack_id(stack_id) url_str = '/stacks/%s/resources/%s' % ( parse.quote(stack_id, ''), parse.quote(encodeutils.safe_encode(resource_name), '')) + if with_attr: + params = {'with_attr': with_attr} + url_str += '?%s' % parse.urlencode(params, True) + resp = self.client.get(url_str) body = utils.get_response_body(resp) return Resource(self, body.get('resource')) diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index a4d7af92..d0fa28a0 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -764,10 +764,16 @@ def do_resource_list(hc, args): help=_('Name or ID of stack to show the resource for.')) @utils.arg('resource', metavar='', help=_('Name of the resource to show the details for.')) +@utils.arg('-a', '--with-attr', metavar='', + help=_('Attribute to show, it can be specified ' + 'multiple times.'), + action='append') def do_resource_show(hc, args): '''Describe the resource.''' fields = {'stack_id': args.id, 'resource_name': args.resource} + if args.with_attr: + fields['with_attr'] = list(args.with_attr) try: resource = hc.resources.get(**fields) except exc.HTTPNotFound: