From df9af212c2c25c85f6f4a738f9e0f893fdd3bb95 Mon Sep 17 00:00:00 2001 From: Rafael Folco Date: Tue, 3 Jan 2017 20:03:39 +0000 Subject: [PATCH] DELETE all inventory for a resource provider This patch adds a new method for deleting all inventories for a resource provider: DELETE /resource-providers/{uuid}/inventories Return codes: 204 NoContent on success 404 NotFound if the resource provider does not exist 405 MethodNotAllowed if a microversion is specified that is before this change (1.5) 409 Conflict if inventory in use or if some other request concurrently updates this resource provider Change-Id: I1ecb12c888f873e8330367c8411d5a2ef0458495 Implements: bp delete-inventories-placement-api --- nova/api/openstack/placement/handler.py | 3 +- .../openstack/placement/handlers/inventory.py | 32 ++++++++++++++ nova/api/openstack/placement/microversion.py | 1 + .../placement/rest_api_version_history.rst | 8 ++++ .../placement/gabbits/inventory.yaml | 43 +++++++++++++++++++ .../placement/gabbits/microversion.yaml | 4 +- .../placement/gabbits/with-allocations.yaml | 11 +++++ ...tories-placement-api-13582910371308c4.yaml | 18 ++++++++ 8 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/delete-inventories-placement-api-13582910371308c4.yaml diff --git a/nova/api/openstack/placement/handler.py b/nova/api/openstack/placement/handler.py index 2c9750f18..0767b0b41 100644 --- a/nova/api/openstack/placement/handler.py +++ b/nova/api/openstack/placement/handler.py @@ -80,7 +80,8 @@ ROUTE_DECLARATIONS = { '/resource_providers/{uuid}/inventories': { 'GET': inventory.get_inventories, 'POST': inventory.create_inventory, - 'PUT': inventory.set_inventories + 'PUT': inventory.set_inventories, + 'DELETE': inventory.delete_inventories }, '/resource_providers/{uuid}/inventories/{resource_class}': { 'GET': inventory.get_inventory, diff --git a/nova/api/openstack/placement/handlers/inventory.py b/nova/api/openstack/placement/handlers/inventory.py index 5ddc638b7..5a4fa0b18 100644 --- a/nova/api/openstack/placement/handlers/inventory.py +++ b/nova/api/openstack/placement/handlers/inventory.py @@ -18,6 +18,7 @@ from oslo_serialization import jsonutils from oslo_utils import encodeutils import webob +from nova.api.openstack.placement import microversion from nova.api.openstack.placement import util from nova.api.openstack.placement import wsgi_wrapper from nova import db @@ -378,6 +379,37 @@ def set_inventories(req): return _send_inventories(req.response, resource_provider, inventories) +@wsgi_wrapper.PlacementWsgify +def delete_inventories(req): + """DELETE all inventory for a resource provider. + + Delete inventory as required to reset all the inventory. + If an inventory to be deleted is in use, return a 409 Conflict. + On success return a 204 No content. + Return 405 Method Not Allowed if the wanted microversion does not match. + """ + microversion.raise_http_status_code_if_not_version(req, 405, (1, 5)) + context = req.environ['placement.context'] + uuid = util.wsgi_path_item(req.environ, 'uuid') + resource_provider = objects.ResourceProvider.get_by_uuid( + context, uuid) + + inventories = objects.InventoryList(objects=[]) + + try: + resource_provider.set_inventory(inventories) + except (exception.ConcurrentUpdateDetected, + exception.InventoryInUse) as exc: + raise webob.exc.HTTPConflict( + _('update conflict: %(error)s') % {'error': exc}) + + response = req.response + response.status = 204 + response.content_type = None + + return response + + @wsgi_wrapper.PlacementWsgify @util.require_content('application/json') def update_inventory(req): diff --git a/nova/api/openstack/placement/microversion.py b/nova/api/openstack/placement/microversion.py index 847127bbf..42facbe8d 100644 --- a/nova/api/openstack/placement/microversion.py +++ b/nova/api/openstack/placement/microversion.py @@ -40,6 +40,7 @@ VERSIONS = [ '1.3', # Adds 'member_of' query parameter to get resource providers # that are members of any of the listed aggregates '1.4', # Adds resources query string parameter in GET /resource_providers + '1.5', # Adds DELETE /resource_providers/{uuid}/inventories ] diff --git a/nova/api/openstack/placement/rest_api_version_history.rst b/nova/api/openstack/placement/rest_api_version_history.rst index f65397244..a0d747bb9 100644 --- a/nova/api/openstack/placement/rest_api_version_history.rst +++ b/nova/api/openstack/placement/rest_api_version_history.rst @@ -81,3 +81,11 @@ If the resource class does not exist, then it will return a HTTP 400. requested for a given inventory and resource provider. The `step_size` is the increment of resource that can be requested for a given resource on a given provider. + +1.5 DELETE all inventory for a resource provider +------------------------------------------------ + +Placement API version 1.5 adds DELETE method for deleting all inventory for a +resource provider. The following new method is supported: + +* DELETE /resource_providers/{uuid}/inventories diff --git a/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml b/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml index 56cde160c..1a68338e5 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml @@ -15,6 +15,12 @@ tests: response_json_paths: $.errors[0].title: Not Found +- name: delete all inventory for missing resource provider + DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + openstack-api-version: placement 1.5 + status: 404 + - name: post new resource provider POST: /resource_providers request_headers: @@ -538,3 +544,40 @@ tests: - Unable to update inventory response_json_paths: $.errors[0].title: Bad Request + +- name: delete all inventory bad generation + PUT: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + content-type: application/json + data: + resource_provider_generation: 99 + inventories: + IPV4_ADDRESS: + total: 253 + status: 409 + response_strings: + - resource provider generation conflict + +- name: delete all inventory + DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + openstack-api-version: placement 1.5 + status: 204 + +- name: get inventories after deletion + GET: /resource_providers/$ENVIRON['RP_UUID']/inventories + response_json_paths: + $.resource_provider_generation: 7 + $.inventories: {} + +- name: delete empty inventories + DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + openstack-api-version: placement 1.5 + status: 204 + +- name: delete inventories earlier version + DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + openstack-api-version: placement 1.4 + status: 405 diff --git a/nova/tests/functional/api/openstack/placement/gabbits/microversion.yaml b/nova/tests/functional/api/openstack/placement/gabbits/microversion.yaml index 72b12e298..390b65713 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/microversion.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/microversion.yaml @@ -39,13 +39,13 @@ tests: response_json_paths: $.errors[0].title: Not Acceptable -- name: latest microversion is 1.4 +- name: latest microversion is 1.5 GET: / request_headers: openstack-api-version: placement latest response_headers: vary: /OpenStack-API-Version/ - openstack-api-version: placement 1.4 + openstack-api-version: placement 1.5 - name: other accept header bad version GET: / diff --git a/nova/tests/functional/api/openstack/placement/gabbits/with-allocations.yaml b/nova/tests/functional/api/openstack/placement/gabbits/with-allocations.yaml index 592eac33a..87e9d699e 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/with-allocations.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/with-allocations.yaml @@ -30,3 +30,14 @@ tests: status: 409 response_strings: - "Unable to delete resource provider $ENVIRON['RP_UUID']: Resource provider has allocations." + +- name: fail to delete all inventory + DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + accept: application/json + openstack-api-version: placement 1.5 + status: 409 + response_headers: + content-type: /application/json/ + response_strings: + - "Inventory for 'VCPU, DISK_GB' on resource provider '$ENVIRON['RP_UUID']' in use" diff --git a/releasenotes/notes/delete-inventories-placement-api-13582910371308c4.yaml b/releasenotes/notes/delete-inventories-placement-api-13582910371308c4.yaml new file mode 100644 index 000000000..69d7195e5 --- /dev/null +++ b/releasenotes/notes/delete-inventories-placement-api-13582910371308c4.yaml @@ -0,0 +1,18 @@ +--- +features: + - | + Supports a new method for deleting all inventory for a + resource provider + + * DELETE /resource-providers/{uuid}/inventories + + Return codes + + * 204 NoContent on success + * 404 NotFound for missing resource provider + * 405 MethodNotAllowed if a microversion is specified that is before + this change (1.5) + * 409 Conflict if inventory in use or if some other request concurrently + updates this resource provider + + Requires OpenStack-API-Version placement 1.5