Browse Source

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
Rui Chen 4 years ago
parent
commit
4e76f9590b

+ 24
- 0
novaclient/tests/unit/v1_1/fakes.py View File

@@ -16,6 +16,7 @@
16 16
 
17 17
 import datetime
18 18
 
19
+import mock
19 20
 from oslo.utils import strutils
20 21
 import six
21 22
 from six.moves.urllib import parse
@@ -2185,3 +2186,26 @@ class FakeHTTPClient(base_client.HTTPClient):
2185 2186
     def delete_os_server_groups_2cbd51f4_fafe_4cdb_801b_cf913a6f288b(
2186 2187
             self, **kw):
2187 2188
         return (202, {}, None)
2189
+
2190
+
2191
+class FakeSessionClient(fakes.FakeClient, client.Client):
2192
+
2193
+    def __init__(self, *args, **kwargs):
2194
+        client.Client.__init__(self, 'username', 'password',
2195
+                               'project_id', 'auth_url',
2196
+                               extensions=kwargs.get('extensions'))
2197
+        self.client = FakeSessionMockClient(**kwargs)
2198
+
2199
+
2200
+class FakeSessionMockClient(base_client.SessionClient, FakeHTTPClient):
2201
+
2202
+    def __init__(self, *args, **kwargs):
2203
+
2204
+        self.callstack = []
2205
+        self.auth = mock.Mock()
2206
+        self.session = mock.Mock()
2207
+
2208
+        self.auth.get_auth_ref.return_value.project_id = 'tenant_id'
2209
+
2210
+    def request(self, url, method, **kwargs):
2211
+        return self._cs_request(url, method, **kwargs)

+ 10
- 0
novaclient/tests/unit/v1_1/test_shell.py View File

@@ -2338,6 +2338,16 @@ class ShellTest(utils.TestCase):
2338 2338
         self.assert_called('DELETE', '/os-server-groups/12345', pos=-2)
2339 2339
 
2340 2340
 
2341
+class ShellWithSessionClientTest(ShellTest):
2342
+
2343
+    def setUp(self):
2344
+        """Run before each test."""
2345
+        super(ShellWithSessionClientTest, self).setUp()
2346
+        self.useFixture(fixtures.MonkeyPatch(
2347
+            'novaclient.client.get_client_class',
2348
+            lambda *_: fakes.FakeSessionClient))
2349
+
2350
+
2341 2351
 class GetSecgroupTest(utils.TestCase):
2342 2352
     def test_with_integer(self):
2343 2353
         cs = mock.Mock(**{

+ 16
- 6
novaclient/v1_1/shell.py View File

@@ -3876,10 +3876,15 @@ def _quota_update(manager, identifier, args):
3876 3876
 def do_quota_show(cs, args):
3877 3877
     """List the quotas for a tenant/user."""
3878 3878
 
3879
-    if not args.tenant:
3880
-        _quota_show(cs.quotas.get(cs.client.tenant_id, user_id=args.user))
3879
+    if args.tenant:
3880
+        project_id = args.tenant
3881
+    elif isinstance(cs.client, client.SessionClient):
3882
+        auth = cs.client.auth
3883
+        project_id = auth.get_auth_ref(cs.client.session).project_id
3881 3884
     else:
3882
-        _quota_show(cs.quotas.get(args.tenant, user_id=args.user))
3885
+        project_id = cs.client.tenant_id
3886
+
3887
+    _quota_show(cs.quotas.get(project_id, user_id=args.user))
3883 3888
 
3884 3889
 
3885 3890
 @cliutils.arg(
@@ -3890,10 +3895,15 @@ def do_quota_show(cs, args):
3890 3895
 def do_quota_defaults(cs, args):
3891 3896
     """List the default quotas for a tenant."""
3892 3897
 
3893
-    if not args.tenant:
3894
-        _quota_show(cs.quotas.defaults(cs.client.tenant_id))
3898
+    if args.tenant:
3899
+        project_id = args.tenant
3900
+    elif isinstance(cs.client, client.SessionClient):
3901
+        auth = cs.client.auth
3902
+        project_id = auth.get_auth_ref(cs.client.session).project_id
3895 3903
     else:
3896
-        _quota_show(cs.quotas.defaults(args.tenant))
3904
+        project_id = cs.client.tenant_id
3905
+
3906
+    _quota_show(cs.quotas.defaults(project_id))
3897 3907
 
3898 3908
 
3899 3909
 @cliutils.arg(

Loading…
Cancel
Save