From 53c58dfcc1368fb575086846b65100f51dca2a84 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 21 Mar 2025 10:57:00 +0000 Subject: [PATCH] api: Add schema for allocations API (versioning) The only thing to watch for is that GET calls fail with HTTP 404 (Not Found) before this microversion, while POST, PATCH and DELETE calls fail with HTTP 405 (Method Not Allowed). Also, PATCH was introduced in a different, later microversion to the others. Change-Id: Ie9a6f2d282bfc97a27e868cb66f56f840b9c6a0d Signed-off-by: Stephen Finucane --- ironic/api/controllers/v1/allocation.py | 39 ++++++++++++++++--------- ironic/api/controllers/v1/utils.py | 8 ----- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/ironic/api/controllers/v1/allocation.py b/ironic/api/controllers/v1/allocation.py index ac42da7cd5..e1c9fc2452 100644 --- a/ironic/api/controllers/v1/allocation.py +++ b/ironic/api/controllers/v1/allocation.py @@ -22,7 +22,9 @@ from ironic.api.controllers import link from ironic.api.controllers.v1 import collection from ironic.api.controllers.v1 import notification_utils as notify from ironic.api.controllers.v1 import utils as api_utils +from ironic.api.controllers.v1 import versions from ironic.api import method +from ironic.api import validation from ironic.common import args from ironic.common import exception from ironic.common.i18n import _ @@ -123,16 +125,6 @@ class AllocationsController(pecan.rest.RestController): invalid_sort_key_list = ['extra', 'candidate_nodes', 'traits'] - @pecan.expose() - def _route(self, args, request=None): - if not api_utils.allow_allocations(): - msg = _("The API version does not allow allocations") - if api.request.method == "GET": - raise webob_exc.HTTPNotFound(msg) - else: - raise webob_exc.HTTPMethodNotAllowed(msg) - return super(AllocationsController, self)._route(args, request) - def _get_allocations_collection(self, node_ident=None, resource_class=None, state=None, owner=None, marker=None, limit=None, sort_key='id', sort_dir='asc', @@ -245,6 +237,10 @@ class AllocationsController(pecan.rest.RestController): @METRICS.timer('AllocationsController.get_all') @method.expose() + @validation.api_version( + min_version=versions.MINOR_52_ALLOCATION, + message=_('The API version does not allow allocations'), + ) @args.validate(node=args.uuid_or_name, resource_class=args.string, state=args.string, @@ -295,6 +291,10 @@ class AllocationsController(pecan.rest.RestController): @METRICS.timer('AllocationsController.get_one') @method.expose() + @validation.api_version( + min_version=versions.MINOR_52_ALLOCATION, + message=_('The API version does not allow allocations'), + ) @args.validate(allocation_ident=args.uuid_or_name, fields=args.string_list) def get_one(self, allocation_ident, fields=None): """Retrieve information about the given allocation. @@ -346,6 +346,11 @@ class AllocationsController(pecan.rest.RestController): @METRICS.timer('AllocationsController.post') @method.expose(status_code=http_client.CREATED) @method.body('allocation') + @validation.api_version( + min_version=versions.MINOR_52_ALLOCATION, + message=_('The API version does not allow allocations'), + exception_class=webob_exc.HTTPMethodNotAllowed, + ) @args.validate(allocation=ALLOCATION_VALIDATOR) def post(self, allocation): """Create a new allocation. @@ -479,6 +484,11 @@ class AllocationsController(pecan.rest.RestController): @METRICS.timer('AllocationsController.patch') @method.expose() @method.body('patch') + @validation.api_version( + min_version=versions.MINOR_57_ALLOCATION_UPDATE, + message=_('The API version does not allow updating allocations'), + exception_class=webob_exc.HTTPMethodNotAllowed, + ) @args.validate(allocation_ident=args.string, patch=args.patch) def patch(self, allocation_ident, patch): """Update an existing allocation. @@ -488,10 +498,6 @@ class AllocationsController(pecan.rest.RestController): :allocation_ident: allocation_ident :patch: allocation_patch """ - if not api_utils.allow_allocation_update(): - raise webob_exc.HTTPMethodNotAllowed(_( - "The API version does not allow updating allocations")) - context = api.request.context rpc_allocation = api_utils.check_allocation_policy_and_retrieve( 'baremetal:allocation:update', allocation_ident) @@ -522,6 +528,11 @@ class AllocationsController(pecan.rest.RestController): @METRICS.timer('AllocationsController.delete') @method.expose(status_code=http_client.NO_CONTENT) + @validation.api_version( + min_version=versions.MINOR_52_ALLOCATION, + message=_('The API version does not allow allocations'), + exception_class=webob_exc.HTTPMethodNotAllowed, + ) @args.validate(allocation_ident=args.uuid_or_name) def delete(self, allocation_ident): """Delete an allocation. diff --git a/ironic/api/controllers/v1/utils.py b/ironic/api/controllers/v1/utils.py index 115fba914c..2bb0b83404 100644 --- a/ironic/api/controllers/v1/utils.py +++ b/ironic/api/controllers/v1/utils.py @@ -2085,14 +2085,6 @@ def allow_configdrive_vendor_data(): >= versions.MINOR_59_CONFIGDRIVE_VENDOR_DATA) -def allow_allocation_update(): - """Check if updating an existing allocation is allowed or not. - - Version 1.57 of the API added support for updating an allocation. - """ - return api.request.version.minor >= versions.MINOR_57_ALLOCATION_UPDATE - - def allow_allocation_backfill(): """Check if backfilling allocations is allowed.