From 4e76f9590b49c925703cbbf42d1e232725465eff Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Sun, 4 Jan 2015 16:35:12 +0800 Subject: [PATCH] Fix issue of quota-show and quota-defaults quota-show and quota-defaults not work because 'SessionClient' object has no attribute 'tenant_id'. Try to get project_id from auth_ref when cs.client is SessionClient instance. Change-Id: Ic125a99ba34e911485868454c3c7531a34eabdc9 Closes-Bug: #1407388 --- novaclient/tests/unit/v1_1/fakes.py | 24 ++++++++++++++++++++++++ novaclient/tests/unit/v1_1/test_shell.py | 10 ++++++++++ novaclient/v1_1/shell.py | 22 ++++++++++++++++------ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/novaclient/tests/unit/v1_1/fakes.py b/novaclient/tests/unit/v1_1/fakes.py index 6d97374fe..7e659cb3d 100644 --- a/novaclient/tests/unit/v1_1/fakes.py +++ b/novaclient/tests/unit/v1_1/fakes.py @@ -16,6 +16,7 @@ import datetime +import mock from oslo.utils import strutils import six from six.moves.urllib import parse @@ -2185,3 +2186,26 @@ class FakeHTTPClient(base_client.HTTPClient): def delete_os_server_groups_2cbd51f4_fafe_4cdb_801b_cf913a6f288b( self, **kw): return (202, {}, None) + + +class FakeSessionClient(fakes.FakeClient, client.Client): + + def __init__(self, *args, **kwargs): + client.Client.__init__(self, 'username', 'password', + 'project_id', 'auth_url', + extensions=kwargs.get('extensions')) + self.client = FakeSessionMockClient(**kwargs) + + +class FakeSessionMockClient(base_client.SessionClient, FakeHTTPClient): + + def __init__(self, *args, **kwargs): + + self.callstack = [] + self.auth = mock.Mock() + self.session = mock.Mock() + + self.auth.get_auth_ref.return_value.project_id = 'tenant_id' + + def request(self, url, method, **kwargs): + return self._cs_request(url, method, **kwargs) diff --git a/novaclient/tests/unit/v1_1/test_shell.py b/novaclient/tests/unit/v1_1/test_shell.py index ccece8012..cca5d6685 100644 --- a/novaclient/tests/unit/v1_1/test_shell.py +++ b/novaclient/tests/unit/v1_1/test_shell.py @@ -2338,6 +2338,16 @@ class ShellTest(utils.TestCase): self.assert_called('DELETE', '/os-server-groups/12345', pos=-2) +class ShellWithSessionClientTest(ShellTest): + + def setUp(self): + """Run before each test.""" + super(ShellWithSessionClientTest, self).setUp() + self.useFixture(fixtures.MonkeyPatch( + 'novaclient.client.get_client_class', + lambda *_: fakes.FakeSessionClient)) + + class GetSecgroupTest(utils.TestCase): def test_with_integer(self): cs = mock.Mock(**{ diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 6bfbc5115..b6f78d85e 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -3876,10 +3876,15 @@ def _quota_update(manager, identifier, args): def do_quota_show(cs, args): """List the quotas for a tenant/user.""" - if not args.tenant: - _quota_show(cs.quotas.get(cs.client.tenant_id, user_id=args.user)) + if args.tenant: + project_id = args.tenant + elif isinstance(cs.client, client.SessionClient): + auth = cs.client.auth + project_id = auth.get_auth_ref(cs.client.session).project_id else: - _quota_show(cs.quotas.get(args.tenant, user_id=args.user)) + project_id = cs.client.tenant_id + + _quota_show(cs.quotas.get(project_id, user_id=args.user)) @cliutils.arg( @@ -3890,10 +3895,15 @@ def do_quota_show(cs, args): def do_quota_defaults(cs, args): """List the default quotas for a tenant.""" - if not args.tenant: - _quota_show(cs.quotas.defaults(cs.client.tenant_id)) + if args.tenant: + project_id = args.tenant + elif isinstance(cs.client, client.SessionClient): + auth = cs.client.auth + project_id = auth.get_auth_ref(cs.client.session).project_id else: - _quota_show(cs.quotas.defaults(args.tenant)) + project_id = cs.client.tenant_id + + _quota_show(cs.quotas.defaults(project_id)) @cliutils.arg(