Add support for limits
Allow to get the limits of your project/tenant. Change-Id: I115b2fad354a4319aece33a13b4dfdc204e4c5fb
This commit is contained in:
parent
abbf0c8d1b
commit
d8b26c3f59
3
releasenotes/notes/get-limits-c383c512f8e01873.yaml
Normal file
3
releasenotes/notes/get-limits-c383c512f8e01873.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
---
|
||||
features:
|
||||
- Allow to retrieve the limits of a specific project
|
@ -106,6 +106,17 @@ class Normalizer(object):
|
||||
reasons.
|
||||
'''
|
||||
|
||||
def _normalize_limits(self, limits):
|
||||
""" Normalize a limits object.
|
||||
|
||||
Limits modified in this method and shouldn't be modified afterwards.
|
||||
"""
|
||||
|
||||
new_limits = munch.Munch(limits['absolute'])
|
||||
new_limits['rate'] = limits.pop('rate')
|
||||
|
||||
return new_limits
|
||||
|
||||
def _normalize_flavors(self, flavors):
|
||||
""" Normalize a list of flavor objects """
|
||||
ret = []
|
||||
|
@ -945,3 +945,8 @@ class ClusterTemplateUpdate(task_manager.Task):
|
||||
class MagnumServicesList(task_manager.Task):
|
||||
def main(self, client):
|
||||
return client.magnum_client.mservices.list(detail=False)
|
||||
|
||||
|
||||
class NovaLimitsGet(task_manager.Task):
|
||||
def main(self, client):
|
||||
return client.nova_client.limits.get(**self.args).to_dict()
|
||||
|
@ -2138,3 +2138,22 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
|
||||
with _utils.shade_exceptions("Error fetching Magnum services list"):
|
||||
return self.manager.submit_task(
|
||||
_tasks.MagnumServicesList())
|
||||
|
||||
def get_limits(self, name_or_id):
|
||||
""" Get limits for a project
|
||||
|
||||
:param name_or_id: project name or id
|
||||
:raises: OpenStackCloudException if it's not a valid project
|
||||
|
||||
:returns: Munch object with the limits
|
||||
"""
|
||||
proj = self.get_project(name_or_id)
|
||||
if not proj.id:
|
||||
raise OpenStackCloudException("project does not exist")
|
||||
|
||||
with _utils.shade_exceptions(
|
||||
"Failed to get limits for the project: {} ".format(proj.id)):
|
||||
limits = self.manager.submit_task(
|
||||
_tasks.NovaLimitsGet(tenant_id=proj.id))
|
||||
|
||||
return self._normalize_limits(limits)
|
||||
|
33
shade/tests/functional/test_limits.py
Normal file
33
shade/tests/functional/test_limits.py
Normal file
@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# 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.
|
||||
|
||||
"""
|
||||
test_limits
|
||||
----------------------------------
|
||||
|
||||
Functional tests for `shade` limits method
|
||||
"""
|
||||
from shade.tests.functional import base
|
||||
|
||||
|
||||
class TestUsage(base.BaseFunctionalTestCase):
|
||||
|
||||
def test_get_limits(self):
|
||||
'''Test quotas functionality'''
|
||||
limits = self.operator_cloud.get_limits('demo')
|
||||
self.assertIsNotNone(limits)
|
||||
self.assertTrue(hasattr(limits, 'rate'))
|
||||
|
||||
# Test normalize limits
|
||||
self.assertFalse(hasattr(limits, 'HUMAN_ID'))
|
30
shade/tests/unit/test_limits.py
Normal file
30
shade/tests/unit/test_limits.py
Normal file
@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# 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 mock
|
||||
|
||||
import shade
|
||||
from shade.tests.unit import base
|
||||
from shade.tests import fakes
|
||||
|
||||
|
||||
class TestLimits(base.TestCase):
|
||||
|
||||
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
|
||||
@mock.patch.object(shade.OpenStackCloud, 'keystone_client')
|
||||
def test_get_limits(self, mock_keystone, mock_nova):
|
||||
project = fakes.FakeProject('project_a')
|
||||
mock_keystone.tenants.list.return_value = [project]
|
||||
self.op_cloud.get_limits(project)
|
||||
|
||||
mock_nova.limits.get.assert_called_once_with(tenant_id='project_a')
|
Loading…
x
Reference in New Issue
Block a user