From 8d791ec69e969d5fbb3ac7848e59e54f05f36e42 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Fri, 31 May 2019 15:50:51 -0700 Subject: [PATCH] Adds "unset" action to the quota command This patch adds "openstack loadbalancer quota unset" action to the octavia client. This allows the user to reset the quota to the default value. Change-Id: If95cc0c91df4c6831d91995ca0cf849986be951e --- octaviaclient/osc/v2/quota.py | 55 +++++++++++++++ octaviaclient/tests/unit/osc/v2/test_quota.py | 67 +++++++++++++++++++ .../Add-unset-commands-15490df414a70df0.yaml | 6 ++ setup.cfg | 1 + 4 files changed, 129 insertions(+) create mode 100644 releasenotes/notes/Add-unset-commands-15490df414a70df0.yaml diff --git a/octaviaclient/osc/v2/quota.py b/octaviaclient/osc/v2/quota.py index 20253a1..092e5c7 100644 --- a/octaviaclient/osc/v2/quota.py +++ b/octaviaclient/osc/v2/quota.py @@ -1,3 +1,5 @@ +# Copyright 2019 Red Hat, Inc. All rights reserved. +# # 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 @@ -183,3 +185,56 @@ class ResetQuota(command.Command): self.app.client_manager.load_balancer.quota_reset( project_id=project_id) + + +class UnsetQuota(command.Command): + """Clear quota settings""" + + def get_parser(self, prog_name): + parser = super(UnsetQuota, self).get_parser(prog_name) + + parser.add_argument( + 'project', + metavar='', + help="Name or UUID of the project." + ) + parser.add_argument( + '--loadbalancer', + action='store_true', + help="Reset the load balancer quota to the default." + ) + parser.add_argument( + '--listener', + action='store_true', + help="Reset the listener quota to the default." + ) + parser.add_argument( + '--pool', + action='store_true', + help="Reset the pool quota to the default." + ) + parser.add_argument( + '--member', + action='store_true', + help="Reset the member quota to the default." + ) + parser.add_argument( + '--healthmonitor', + action='store_true', + help="Reset the health monitor quota to the default." + ) + return parser + + def take_action(self, parsed_args): + unset_args = v2_utils.get_unsets(parsed_args) + if not len(unset_args): + return + + project_id = v2_utils.get_resource_id( + self.app.client_manager.identity, + 'project', parsed_args.project) + + body = {'quota': unset_args} + + self.app.client_manager.load_balancer.quota_set( + project_id, json=body) diff --git a/octaviaclient/tests/unit/osc/v2/test_quota.py b/octaviaclient/tests/unit/osc/v2/test_quota.py index 10152f1..fcfa64f 100644 --- a/octaviaclient/tests/unit/osc/v2/test_quota.py +++ b/octaviaclient/tests/unit/osc/v2/test_quota.py @@ -198,3 +198,70 @@ class TestQuotaReset(TestQuota): self.cmd.take_action(parsed_args) self.api_mock.quota_reset.assert_called_with( project_id=qt_reset.project_id) + + +class TestQuotaUnset(TestQuota): + PARAMETERS = ('loadbalancer', 'listener', 'pool', + 'member', 'healthmonitor') + + def setUp(self): + super(TestQuotaUnset, self).setUp() + self.cmd = quota.UnsetQuota(self.app, None) + + def test_quota_unset_loadbalancer(self): + self._test_quota_unset_param('loadbalancer') + + def test_quota_unset_listener(self): + self._test_quota_unset_param('listener') + + def test_quota_unset_pool(self): + self._test_quota_unset_param('pool') + + def test_quota_unset_health_monitor(self): + self._test_quota_unset_param('healthmonitor') + + def test_quota_unset_member(self): + self._test_quota_unset_param('member') + + @mock.patch('octaviaclient.osc.v2.utils.get_resource_id') + def _test_quota_unset_param(self, param, mock_get_resource): + self.api_mock.quota_set.reset_mock() + mock_get_resource.return_value = self._qt.project_id + arg_param = param.replace('_', '-') if '_' in param else param + arglist = [self._qt.project_id, '--%s' % arg_param] + ref_body = {'quota': {param: None}} + verifylist = [ + ('project', self._qt.project_id), + ] + for ref_param in self.PARAMETERS: + verifylist.append((ref_param, param == ref_param)) + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self.api_mock.quota_set.assert_called_once_with( + self._qt.project_id, json=ref_body) + + @mock.patch('octaviaclient.osc.v2.utils.get_resource_id') + def test_quota_unset_all(self, mock_get_resource): + self.api_mock.quota_set.reset_mock() + mock_get_resource.return_value = self._qt.project_id + ref_body = {'quota': {x: None for x in self.PARAMETERS}} + arglist = [self._qt.project_id] + for ref_param in self.PARAMETERS: + arg_param = (ref_param.replace('_', '-') if '_' in ref_param else + ref_param) + arglist.append('--%s' % arg_param) + verifylist = list(zip(self.PARAMETERS, [True]*len(self.PARAMETERS))) + verifylist = [('project', self._qt.project_id)] + verifylist + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self.api_mock.quota_set.assert_called_once_with( + self._qt.project_id, json=ref_body) + + def test_quota_unset_none(self): + self.api_mock.quota_set.reset_mock() + arglist = [self._qt.project_id] + verifylist = list(zip(self.PARAMETERS, [False]*len(self.PARAMETERS))) + verifylist = [('project', self._qt.project_id)] + verifylist + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self.api_mock.quota_set.assert_not_called() diff --git a/releasenotes/notes/Add-unset-commands-15490df414a70df0.yaml b/releasenotes/notes/Add-unset-commands-15490df414a70df0.yaml new file mode 100644 index 0000000..b07c70d --- /dev/null +++ b/releasenotes/notes/Add-unset-commands-15490df414a70df0.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + The loadbalancer commands now have unset actions. These will either clear + the field back to None or it will reset the value to the API default + value. diff --git a/setup.cfg b/setup.cfg index 078982a..aed1e10 100644 --- a/setup.cfg +++ b/setup.cfg @@ -79,6 +79,7 @@ openstack.load_balancer.v2 = loadbalancer_quota_defaults_show = octaviaclient.osc.v2.quota:ShowQuotaDefaults loadbalancer_quota_reset = octaviaclient.osc.v2.quota:ResetQuota loadbalancer_quota_set = octaviaclient.osc.v2.quota:SetQuota + loadbalancer_quota_unset = octaviaclient.osc.v2.quota:UnsetQuota loadbalancer_amphora_list = octaviaclient.osc.v2.amphora:ListAmphora loadbalancer_amphora_show = octaviaclient.osc.v2.amphora:ShowAmphora loadbalancer_amphora_configure = octaviaclient.osc.v2.amphora:ConfigureAmphora