Skip to content

Commit d80d253

Browse files
JohnGarbuttmelwitt
authored andcommitted
Add legacy limits and usage to placement unified limits
Before we can update the API to say what limits are set in keystone we need to logic to pull that information from keystone. blueprint unified-limits-nova Change-Id: I322d5143f3088b04d1d990880b57afc9dc08af6c
1 parent 140b3b8 commit d80d253

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

nova/limit/placement.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
# Cache to avoid repopulating ksa state
3333
PLACEMENT_CLIENT = None
3434

35+
LEGACY_LIMITS = {
36+
"servers": "instances",
37+
"class:VCPU": "cores",
38+
"class:MEMORY_MB": "ram",
39+
}
40+
3541

3642
def _get_placement_usages(context, project_id):
3743
global PLACEMENT_CLIENT
@@ -166,3 +172,30 @@ def enforce_num_instances_and_flavor(context, project_id, flavor, is_bfvm,
166172

167173
# no problems with max_count, so we return max count
168174
return max_count
175+
176+
177+
def _convert_keys_to_legacy_name(new_dict):
178+
legacy = {}
179+
for new_name, old_name in LEGACY_LIMITS.items():
180+
# defensive incase oslo or keystone doesn't give us an answer
181+
legacy[old_name] = new_dict.get(new_name) or 0
182+
return legacy
183+
184+
185+
def get_legacy_default_limits():
186+
enforcer = limit.Enforcer(lambda: None)
187+
new_limits = enforcer.get_registered_limits(LEGACY_LIMITS.keys())
188+
return _convert_keys_to_legacy_name(dict(new_limits))
189+
190+
191+
def get_legacy_project_limits(project_id):
192+
enforcer = limit.Enforcer(lambda: None)
193+
new_limits = enforcer.get_project_limits(project_id, LEGACY_LIMITS.keys())
194+
return _convert_keys_to_legacy_name(dict(new_limits))
195+
196+
197+
def get_legacy_counts(context, project_id):
198+
resource_names = list(LEGACY_LIMITS.keys())
199+
resource_names.sort()
200+
new_usage = _get_usage(context, project_id, resource_names)
201+
return _convert_keys_to_legacy_name(new_usage)

nova/tests/unit/limit/test_placement.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from oslo_config import cfg
1818
from oslo_limit import exception as limit_exceptions
19+
from oslo_limit import fixture as limit_fixture
1920
from oslo_limit import limit
2021
from oslo_utils.fixture import uuidsentinel as uuids
2122

@@ -309,3 +310,44 @@ def test_enforce_num_instances_and_flavor_placement_fail(self, mock_limit):
309310

310311
expected = str(mock_enforcer.enforce.side_effect)
311312
self.assertEqual(expected, str(e))
313+
314+
315+
class GetLegacyLimitsTest(test.NoDBTestCase):
316+
def setUp(self):
317+
super(GetLegacyLimitsTest, self).setUp()
318+
self.new = {"servers": 1, "class:VCPU": 2, "class:MEMORY_MB": 3}
319+
self.legacy = {"instances": 1, "cores": 2, "ram": 3}
320+
self.resources = ["servers", "class:VCPU", "class:MEMORY_MB"]
321+
self.resources.sort()
322+
self.flags(driver=limit_utils.UNIFIED_LIMITS_DRIVER, group="quota")
323+
324+
def test_convert_keys_to_legacy_name(self):
325+
limits = placement_limits._convert_keys_to_legacy_name(self.new)
326+
self.assertEqual(self.legacy, limits)
327+
328+
def test_get_legacy_default_limits(self):
329+
reglimits = {'servers': 1, 'class:VCPU': 2}
330+
self.useFixture(limit_fixture.LimitFixture(reglimits, {}))
331+
limits = placement_limits.get_legacy_default_limits()
332+
self.assertEqual({'cores': 2, 'instances': 1, 'ram': 0}, limits)
333+
334+
def test_get_legacy_project_limits(self):
335+
reglimits = {'servers': 5, 'class:MEMORY_MB': 7}
336+
projlimits = {uuids.project_id: {'servers': 1}}
337+
self.useFixture(limit_fixture.LimitFixture(reglimits, projlimits))
338+
limits = placement_limits.get_legacy_project_limits(uuids.project_id)
339+
self.assertEqual({'instances': 1, 'cores': 0, 'ram': 7}, limits)
340+
341+
@mock.patch.object(report.SchedulerReportClient,
342+
"get_usages_counts_for_limits")
343+
@mock.patch.object(objects.InstanceMappingList, "get_counts")
344+
@mock.patch.object(quota, "is_qfd_populated")
345+
def test_get_legacy_counts(self, mock_qfd, mock_counts, mock_placement):
346+
mock_qfd.return_value = True
347+
mock_counts.return_value = {"project": {"instances": 1}}
348+
mock_placement.return_value = {
349+
"VCPU": 2, "CUSTOM_BAREMETAL": 2, "MEMORY_MB": 3,
350+
}
351+
counts = placement_limits.get_legacy_counts(
352+
"context", uuids.project_id)
353+
self.assertEqual(self.legacy, counts)

0 commit comments

Comments
 (0)