Limit allocation candidates (v1.15, v1.16)

v1.15:
Microversion 1.15 does not need any support in osc
so we just skip over that in this patch.

v1.16:
New optional parameter to allocation candidate list:
    --limit N

Change-Id: Ic23dc4274c6fe42bfdf97fec55ddf0c36462501c
Partially-Implements: blueprint placement-osc-plugin-rocky
This commit is contained in:
Bence Romsics 2018-02-26 14:21:11 +01:00 committed by Matt Riedemann
parent 565fb8d8c4
commit 9f4e7eb9e8
5 changed files with 55 additions and 7 deletions

View File

@ -68,6 +68,14 @@ class ListAllocationCandidate(command.Lister, version.CheckerMixin):
'``--resource VCP=4 --resource DISK_GB=64 '
'--resource MEMORY_MB=2048``'
)
parser.add_argument(
'--limit',
metavar='<limit>',
help='A positive integer to limit '
'the maximum number of allocation candidates. '
'This option requires at least '
'``--os-placement-api-version 1.16``.'
)
return parser
@ -81,6 +89,10 @@ class ListAllocationCandidate(command.Lister, version.CheckerMixin):
params = {'resources': ','.join(
resource.replace('=', ':') for resource in parsed_args.resource)}
if 'limit' in parsed_args and parsed_args.limit:
# Fail if --limit but not high enough microversion.
self.check_version(version.ge('1.16'))
params['limit'] = int(parsed_args.limit)
resp = http.request('GET', BASE_URL, params=params).json()
rps = {}

View File

@ -258,7 +258,9 @@ class BaseTestCase(base.BaseTestCase):
cmd = 'resource provider trait delete %s ' % uuid
self.openstack(cmd)
def allocation_candidate_list(self, *resources):
def allocation_candidate_list(self, resources, limit=None):
cmd = 'allocation candidate list ' + ' '.join(
'--resource %s' % resource for resource in resources)
if limit is not None:
cmd += ' --limit %d' % limit
return self.openstack(cmd, use_json=True)

View File

@ -29,12 +29,13 @@ class TestAllocationCandidate(base.BaseTestCase):
def test_list_empty(self):
self.assertEqual([], self.allocation_candidate_list(
'MEMORY_MB=999999999'))
resources=['MEMORY_MB=999999999']))
def test_list_one(self):
rp = self.resource_provider_create()
self.resource_inventory_set(rp['uuid'], 'MEMORY_MB=1024')
candidates = self.allocation_candidate_list('MEMORY_MB=256')
candidates = self.allocation_candidate_list(
resources=('MEMORY_MB=256',))
self.assertIn(
rp['uuid'],
[candidate['resource provider'] for candidate in candidates])
@ -50,7 +51,7 @@ class TestAllocationCandidate(base.BaseTestCase):
self.resource_inventory_set(
rp2['uuid'], 'MEMORY_MB=16384', 'DISK_GB=1024')
candidates = self.allocation_candidate_list(
'MEMORY_MB=1024', 'DISK_GB=80')
resources=('MEMORY_MB=1024', 'DISK_GB=80'))
rps = {c['resource provider']: c for c in candidates}
self.assertResourceEqual(
'MEMORY_MB=1024,DISK_GB=80', rps[rp1['uuid']]['allocation'])
@ -74,7 +75,7 @@ class TestAllocationCandidate(base.BaseTestCase):
self.resource_provider_trait_set(
rp2['uuid'], 'MISC_SHARES_VIA_AGGREGATE')
candidates = self.allocation_candidate_list(
'MEMORY_MB=1024', 'DISK_GB=80')
resources=('MEMORY_MB=1024', 'DISK_GB=80'))
rps = {c['resource provider']: c for c in candidates}
self.assertResourceEqual(
'MEMORY_MB=1024', rps[rp1['uuid']]['allocation'])
@ -89,8 +90,31 @@ class TestAllocationCandidate(base.BaseTestCase):
def test_fail_if_unknown_rc(self):
self.assertCommandFailed(
'No such resource', self.allocation_candidate_list, 'UNKNOWN=10')
'No such resource',
self.allocation_candidate_list,
resources=('UNKNOWN=10',))
class TestAllocationCandidate112(TestAllocationCandidate):
VERSION = '1.12'
class TestAllocationCandidate116(base.BaseTestCase):
VERSION = '1.16'
def test_list_limit(self):
rp1 = self.resource_provider_create()
rp2 = self.resource_provider_create()
self.resource_inventory_set(
rp1['uuid'], 'MEMORY_MB=8192', 'DISK_GB=512')
self.resource_inventory_set(
rp2['uuid'], 'MEMORY_MB=8192', 'DISK_GB=512')
unlimited = self.allocation_candidate_list(
resources=('MEMORY_MB=1024', 'DISK_GB=80'))
self.assertTrue(len(set([row['#'] for row in unlimited])) > 1)
limited = self.allocation_candidate_list(
resources=('MEMORY_MB=1024', 'DISK_GB=80'),
limit=1)
self.assertEqual(len(set([row['#'] for row in limited])), 1)

View File

@ -29,7 +29,9 @@ SUPPORTED_VERSIONS = [
'1.11',
'1.12',
'1.13', # unused
'1.14'
'1.14',
'1.15', # unused
'1.16'
]

View File

@ -0,0 +1,8 @@
---
features:
- |
Support is added for the `1.16`_ placement API microversion by adding
the ``--limit`` option to the ``openstack allocation candidate list``
command.
.. _1.16: https://docs.openstack.org/nova/latest/user/placement.html#limit-allocation-candidates