From 9f4e7eb9e82de3bd5778802e11fa15911c79b7bb Mon Sep 17 00:00:00 2001 From: Bence Romsics Date: Mon, 26 Feb 2018 14:21:11 +0100 Subject: [PATCH] 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 --- .../resources/allocation_candidate.py | 12 +++++++ osc_placement/tests/functional/base.py | 4 ++- .../functional/test_allocation_candidate.py | 34 ++++++++++++++++--- osc_placement/version.py | 4 ++- ...loc-candidates-limit-8310675ecc99a82a.yaml | 8 +++++ 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 releasenotes/notes/microversion-1.16-alloc-candidates-limit-8310675ecc99a82a.yaml diff --git a/osc_placement/resources/allocation_candidate.py b/osc_placement/resources/allocation_candidate.py index ec8c398..50da6ac 100644 --- a/osc_placement/resources/allocation_candidate.py +++ b/osc_placement/resources/allocation_candidate.py @@ -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='', + 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 = {} diff --git a/osc_placement/tests/functional/base.py b/osc_placement/tests/functional/base.py index a6309ed..79b7c77 100644 --- a/osc_placement/tests/functional/base.py +++ b/osc_placement/tests/functional/base.py @@ -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) diff --git a/osc_placement/tests/functional/test_allocation_candidate.py b/osc_placement/tests/functional/test_allocation_candidate.py index fa16f83..d4ede2b 100644 --- a/osc_placement/tests/functional/test_allocation_candidate.py +++ b/osc_placement/tests/functional/test_allocation_candidate.py @@ -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) diff --git a/osc_placement/version.py b/osc_placement/version.py index c3f0179..b214495 100644 --- a/osc_placement/version.py +++ b/osc_placement/version.py @@ -29,7 +29,9 @@ SUPPORTED_VERSIONS = [ '1.11', '1.12', '1.13', # unused - '1.14' + '1.14', + '1.15', # unused + '1.16' ] diff --git a/releasenotes/notes/microversion-1.16-alloc-candidates-limit-8310675ecc99a82a.yaml b/releasenotes/notes/microversion-1.16-alloc-candidates-limit-8310675ecc99a82a.yaml new file mode 100644 index 0000000..60b2778 --- /dev/null +++ b/releasenotes/notes/microversion-1.16-alloc-candidates-limit-8310675ecc99a82a.yaml @@ -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