Skip to content

Commit b328cf7

Browse files
author
hackertron
committed
Add '--force; parameter to 'openstack quota set'
The compute service allows us to to force set a quota, setting a quota value that is less than the amount of the resource currently consumed. Expose this feature by way of a '--force' boolean parameter. Change-Id: I1d1ac1ac46f49f64794ffc8631e166935537966c
1 parent 8c4ecbe commit b328cf7

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

openstackclient/common/quota.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ def get_parser(self, prog_name):
516516
metavar='<volume-type>',
517517
help=_('Set quotas for a specific <volume-type>'),
518518
)
519+
parser.add_argument(
520+
'--force',
521+
action='store_true',
522+
help=_('Force quota update (only supported by compute)')
523+
)
519524
return parser
520525

521526
def take_action(self, parsed_args):
@@ -529,6 +534,9 @@ def take_action(self, parsed_args):
529534
if value is not None:
530535
compute_kwargs[k] = value
531536

537+
if parsed_args.force:
538+
compute_kwargs['force'] = True
539+
532540
volume_kwargs = {}
533541
for k, v in VOLUME_QUOTAS.items():
534542
value = getattr(parsed_args, k, None)

openstackclient/tests/functional/common/test_quota.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,47 @@ def test_quota_set_class(self):
165165
# returned attributes
166166
self.assertTrue(cmd_output["key-pairs"] >= 0)
167167
self.assertTrue(cmd_output["snapshots"] >= 0)
168+
169+
def test_quota_set_force(self):
170+
"""Test to set instance value by force """
171+
json_output = json.loads(self.openstack(
172+
'quota list -f json --detail --compute'
173+
))
174+
in_use = limit = None
175+
for j in json_output:
176+
if j["Resource"] == "instances":
177+
in_use = j["In Use"]
178+
limit = j["Limit"]
179+
180+
# Reduce count of in_use
181+
in_use = in_use - 1
182+
# cannot have negative instances limit
183+
if in_use < 0:
184+
in_use = 0
185+
186+
# set the limit by force now
187+
self.openstack(
188+
'quota set ' + self.PROJECT_NAME +
189+
'--instances ' + str(in_use) + ' --force'
190+
)
191+
cmd_output = json.loads(self.openstack(
192+
'quota show -f json ' + self.PROJECT_NAME
193+
))
194+
self.assertIsNotNone(cmd_output)
195+
self.assertEqual(
196+
in_use,
197+
cmd_output["instances"]
198+
)
199+
200+
# Set instances limit to original limit now
201+
self.openstack(
202+
'quota set ' + self.PROJECT_NAME + '--instances ' + str(limit)
203+
)
204+
cmd_output = json.loads(self.openstack(
205+
'quota show -f json ' + self.PROJECT_NAME
206+
))
207+
self.assertIsNotNone(cmd_output)
208+
self.assertEqual(
209+
limit,
210+
cmd_output["instances"]
211+
)

openstackclient/tests/unit/common/test_quota.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,56 @@ def test_quota_set_with_class(self):
831831
self.assertNotCalled(self.network_mock.update_quota)
832832
self.assertIsNone(result)
833833

834+
def test_quota_set_with_force(self):
835+
arglist = [
836+
'--cores', str(compute_fakes.core_num),
837+
'--ram', str(compute_fakes.ram_num),
838+
'--instances', str(compute_fakes.instance_num),
839+
'--volumes', str(volume_fakes.QUOTA['volumes']),
840+
'--subnets', str(network_fakes.QUOTA['subnet']),
841+
'--force',
842+
self.projects[0].name,
843+
]
844+
verifylist = [
845+
('cores', compute_fakes.core_num),
846+
('ram', compute_fakes.ram_num),
847+
('instances', compute_fakes.instance_num),
848+
('volumes', volume_fakes.QUOTA['volumes']),
849+
('subnet', network_fakes.QUOTA['subnet']),
850+
('force', True),
851+
('project', self.projects[0].name),
852+
]
853+
self.app.client_manager.network_endpoint_enabled = True
854+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
855+
856+
result = self.cmd.take_action(parsed_args)
857+
858+
kwargs_compute = {
859+
'cores': compute_fakes.core_num,
860+
'ram': compute_fakes.ram_num,
861+
'instances': compute_fakes.instance_num,
862+
'force': True,
863+
}
864+
kwargs_volume = {
865+
'volumes': volume_fakes.QUOTA['volumes'],
866+
}
867+
kwargs_network = {
868+
'subnet': network_fakes.QUOTA['subnet'],
869+
}
870+
self.compute_quotas_mock.update.assert_called_once_with(
871+
self.projects[0].id,
872+
**kwargs_compute
873+
)
874+
self.volume_quotas_mock.update.assert_called_once_with(
875+
self.projects[0].id,
876+
**kwargs_volume
877+
)
878+
self.network_mock.update_quota.assert_called_once_with(
879+
self.projects[0].id,
880+
**kwargs_network
881+
)
882+
self.assertIsNone(result)
883+
834884

835885
class TestQuotaShow(TestQuota):
836886

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- Add ``--force`` options to the ``openstack quota set``
4+
command. The compute service allows us to to force set a quota, setting a
5+
quota value that is less than the amount of the resource currently
6+
consumed. Expose this feature by way of a ``--force`` boolean parameter.

0 commit comments

Comments
 (0)