From 3ee70cf4e8c2d8505230f73ef0b6b44185567dc2 Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Sat, 24 Dec 2016 11:28:35 +0800 Subject: [PATCH] Show 'project' info if heat server returns Show 'project'/'Project' info in heatclient if heat server returns 'project' info of stacks. Change-Id: I7d95ae96a678ef41bcd3d5379f204db83e4b585c Closes-Bug: #1652412 --- heatclient/osc/v1/stack.py | 7 ++-- heatclient/tests/unit/osc/v1/test_stack.py | 21 +++++++++++ heatclient/tests/unit/test_shell.py | 42 +++++++++++----------- heatclient/v1/shell.py | 7 ++-- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/heatclient/osc/v1/stack.py b/heatclient/osc/v1/stack.py index cf03cec6..4b04dff4 100644 --- a/heatclient/osc/v1/stack.py +++ b/heatclient/osc/v1/stack.py @@ -623,8 +623,6 @@ def _list(client, args=None): columns.pop() if args.long: columns.insert(2, 'Stack Owner') - if args.long or args.all_projects: - columns.insert(2, 'Project') if args.nested: columns.append('Parent') @@ -634,6 +632,11 @@ def _list(client, args=None): columns.append('Deletion Time') data = client.stacks.list(**kwargs) + data = list(data) + for stk in data: + if hasattr(stk, 'project'): + columns.insert(2, 'Project') + break data = utils.sort_items(data, args.sort if args else None) return ( diff --git a/heatclient/tests/unit/osc/v1/test_stack.py b/heatclient/tests/unit/osc/v1/test_stack.py index c40bdf19..7481acb8 100644 --- a/heatclient/tests/unit/osc/v1/test_stack.py +++ b/heatclient/tests/unit/osc/v1/test_stack.py @@ -453,6 +453,9 @@ class TestStackList(TestStack): 'deletion_time': '2015-10-21T07:50:00Z', } + data_with_project = copy.deepcopy(data) + data_with_project['project'] = 'test_project' + def setUp(self): super(TestStackList, self).setUp() self.cmd = stack.ListStack(self.app, None) @@ -495,6 +498,8 @@ class TestStackList(TestStack): self.assertEqual(cols, columns) def test_stack_list_all_projects(self): + self.stack_client.list.return_value = [ + stacks.Stack(None, self.data_with_project)] kwargs = copy.deepcopy(self.defaults) kwargs['global_tenant'] = True cols = copy.deepcopy(self.columns) @@ -507,7 +512,23 @@ class TestStackList(TestStack): self.stack_client.list.assert_called_with(**kwargs) self.assertEqual(cols, columns) + def test_stack_list_with_project(self): + self.stack_client.list.return_value = [ + stacks.Stack(None, self.data_with_project)] + kwargs = copy.deepcopy(self.defaults) + cols = copy.deepcopy(self.columns) + cols.insert(2, 'Project') + arglist = [] + parsed_args = self.check_parser(self.cmd, arglist, []) + + columns, data = self.cmd.take_action(parsed_args) + + self.stack_client.list.assert_called_with(**kwargs) + self.assertEqual(cols, columns) + def test_stack_list_long(self): + self.stack_client.list.return_value = [ + stacks.Stack(None, self.data_with_project)] kwargs = copy.deepcopy(self.defaults) kwargs['global_tenant'] = True cols = copy.deepcopy(self.columns) diff --git a/heatclient/tests/unit/test_shell.py b/heatclient/tests/unit/test_shell.py index 18173ded..abb58456 100644 --- a/heatclient/tests/unit/test_shell.py +++ b/heatclient/tests/unit/test_shell.py @@ -126,25 +126,25 @@ class TestCase(testtools.TestCase): mockfixture = self.useFixture(mockpatch.Patch(target, **kwargs)) return mockfixture.mock - def stack_list_resp_dict(self, show_nested=False): - resp_dict = {"stacks": [ - { - "id": "1", - "stack_name": "teststack", - "stack_owner": "testowner", - "project": "testproject", - "stack_status": 'CREATE_COMPLETE', - "creation_time": "2012-10-25T01:58:47Z" - }, - { - "id": "2", - "stack_name": "teststack2", - "stack_owner": "testowner", - "project": "testproject", - "stack_status": 'IN_PROGRESS', - "creation_time": "2012-10-25T01:58:47Z" - }] - } + def stack_list_resp_dict(self, show_nested=False, include_project=False): + stack1 = { + "id": "1", + "stack_name": "teststack", + "stack_owner": "testowner", + "stack_status": 'CREATE_COMPLETE', + "creation_time": "2012-10-25T01:58:47Z"} + stack2 = { + "id": "2", + "stack_name": "teststack2", + "stack_owner": "testowner", + "stack_status": 'IN_PROGRESS', + "creation_time": "2012-10-25T01:58:47Z" + } + if include_project: + stack1['project'] = 'testproject' + stack1['project'] = 'testproject' + + resp_dict = {"stacks": [stack1, stack2]} if show_nested: nested = { "id": "3", @@ -153,6 +153,8 @@ class TestCase(testtools.TestCase): "creation_time": "2012-10-25T01:58:47Z", "parent": "theparentof3" } + if include_project: + nested['project'] = 'testproject' resp_dict["stacks"].append(nested) return resp_dict @@ -4209,7 +4211,7 @@ class MockShellTestUserPass(MockShellBase): def test_stack_list_with_args(self): self.register_keystone_auth_fixture() - resp_dict = self.stack_list_resp_dict() + resp_dict = self.stack_list_resp_dict(include_project=True) resp = fakes.FakeHTTPResponse( 200, 'success, you', diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index a3179b31..a38c32a4 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -689,13 +689,16 @@ def do_stack_list(hc, args=None): if args.global_tenant or args.show_owner: fields.append('stack_owner') - if args.global_tenant: - fields.append('project') if args.show_deleted: fields.append('deletion_time') stacks = hc.stacks.list(**kwargs) + stacks = list(stacks) + for stk in stacks: + if hasattr(stk, 'project'): + fields.append('project') + break utils.print_list(stacks, fields, sortby_index=sortby_index)