Implement quota show command for KB
kingbird quota show lists the global limits for a tenant. Add test-case for the same Change-Id: I7c3f809afcf65077701e320fc97135a3c21c7786
This commit is contained in:
parent
7429c1e479
commit
f57144cfcb
|
@ -14,6 +14,8 @@
|
|||
|
||||
import json
|
||||
|
||||
from kingbirdclient import exceptions
|
||||
|
||||
|
||||
class Resource(object):
|
||||
# This will be overridden by the actual resource
|
||||
|
@ -31,13 +33,8 @@ class ResourceManager(object):
|
|||
def __init__(self, http_client):
|
||||
self.http_client = http_client
|
||||
|
||||
def _list(self, url, response_key=None):
|
||||
resp = self.http_client.get(url)
|
||||
if resp.status_code != 200:
|
||||
self._raise_api_exception(resp)
|
||||
json_response_key = get_json(resp)
|
||||
def _generate_resource(self, json_response_key):
|
||||
json_objects = [json_response_key[item] for item in json_response_key]
|
||||
|
||||
resource = []
|
||||
for json_object in json_objects:
|
||||
for resource_data in json_object:
|
||||
|
@ -45,6 +42,19 @@ class ResourceManager(object):
|
|||
json_object[resource_data]))
|
||||
return resource
|
||||
|
||||
def _list(self, url, response_key=None):
|
||||
resp = self.http_client.get(url)
|
||||
if resp.status_code != 200:
|
||||
self._raise_api_exception(resp)
|
||||
json_response_key = get_json(resp)
|
||||
resource = self._generate_resource(json_response_key)
|
||||
return resource
|
||||
|
||||
def _raise_api_exception(self, resp):
|
||||
error_data = resp.content
|
||||
raise exceptions.APIException(error_code=resp.status_code,
|
||||
error_message=error_data)
|
||||
|
||||
|
||||
def get_json(response):
|
||||
"""Get JSON representation of response."""
|
||||
|
|
|
@ -27,3 +27,10 @@ class quota_manager(base.ResourceManager):
|
|||
tenant = self.http_client.project_id
|
||||
url = '/%s/os-quota-sets/defaults' % tenant
|
||||
return self._list(url)
|
||||
|
||||
def global_limits(self, target_tenant_id=None):
|
||||
tenant = self.http_client.project_id
|
||||
if not target_tenant_id:
|
||||
target_tenant_id = tenant
|
||||
url = '/%s/os-quota-sets/%s' % (tenant, target_tenant_id)
|
||||
return self._list(url)
|
||||
|
|
|
@ -42,3 +42,26 @@ class ListDefaults(base.KingbirdLister):
|
|||
def _get_resources(self, parsed_args):
|
||||
kingbird_client = self.app.client_manager.sync_engine
|
||||
return kingbird_client.quota_manager.list_defaults()
|
||||
|
||||
|
||||
class GlobalLimits(base.KingbirdLister):
|
||||
"""Lists the global limit of a tenant."""
|
||||
|
||||
def _get_format_function(self):
|
||||
return format
|
||||
|
||||
def get_parser(self, parsed_args):
|
||||
parser = super(GlobalLimits, self).get_parser(parsed_args)
|
||||
|
||||
parser.add_argument(
|
||||
'--tenant',
|
||||
help='Lists global limit of a specified tenant-id.'
|
||||
' Admin tenant can perform this operation.'
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
def _get_resources(self, parsed_args):
|
||||
kingbird_client = self.app.client_manager.sync_engine
|
||||
target_tenant_id = parsed_args.tenant
|
||||
return kingbird_client.quota_manager.global_limits(target_tenant_id)
|
||||
|
|
|
@ -47,3 +47,10 @@ class CommandError(KingbirdClientException):
|
|||
def __init__(self, message=None):
|
||||
if message:
|
||||
self.message = message
|
||||
|
||||
|
||||
class APIException(Exception):
|
||||
def __init__(self, error_code=None, error_message=None):
|
||||
super(APIException, self).__init__(error_message)
|
||||
self.error_code = error_code
|
||||
self.error_message = error_message
|
||||
|
|
|
@ -394,6 +394,7 @@ class KingbirdShell(app.App):
|
|||
return {
|
||||
'bash-completion': BashCompletionCommand,
|
||||
'quota defaults': qm.ListDefaults,
|
||||
'quota show': qm.GlobalLimits,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -38,3 +38,13 @@ class TestCLIQuotaManagerV1(base.BaseCommandTest):
|
|||
self.client.quota_manager.list_defaults.return_value = []
|
||||
actual_quota = self.call(quota_cmd.ListDefaults)
|
||||
self.assertEqual((('<none>', '<none>'),), actual_quota[1])
|
||||
|
||||
def test_global_limits(self):
|
||||
self.client.quota_manager.global_limits.return_value = [QUOTAMANAGER]
|
||||
actual_quota = self.call(quota_cmd.GlobalLimits)
|
||||
self.assertEqual([('fake_item', '123')], actual_quota[1])
|
||||
|
||||
def test_negative_global_limits(self):
|
||||
self.client.quota_manager.global_limits.return_value = []
|
||||
actual_quota = self.call(quota_cmd.GlobalLimits)
|
||||
self.assertEqual((('<none>', '<none>'),), actual_quota[1])
|
||||
|
|
Loading…
Reference in New Issue