From 7c9761b3bb52788021b5bf0eb9726af833db4074 Mon Sep 17 00:00:00 2001 From: Unmesh Gurjar Date: Thu, 11 Oct 2012 23:48:08 -0700 Subject: [PATCH] Fix error on invalid delete_on_termination value 1. Fixed 500 error when an invalid value is passed to 'delete_on_termination' parameter in 'block_device_mapping' of create server API (json request). Converted the value to 'bool' (similar to XML request workflow) to fix issue. 2. Also added unit test coverage. Fixes LP: #1060955 Change-Id: Ib35040f17a3712dac45ec1e3c2f32fee17db7c03 --- nova/api/openstack/compute/servers.py | 6 ++++- .../api/openstack/compute/test_servers.py | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 89eac8bfd6e..cdc95db0ba8 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -792,7 +792,11 @@ def create(self, req, body): block_device_mapping = None if self.ext_mgr.is_loaded('os-volumes'): - block_device_mapping = server_dict.get('block_device_mapping') + block_device_mapping = server_dict.get('block_device_mapping', []) + for bdm in block_device_mapping: + if 'delete_on_termination' in bdm: + bdm['delete_on_termination'] = utils.bool_from_str( + bdm['delete_on_termination']) ret_resv_id = False # min_count and max_count are optional. If they exist, they may come diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py index e8162fda65a..f57ffe32bfa 100644 --- a/nova/tests/api/openstack/compute/test_servers.py +++ b/nova/tests/api/openstack/compute/test_servers.py @@ -2047,6 +2047,29 @@ def create(*args, **kwargs): self.stubs.Set(compute_api.API, 'create', create) self._test_create_extra(params) + def test_create_instance_with_bdm_delete_on_termination(self): + self.ext_mgr.extensions = {'os-volumes': 'fake'} + bdm = [{'device_name': 'foo1', 'delete_on_termination': 1}, + {'device_name': 'foo2', 'delete_on_termination': True}, + {'device_name': 'foo3', 'delete_on_termination': 'invalid'}, + {'device_name': 'foo4', 'delete_on_termination': 0}, + {'device_name': 'foo5', 'delete_on_termination': False}] + expected_dbm = [ + {'device_name': 'foo1', 'delete_on_termination': True}, + {'device_name': 'foo2', 'delete_on_termination': True}, + {'device_name': 'foo3', 'delete_on_termination': False}, + {'device_name': 'foo4', 'delete_on_termination': False}, + {'device_name': 'foo5', 'delete_on_termination': False}] + params = {'block_device_mapping': bdm} + old_create = compute_api.API.create + + def create(*args, **kwargs): + self.assertEqual(kwargs['block_device_mapping'], expected_dbm) + return old_create(*args, **kwargs) + + self.stubs.Set(compute_api.API, 'create', create) + self._test_create_extra(params) + def test_create_instance_with_user_data_enabled(self): self.ext_mgr.extensions = {'os-user-data': 'fake'} user_data = 'fake'