Add extension block_device_mapping_v1 for v2.1

In the beginning v3 didn't support legacy block_device_mapping request.
For v2.1, we want to keep back-compatibility with v2 api, so we need
add legacy block_device_mapping.

This patch add new extension block_device_mapping_v1 for v2.1. Also
share legacy block_device_mapping related unittests between v2 and v2.1.

Partially implements blueprint v2-on-v3-api

Change-Id: I841833d8fd0995ddf49febd9c1611cf711742f5a
This commit is contained in:
He Jie Xu
2014-08-20 13:51:21 +08:00
parent 606d25ffdc
commit 8353cb0524
7 changed files with 503 additions and 280 deletions

View File

@@ -20,9 +20,11 @@ from webob import exc
from nova.api.openstack import extensions
from nova import block_device
from nova import exception
from nova.i18n import _
ALIAS = "os-block-device-mapping"
ATTRIBUTE_NAME = "block_device_mapping_v2"
LEGACY_ATTRIBUTE_NAME = "block_device_mapping"
class BlockDeviceMapping(extensions.V3APIExtensionBase):
@@ -43,18 +45,23 @@ class BlockDeviceMapping(extensions.V3APIExtensionBase):
# NOTE(gmann): This function is not supposed to use 'body_deprecated_param'
# parameter as this is placed to handle scheduler_hint extension for V2.1.
def server_create(self, server_dict, create_kwargs, body_deprecated_param):
block_device_mapping = server_dict.get(ATTRIBUTE_NAME, [])
bdm = server_dict.get(ATTRIBUTE_NAME, [])
legacy_bdm = server_dict.get(LEGACY_ATTRIBUTE_NAME, [])
if bdm and legacy_bdm:
expl = _('Using different block_device_mapping syntaxes '
'is not allowed in the same request.')
raise exc.HTTPBadRequest(explanation=expl)
try:
block_device_mapping = [
block_device.BlockDeviceDict.from_api(bdm_dict)
for bdm_dict in block_device_mapping]
for bdm_dict in bdm]
except (exception.InvalidBDMFormat,
exception.InvalidBDMVolumeNotBootable) as e:
raise exc.HTTPBadRequest(explanation=e.format_message())
create_kwargs['block_device_mapping'] = block_device_mapping
# Unset the legacy_bdm flag if we got a block device mapping.
if block_device_mapping:
create_kwargs['block_device_mapping'] = block_device_mapping
# Unset the legacy_bdm flag if we got a block device mapping.
create_kwargs['legacy_bdm'] = False

View File

@@ -0,0 +1,71 @@
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""The legacy block device mappings extension."""
from webob import exc
from nova.api.openstack import extensions
from nova import block_device
from nova import exception
from nova.i18n import _
from nova.openstack.common import strutils
ALIAS = "os-block-device-mapping-v1"
ATTRIBUTE_NAME = "block_device_mapping"
ATTRIBUTE_NAME_V2 = "block_device_mapping_v2"
class BlockDeviceMappingV1(extensions.V3APIExtensionBase):
"""Block device mapping boot support."""
name = "BlockDeviceMappingV1"
alias = ALIAS
version = 1
def get_resources(self):
return []
def get_controller_extensions(self):
return []
# use nova.api.extensions.server.extensions entry point to modify
# server create kwargs
# NOTE(gmann): This function is not supposed to use 'body_deprecated_param'
# parameter as this is placed to handle scheduler_hint extension for V2.1.
def server_create(self, server_dict, create_kwargs, body_deprecated_param):
block_device_mapping = server_dict.get(ATTRIBUTE_NAME, [])
block_device_mapping_v2 = server_dict.get(ATTRIBUTE_NAME_V2, [])
if block_device_mapping and block_device_mapping_v2:
expl = _('Using different block_device_mapping syntaxes '
'is not allowed in the same request.')
raise exc.HTTPBadRequest(explanation=expl)
for bdm in block_device_mapping:
try:
block_device.validate_device_name(bdm.get("device_name"))
block_device.validate_and_default_volume_size(bdm)
except exception.InvalidBDMFormat as e:
raise exc.HTTPBadRequest(explanation=e.format_message())
if 'delete_on_termination' in bdm:
bdm['delete_on_termination'] = strutils.bool_from_string(
bdm['delete_on_termination'])
if block_device_mapping:
create_kwargs['block_device_mapping'] = block_device_mapping
# Sets the legacy_bdm flag if we got a legacy block device mapping.
create_kwargs['legacy_bdm'] = True

View File

@@ -551,7 +551,8 @@ class ServersController(wsgi.Controller):
exception.InvalidBDMVolume,
exception.InvalidBDMImage,
exception.InvalidBDMBootSequence,
exception.InvalidBDMLocalsLimit) as error:
exception.InvalidBDMLocalsLimit,
exception.InvalidBDMVolumeNotBootable) as error:
raise exc.HTTPBadRequest(explanation=error.format_message())
except (exception.PortInUse,
exception.NetworkAmbiguous,