Refactor the code about container force delete in api
In [1], we split two method about delete to disallow non-admin user to force delete containers. It creates a lot of duplicated code, which will be a burden for further work. This patch will try to refactor it. [1] https://review.openstack.org/#/c/498767/ Change-Id: Icfb9fb25c3ce763a44e83209502da935c63d77a4
This commit is contained in:
parent
e2bdfa13f5
commit
adc0d449e2
@ -428,36 +428,6 @@ class ContainersController(base.Controller):
|
|||||||
container.save(context)
|
container.save(context)
|
||||||
return view.format_container(pecan.request.host_url, container)
|
return view.format_container(pecan.request.host_url, container)
|
||||||
|
|
||||||
@base.Controller.api_version("1.1", "1.6")
|
|
||||||
@pecan.expose('json')
|
|
||||||
@exception.wrap_pecan_controller_exception
|
|
||||||
@validation.validate_query_param(pecan.request, schema.query_param_delete)
|
|
||||||
def delete(self, container_ident, force=False, **kwargs):
|
|
||||||
"""Delete a container.
|
|
||||||
|
|
||||||
:param container_ident: UUID or Name of a container.
|
|
||||||
"""
|
|
||||||
context = pecan.request.context
|
|
||||||
if utils.is_all_tenants(kwargs):
|
|
||||||
policy.enforce(context, "container:delete_all_tenants",
|
|
||||||
action="container:delete_all_tenants")
|
|
||||||
context.all_tenants = True
|
|
||||||
container = utils.get_container(container_ident)
|
|
||||||
check_policy_on_container(container.as_dict(), "container:delete")
|
|
||||||
try:
|
|
||||||
force = strutils.bool_from_string(force, strict=True)
|
|
||||||
except ValueError:
|
|
||||||
msg = _('Valid force values are true, false, 0, 1, yes and no')
|
|
||||||
raise exception.InvalidValue(msg)
|
|
||||||
if not force:
|
|
||||||
utils.validate_container_state(container, 'delete')
|
|
||||||
else:
|
|
||||||
utils.validate_container_state(container, 'delete_force')
|
|
||||||
compute_api = pecan.request.compute_api
|
|
||||||
compute_api.container_delete(context, container, force)
|
|
||||||
pecan.response.status = 204
|
|
||||||
|
|
||||||
@base.Controller.api_version("1.7") # noqa
|
|
||||||
@pecan.expose('json')
|
@pecan.expose('json')
|
||||||
@exception.wrap_pecan_controller_exception
|
@exception.wrap_pecan_controller_exception
|
||||||
@validation.validate_query_param(pecan.request, schema.query_param_delete)
|
@validation.validate_query_param(pecan.request, schema.query_param_delete)
|
||||||
@ -482,9 +452,12 @@ class ContainersController(base.Controller):
|
|||||||
if not force:
|
if not force:
|
||||||
utils.validate_container_state(container, 'delete')
|
utils.validate_container_state(container, 'delete')
|
||||||
else:
|
else:
|
||||||
|
req_version = pecan.request.version
|
||||||
|
min_version = versions.Version('', '', '', '1.7')
|
||||||
|
if req_version >= min_version:
|
||||||
|
policy.enforce(context, "container:delete_force",
|
||||||
|
action="container:delete_force")
|
||||||
utils.validate_container_state(container, 'delete_force')
|
utils.validate_container_state(container, 'delete_force')
|
||||||
policy.enforce(context, "container:delete_force",
|
|
||||||
action="container:delete_force")
|
|
||||||
compute_api = pecan.request.compute_api
|
compute_api = pecan.request.compute_api
|
||||||
container.status = consts.DELETING
|
container.status = consts.DELETING
|
||||||
compute_api.container_delete(context, container, force)
|
compute_api.container_delete(context, container, force)
|
||||||
|
@ -1285,7 +1285,9 @@ class TestContainerController(api_base.FunctionalTest):
|
|||||||
"Cannot delete container %s in Running state" % uuid):
|
"Cannot delete container %s in Running state" % uuid):
|
||||||
self.delete('/v1/containers/%s' % (test_object.uuid))
|
self.delete('/v1/containers/%s' % (test_object.uuid))
|
||||||
|
|
||||||
def test_delete_force_by_uuid_invalid_state(self):
|
@patch('zun.common.policy.enforce')
|
||||||
|
def test_delete_force_by_uuid_invalid_state(self, mock_policy):
|
||||||
|
mock_policy.return_value = True
|
||||||
uuid = uuidutils.generate_uuid()
|
uuid = uuidutils.generate_uuid()
|
||||||
test_object = utils.create_test_container(context=self.context,
|
test_object = utils.create_test_container(context=self.context,
|
||||||
uuid=uuid, status='Paused')
|
uuid=uuid, status='Paused')
|
||||||
|
Loading…
Reference in New Issue
Block a user