Merge "Switch quota methods of cloud layer to proxy" into feature/r1

This commit is contained in:
Zuul 2021-12-23 17:33:04 +00:00 committed by Gerrit Code Review
commit d7d1a2fac6
4 changed files with 52 additions and 40 deletions

View File

@ -27,6 +27,7 @@ from openstack.cloud import _normalize
from openstack.cloud import _utils
from openstack.cloud import exc
from openstack.cloud import meta
from openstack.compute.v2 import quota_set as _qs
from openstack.compute.v2 import server as _server
from openstack import exceptions
from openstack import proxy
@ -1593,25 +1594,13 @@ class ComputeCloudMixin(_normalize.Normalizer):
:raises: OpenStackCloudException if the resource to set the
quota does not exist.
"""
proj = self.get_project(name_or_id)
if not proj:
raise exc.OpenStackCloudException("project does not exist")
# compute_quotas = {key: val for key, val in kwargs.items()
# if key in quota.COMPUTE_QUOTAS}
# TODO(ghe): Manage volume and network quotas
# network_quotas = {key: val for key, val in kwargs.items()
# if key in quota.NETWORK_QUOTAS}
# volume_quotas = {key: val for key, val in kwargs.items()
# if key in quota.VOLUME_QUOTAS}
proj = self.identity.find_project(
name_or_id, ignore_missing=False)
kwargs['force'] = True
proxy._json_response(
self.compute.put(
'/os-quota-sets/{project}'.format(project=proj.id),
json={'quota_set': kwargs}),
error_message="No valid quota or resource")
self.compute.update_quota_set(
_qs.QuotaSet(project_id=proj.id),
**kwargs
)
def get_compute_quotas(self, name_or_id):
""" Get quota for a project
@ -1621,13 +1610,9 @@ class ComputeCloudMixin(_normalize.Normalizer):
:returns: Munch object with the quotas
"""
proj = self.get_project(name_or_id)
if not proj:
raise exc.OpenStackCloudException("project does not exist")
data = proxy._json_response(
self.compute.get(
'/os-quota-sets/{project}'.format(project=proj.id)))
return self._get_and_munchify('quota_set', data)
proj = self.identity.find_project(
name_or_id, ignore_missing=False)
return self.compute.get_quota_set(proj)
def delete_compute_quotas(self, name_or_id):
""" Delete quota for a project
@ -1638,12 +1623,9 @@ class ComputeCloudMixin(_normalize.Normalizer):
:returns: dict with the quotas
"""
proj = self.get_project(name_or_id)
if not proj:
raise exc.OpenStackCloudException("project does not exist")
return proxy._json_response(
self.compute.delete(
'/os-quota-sets/{project}'.format(project=proj.id)))
proj = self.identity.find_project(
name_or_id, ignore_missing=False)
return self.compute.revert_quota_set(proj)
def get_compute_usage(self, name_or_id, start=None, end=None):
""" Get usage for a specific project

View File

@ -1954,6 +1954,8 @@ class Proxy(proxy.Proxy):
project = self._get_resource(_project.Project, project)
res = self._get_resource(
_quota_set.QuotaSet, None, project_id=project.id)
if not query:
query = {}
return res.fetch(
self, usage=usage, **query)
@ -1988,6 +1990,8 @@ class Proxy(proxy.Proxy):
res = self._get_resource(
_quota_set.QuotaSet, None, project_id=project.id)
if not query:
query = {}
return res.delete(self, **query)
def update_quota_set(self, quota_set, query=None, **attrs):
@ -2003,6 +2007,8 @@ class Proxy(proxy.Proxy):
:rtype: :class:`~openstack.compute.v2.quota_set.QuotaSet`
"""
res = self._get_resource(_quota_set.QuotaSet, quota_set, **attrs)
if not query:
query = {}
return res.commit(self, **query)
# ========== Utilities ==========

View File

@ -26,6 +26,9 @@ class QuotaSet(quota_set.QuotaSet):
fixed_ips = resource.Body('fixed_ips', type=int)
#: The number of allowed floating IP addresses for each tenant.
floating_ips = resource.Body('floating_ips', type=int)
#: You can force the update even if the quota has already been used and
#: the reserved quota exceeds the new quota.
force = resource.Body('force', type=bool)
#: The number of allowed bytes of content for each injected file.
injected_file_content_bytes = resource.Body(
'injected_file_content_bytes', type=int)

View File

@ -38,10 +38,15 @@ class TestQuotas(base.TestCase):
cloud_config_fixture=cloud_config_fixture)
def test_update_quotas(self):
project = self.mock_for_keystone_projects(project_count=1,
list_get=True)[0]
project = self._get_project_data()
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'identity', 'public',
append=['v3', 'projects', project.project_id]),
json={'project': project.json_response['project']}),
self.get_nova_discovery_mock_dict(),
dict(method='PUT',
uri=self.get_mock_url(
'compute', 'public',
@ -60,10 +65,15 @@ class TestQuotas(base.TestCase):
self.assert_calls()
def test_update_quotas_bad_request(self):
project = self.mock_for_keystone_projects(project_count=1,
list_get=True)[0]
project = self._get_project_data()
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'identity', 'public',
append=['v3', 'projects', project.project_id]),
json={'project': project.json_response['project']}),
self.get_nova_discovery_mock_dict(),
dict(method='PUT',
uri=self.get_mock_url(
'compute', 'public',
@ -77,13 +87,19 @@ class TestQuotas(base.TestCase):
self.assert_calls()
def test_get_quotas(self):
project = self.mock_for_keystone_projects(project_count=1,
list_get=True)[0]
project = self._get_project_data()
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'identity', 'public',
append=['v3', 'projects', project.project_id]),
json={'project': project.json_response['project']}),
self.get_nova_discovery_mock_dict(),
dict(method='GET',
uri=self.get_mock_url(
'compute', 'public',
append=['os-quota-sets', project.project_id]),
append=['os-quota-sets', project.project_id],
qs_elements=['usage=False']),
json={'quota_set': fake_quota_set}),
])
@ -92,10 +108,15 @@ class TestQuotas(base.TestCase):
self.assert_calls()
def test_delete_quotas(self):
project = self.mock_for_keystone_projects(project_count=1,
list_get=True)[0]
project = self._get_project_data()
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'identity', 'public',
append=['v3', 'projects', project.project_id]),
json={'project': project.json_response['project']}),
self.get_nova_discovery_mock_dict(),
dict(method='DELETE',
uri=self.get_mock_url(
'compute', 'public',