From a4cec6cb18418a5e1d5508aa627f919f6a08ab11 Mon Sep 17 00:00:00 2001 From: chenying Date: Sat, 25 Nov 2017 15:27:26 +0800 Subject: [PATCH] Add OSC commands for quota classes API Change-Id: I9286d53d307d7b7b58340102967fc30bf87252a7 Implements: blueprint support-quotas-in-karbor --- karborclient/osc/v1/quota_classes.py | 75 ++++++++++++++++++ .../tests/unit/osc/v1/test_quota_classes.py | 77 +++++++++++++++++++ setup.cfg | 2 + 3 files changed, 154 insertions(+) create mode 100644 karborclient/osc/v1/quota_classes.py create mode 100644 karborclient/tests/unit/osc/v1/test_quota_classes.py diff --git a/karborclient/osc/v1/quota_classes.py b/karborclient/osc/v1/quota_classes.py new file mode 100644 index 0000000..09ea128 --- /dev/null +++ b/karborclient/osc/v1/quota_classes.py @@ -0,0 +1,75 @@ +# 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. + +"""Data protection V1 quota classes action implementations""" + +from osc_lib.command import command + + +def quota_class_set_pretty_show(quota_classes): + """Convert quotas class object to dict and display.""" + + new_quota_classes = [] + for quota_k, quota_v in sorted(quota_classes.to_dict().items()): + if isinstance(quota_v, dict): + quota_v = '\n'.join( + ['%s = %s' % (k, v) for k, v in sorted(quota_v.items())]) + new_quota_classes.append((quota_k, quota_v)) + + return new_quota_classes + + +class ShowQuotaClasses(command.ShowOne): + _description = "Shows Quota classes." + + def get_parser(self, prog_name): + parser = super(ShowQuotaClasses, self).get_parser(prog_name) + parser.add_argument( + 'class_name', + metavar='', + help='Name of quota class to list the quotas for.') + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.data_protection + result = client.quota_classes.get(parsed_args.class_name) + quota_classes = quota_class_set_pretty_show(result) + return zip(*sorted(quota_classes)) + + +class UpdateQuotaClasses(command.ShowOne): + _description = "Update the quotas for a quota class (Admin only)." + + def get_parser(self, prog_name): + parser = super(UpdateQuotaClasses, self).get_parser(prog_name) + parser.add_argument( + 'class_name', + metavar='', + help='Name of quota class to set the quotas for.') + parser.add_argument( + '--plans', + metavar='', + type=int, + default=None, + help='New value for the "plans" quota.') + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.data_protection + class_name = parsed_args.class_name + data = { + "plans": parsed_args.plans, + } + result = client.quota_classes.update(class_name, data) + quota_classes = quota_class_set_pretty_show(result) + + return zip(*sorted(quota_classes)) diff --git a/karborclient/tests/unit/osc/v1/test_quota_classes.py b/karborclient/tests/unit/osc/v1/test_quota_classes.py new file mode 100644 index 0000000..e944245 --- /dev/null +++ b/karborclient/tests/unit/osc/v1/test_quota_classes.py @@ -0,0 +1,77 @@ +# 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 copy + +from karborclient.osc.v1 import quota_classes as osc_quota_classes +from karborclient.tests.unit.osc.v1 import fakes +from karborclient.v1 import quota_classes + + +QUOTA_CLASSES_INFO = { + "id": "default", + "plans": "40" +} + + +class TestQuotaClasses(fakes.TestDataProtection): + def setUp(self): + super(TestQuotaClasses, self).setUp() + self.quotas_mock = ( + self.app.client_manager.data_protection.quota_classes) + self.quotas_mock.reset_mock() + + +class TestUpdateQuotaClasses(TestQuotaClasses): + def setUp(self): + super(TestUpdateQuotaClasses, self).setUp() + self.quotas_mock.update.return_value = quota_classes.QuotaClass( + None, copy.deepcopy(QUOTA_CLASSES_INFO)) + self.cmd = osc_quota_classes.UpdateQuotaClasses(self.app, None) + + def test_quota_classes_update(self): + arglist = ['--plans', + '40', 'default'] + verifylist = [('plans', 40), + ('class_name', + 'default')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.quotas_mock.update.assert_called_once_with( + 'default', + {'plans': 40}) + + +class TestShowQuotaClasses(TestQuotaClasses): + def setUp(self): + super(TestShowQuotaClasses, self).setUp() + self._quota_info = copy.deepcopy(QUOTA_CLASSES_INFO) + self.quotas_mock.get.return_value = quota_classes.QuotaClass( + None, copy.deepcopy(QUOTA_CLASSES_INFO)) + + self.cmd = osc_quota_classes.ShowQuotaClasses(self.app, None) + + def test_quota_classes_show(self): + arglist = ['default'] + verifylist = [('class_name', 'default')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(('id', 'plans'), columns) + self.assertEqual(('default', '40'), + data) diff --git a/setup.cfg b/setup.cfg index c016899..4bc5d85 100644 --- a/setup.cfg +++ b/setup.cfg @@ -72,6 +72,8 @@ openstack.data_protection.v1 = data_protection_quotas_show = karborclient.osc.v1.quotas:ShowQuotas data_protection_quotas_default = karborclient.osc.v1.quotas:ShowDefaultQuotas data_protection_quotas_update = karborclient.osc.v1.quotas:UpdateQuotas + data_protection_quotaclass_show = karborclient.osc.v1.quota_classes:ShowQuotaClasses + data_protection_quotaclass_update = karborclient.osc.v1.quota_classes:UpdateQuotaClasses [compile_catalog] directory = karborclient/locale