Merge "Optimize-code: optimize the method of checking version"

This commit is contained in:
Zuul 2018-04-16 06:27:16 +00:00 committed by Gerrit Code Review
commit 30431c3a33
2 changed files with 43 additions and 62 deletions

View File

@ -24,7 +24,6 @@ from zun.api.controllers import link
from zun.api.controllers.v1 import collection from zun.api.controllers.v1 import collection
from zun.api.controllers.v1.schemas import containers as schema from zun.api.controllers.v1.schemas import containers as schema
from zun.api.controllers.v1.views import containers_view as view from zun.api.controllers.v1.views import containers_view as view
from zun.api.controllers import versions
from zun.api import utils as api_utils from zun.api import utils as api_utils
from zun.api import validation from zun.api import validation
from zun.common import consts from zun.common import consts
@ -297,41 +296,23 @@ class ContainersController(base.Controller):
auto_remove = container_dict.pop('auto_remove', None) auto_remove = container_dict.pop('auto_remove', None)
if auto_remove is not None: if auto_remove is not None:
req_version = pecan.request.version api_utils.version_check('auto_remove', '1.3')
min_version = versions.Version('', '', '', '1.3') try:
if req_version >= min_version: container_dict['auto_remove'] = strutils.bool_from_string(
try: auto_remove, strict=True)
container_dict['auto_remove'] = strutils.bool_from_string( except ValueError:
auto_remove, strict=True) raise exception.InvalidValue(_('Auto_remove values are: '
except ValueError: 'true, false, True, False'))
raise exception.InvalidValue(_('Auto_remove values are: '
'true, false, True, False'))
else:
raise exception.InvalidParamInVersion(param='auto_remove',
req_version=req_version,
min_version=min_version)
runtime = container_dict.pop('runtime', None) runtime = container_dict.pop('runtime', None)
if runtime is not None: if runtime is not None:
req_version = pecan.request.version api_utils.version_check('runtime', '1.5')
min_version = versions.Version('', '', '', '1.5') container_dict['runtime'] = runtime
if req_version >= min_version:
container_dict['runtime'] = runtime
else:
raise exception.InvalidParamInVersion(param='runtime',
req_version=req_version,
min_version=min_version)
hostname = container_dict.pop('hostname', None) hostname = container_dict.pop('hostname', None)
if hostname is not None: if hostname is not None:
req_version = pecan.request.version api_utils.version_check('hostname', '1.9')
min_version = versions.Version('', '', '', '1.9') container_dict['hostname'] = hostname
if req_version >= min_version:
container_dict['hostname'] = hostname
else:
raise exception.InvalidParamInVersion(param='hostname',
req_version=req_version,
min_version=min_version)
nets = container_dict.get('nets', []) nets = container_dict.get('nets', [])
requested_networks = utils.build_requested_networks(context, nets) requested_networks = utils.build_requested_networks(context, nets)
@ -340,12 +321,7 @@ class ContainersController(base.Controller):
mounts = container_dict.pop('mounts', []) mounts = container_dict.pop('mounts', [])
if mounts: if mounts:
req_version = pecan.request.version api_utils.version_check('mounts', '1.11')
min_version = versions.Version('', '', '', '1.11')
if req_version < min_version:
raise exception.InvalidParamInVersion(param='mounts',
req_version=req_version,
min_version=min_version)
requested_volumes = self._build_requested_volumes(context, mounts) requested_volumes = self._build_requested_volumes(context, mounts)
@ -640,33 +616,21 @@ class ContainersController(base.Controller):
if not force and not stop: if not force and not stop:
utils.validate_container_state(container, 'delete') utils.validate_container_state(container, 'delete')
elif force and not stop: elif force and not stop:
req_version = pecan.request.version api_utils.version_check('force', '1.7')
min_version = versions.Version('', '', '', '1.7') policy.enforce(context, "container:delete_force",
if req_version >= min_version: action="container:delete_force")
policy.enforce(context, "container:delete_force", utils.validate_container_state(container, 'delete_force')
action="container:delete_force")
utils.validate_container_state(container, 'delete_force')
else:
raise exception.InvalidParamInVersion(param='force',
req_version=req_version,
min_version=min_version)
elif stop: elif stop:
req_version = pecan.request.version api_utils.version_check('stop', '1.12')
min_version = versions.Version('', '', '', '1.12') check_policy_on_container(container.as_dict(),
if req_version >= min_version: "container:stop")
check_policy_on_container(container.as_dict(), utils.validate_container_state(container,
"container:stop") 'delete_after_stop')
utils.validate_container_state(container, if container.status == consts.RUNNING:
'delete_after_stop') LOG.debug('Calling compute.container_stop with %s '
if container.status == consts.RUNNING: 'before delete',
LOG.debug('Calling compute.container_stop with %s ' container.uuid)
'before delete', compute_api.container_stop(context, container, 10)
container.uuid)
compute_api.container_stop(context, container, 10)
else:
raise exception.InvalidParamInVersion(param='stop',
req_version=req_version,
min_version=min_version)
container.status = consts.DELETING container.status = consts.DELETING
if container.host: if container.host:
compute_api.container_delete(context, container, force) compute_api.container_delete(context, container, force)

View File

@ -18,6 +18,8 @@ from oslo_utils import uuidutils
import pecan import pecan
import wsme import wsme
from zun.api.controllers import versions
from zun.common import exception
from zun.common.i18n import _ from zun.common.i18n import _
import zun.conf import zun.conf
from zun import objects from zun import objects
@ -123,3 +125,18 @@ def enforce_content_types(valid_content_types):
return content_types_enforcer return content_types_enforcer
return content_types_decorator return content_types_decorator
def version_check(action, version):
"""Check whether the current version supports the operation.
:param action: Operations to be executed.
:param version: The minimum version required to perform the operation.
"""
req_version = pecan.request.version
min_version = versions.Version('', '', '', version)
if req_version < min_version:
raise exception.InvalidParamInVersion(param=action,
req_version=req_version,
min_version=min_version)