Add project-name/-id validation for the OSC "openstack quota set"

The quota info would be set into DB, even though the project actually
does not exist.
This patch tried to add a validation to forbid this undesirable behavior.

Change-Id: Ia2d8c96527820e25b074e6486d3f39c5ad7eae60
Closes-Bug: #1512638
This commit is contained in:
xiexs 2015-11-04 10:22:39 -05:00
parent 266ecf57f5
commit 7d8bb331a0
2 changed files with 31 additions and 10 deletions

View File

@ -94,6 +94,7 @@ class SetQuota(command.Command):
@utils.log_method(log)
def take_action(self, parsed_args):
identity_client = self.app.client_manager.identity
compute_client = self.app.client_manager.compute
volume_client = self.app.client_manager.volume
@ -115,23 +116,29 @@ class SetQuota(command.Command):
sys.stderr.write("No quotas updated")
return
if parsed_args.project:
project = utils.find_resource(
identity_client.projects,
parsed_args.project,
)
if parsed_args.quota_class:
if compute_kwargs:
compute_client.quota_classes.update(
parsed_args.project,
project.id,
**compute_kwargs)
if volume_kwargs:
volume_client.quota_classes.update(
parsed_args.project,
project.id,
**volume_kwargs)
else:
if compute_kwargs:
compute_client.quotas.update(
parsed_args.project,
project.id,
**compute_kwargs)
if volume_kwargs:
volume_client.quotas.update(
parsed_args.project,
project.id,
**volume_kwargs)

View File

@ -17,6 +17,7 @@ import mock
from openstackclient.common import quota
from openstackclient.tests.compute.v2 import fakes as compute_fakes
from openstackclient.tests import fakes
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
class FakeQuotaResource(fakes.FakeResource):
@ -45,6 +46,8 @@ class TestQuota(compute_fakes.TestComputev2):
self.app.client_manager.volume = volume_mock
self.volume_quotas_mock = volume_mock.quotas
self.volume_quotas_mock.reset_mock()
self.projects_mock = self.app.client_manager.identity.projects
self.projects_mock.reset_mock()
class TestQuotaSet(TestQuota):
@ -76,6 +79,12 @@ class TestQuotaSet(TestQuota):
loaded=True,
)
self.projects_mock.get.return_value = fakes.FakeResource(
None,
copy.deepcopy(identity_fakes.PROJECT),
loaded=True,
)
self.cmd = quota.SetQuota(self.app, None)
def test_quota_set(self):
@ -84,14 +93,14 @@ class TestQuotaSet(TestQuota):
'--fixed-ips', str(compute_fakes.fix_ip_num),
'--injected-files', str(compute_fakes.injected_file_num),
'--key-pairs', str(compute_fakes.key_pair_num),
compute_fakes.project_name,
identity_fakes.project_name,
]
verifylist = [
('floating_ips', compute_fakes.floating_ip_num),
('fixed_ips', compute_fakes.fix_ip_num),
('injected_files', compute_fakes.injected_file_num),
('key_pairs', compute_fakes.key_pair_num),
('project', compute_fakes.project_name),
('project', identity_fakes.project_name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -105,14 +114,17 @@ class TestQuotaSet(TestQuota):
'key_pairs': compute_fakes.key_pair_num,
}
self.quotas_mock.update.assert_called_with('project_test', **kwargs)
self.quotas_mock.update.assert_called_with(
identity_fakes.project_id,
**kwargs
)
def test_quota_set_volume(self):
arglist = [
'--gigabytes', str(compute_fakes.floating_ip_num),
'--snapshots', str(compute_fakes.fix_ip_num),
'--volumes', str(compute_fakes.injected_file_num),
compute_fakes.project_name,
identity_fakes.project_name,
]
verifylist = [
('gigabytes', compute_fakes.floating_ip_num),
@ -130,5 +142,7 @@ class TestQuotaSet(TestQuota):
'volumes': compute_fakes.injected_file_num,
}
self.volume_quotas_mock.update.assert_called_with('project_test',
**kwargs)
self.volume_quotas_mock.update.assert_called_with(
identity_fakes.project_id,
**kwargs
)