From 3a22b3fcddb89260b52d2be6993abd7c399fc301 Mon Sep 17 00:00:00 2001 From: chenying Date: Tue, 14 Nov 2017 19:38:42 +0800 Subject: [PATCH] Add commands for quota class API Change-Id: Id1710f986d7ff1943fe3841cfd9837d2316419e0 Implements: blueprint support-quotas-in-karbor --- .../tests/unit/v1/test_quota_classes.py | 40 ++++++++++++++++++ karborclient/v1/client.py | 2 + karborclient/v1/quota_classes.py | 42 +++++++++++++++++++ karborclient/v1/shell.py | 30 +++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 karborclient/tests/unit/v1/test_quota_classes.py create mode 100644 karborclient/v1/quota_classes.py diff --git a/karborclient/tests/unit/v1/test_quota_classes.py b/karborclient/tests/unit/v1/test_quota_classes.py new file mode 100644 index 0000000..d5d93b4 --- /dev/null +++ b/karborclient/tests/unit/v1/test_quota_classes.py @@ -0,0 +1,40 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from karborclient.tests.unit import base +from karborclient.tests.unit.v1 import fakes + +cs = fakes.FakeClient() +mock_request_return = ({}, {'quota_class': {'plans': 50}}) + + +class QuotaClassesTest(base.TestCaseShell): + + @mock.patch('karborclient.common.http.HTTPClient.json_request') + def test_quota_class_update(self, mock_request): + mock_request.return_value = mock_request_return + cs.quota_classes.update('default', {'plans': 50}) + mock_request.assert_called_with( + 'PUT', + '/quota_classes/default', + data={'quota_class': {'plans': 50}}, headers={}) + + @mock.patch('karborclient.common.http.HTTPClient.json_request') + def test_show_quota_class(self, mock_request): + mock_request.return_value = mock_request_return + cs.quota_classes.get('default') + mock_request.assert_called_with( + 'GET', + '/quota_classes/default', + headers={}) diff --git a/karborclient/v1/client.py b/karborclient/v1/client.py index e3cf0f3..6dac3c6 100644 --- a/karborclient/v1/client.py +++ b/karborclient/v1/client.py @@ -18,6 +18,7 @@ from karborclient.v1 import operation_logs from karborclient.v1 import plans from karborclient.v1 import protectables from karborclient.v1 import providers +from karborclient.v1 import quota_classes from karborclient.v1 import quotas from karborclient.v1 import restores from karborclient.v1 import scheduled_operations @@ -52,3 +53,4 @@ class Client(object): self.http_client) self.services = services.ServiceManager(self.http_client) self.quotas = quotas.QuotaManager(self.http_client) + self.quota_classes = quota_classes.QuotaClassManager(self.http_client) diff --git a/karborclient/v1/quota_classes.py b/karborclient/v1/quota_classes.py new file mode 100644 index 0000000..1fffa2e --- /dev/null +++ b/karborclient/v1/quota_classes.py @@ -0,0 +1,42 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from karborclient.common import base + + +class QuotaClass(base.Resource): + def __repr__(self): + return "" % self._info + + +class QuotaClassManager(base.ManagerWithFind): + resource_class = QuotaClass + + def list(self): + pass + + def update(self, class_name, data): + + body = {"quota_class": data} + + return self._update('/quota_classes/{class_name}' + .format(class_name=class_name), + body, "quota_class") + + def get(self, class_name, session_id=None): + if session_id: + headers = {'X-Configuration-Session': session_id} + else: + headers = {} + url = "/quota_classes/{class_name}".format( + class_name=class_name) + return self._get(url, response_key="quota_class", headers=headers) diff --git a/karborclient/v1/shell.py b/karborclient/v1/shell.py index a788314..f5ad330 100644 --- a/karborclient/v1/shell.py +++ b/karborclient/v1/shell.py @@ -1354,3 +1354,33 @@ def do_quota_defaults(cs, args): result = cs.quotas.defaults(project_id) _quota_set_pretty_show(result) + + +@utils.arg( + 'class_name', + metavar='', + help='Name of quota class to list the quotas for.') +def do_quota_class_show(cs, args): + """List the quotas for a quota class.""" + result = cs.quota_classes.get(args.class_name) + _quota_set_pretty_show(result) + + +@utils.arg( + 'class_name', + metavar='', + help='Name of quota class to set the quotas for.') +@utils.arg( + '--plans', + metavar='', + type=int, + default=None, + help='New value for the "plans" quota.') +def do_quota_class_update(cs, args): + """Update the quotas for a quota class (Admin only).""" + class_name = args.class_name + data = { + "plans": args.plans, + } + result = cs.quota_classes.update(class_name, data) + _quota_set_pretty_show(result)