Merge "Add support for limits"

This commit is contained in:
Jenkins 2017-01-10 14:47:16 +00:00 committed by Gerrit Code Review
commit 576bda8d79
6 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,3 @@
---
features:
- Allow to retrieve the limits of a specific project

View File

@ -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 = []

View File

@ -860,3 +860,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()

View File

@ -2141,3 +2141,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)

View 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'))

View 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')