From c840aabc9505215def330d627bc177d4cf652a1a Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Wed, 28 Dec 2016 18:01:27 +0800 Subject: [PATCH] Add default az support Add a default_schedule_zone config option which is default to None, when user doesn't specify az, set it to instance. Change-Id: Id178c8f996ca33e76620608a7a1531a37ccf9c3c --- nimble/conf/engine.py | 5 ++- nimble/engine/api.py | 3 ++ nimble/engine/scheduler/node_manager.py | 3 +- nimble/tests/unit/engine/test_engine_api.py | 42 +++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/nimble/conf/engine.py b/nimble/conf/engine.py index 2278acf3..9aee9077 100644 --- a/nimble/conf/engine.py +++ b/nimble/conf/engine.py @@ -37,7 +37,10 @@ opts = [ cfg.StrOpt('scheduler_driver', default='nimble.engine.scheduler.filter_scheduler.' 'FilterScheduler', - help='Default scheduler driver to use') + help=_('Default scheduler driver to use')), + cfg.StrOpt('default_schedule_zone', + help=_("Availability zone to use when user doesn't " + "specify one.")) ] diff --git a/nimble/engine/api.py b/nimble/engine/api.py index 1ff28e3b..a8735d44 100644 --- a/nimble/engine/api.py +++ b/nimble/engine/api.py @@ -19,6 +19,7 @@ from oslo_log import log from oslo_utils import timeutils from nimble.common import exception +from nimble.conf import CONF from nimble.engine import rpcapi from nimble.engine import status from nimble import image @@ -114,6 +115,8 @@ class API(object): azs = self.engine_rpcapi.list_availability_zones(context) if availability_zone not in azs['availability_zones']: raise exception.AZNotFound + else: + availability_zone = CONF.engine.default_schedule_zone return self._create_instance(context, instance_type, image_uuid, name, description, diff --git a/nimble/engine/scheduler/node_manager.py b/nimble/engine/scheduler/node_manager.py index 2a0753f5..135ca0a8 100644 --- a/nimble/engine/scheduler/node_manager.py +++ b/nimble/engine/scheduler/node_manager.py @@ -35,7 +35,8 @@ class NodeState(object): def __init__(self, node): self.node = node.uuid self.capabilities = node.properties.get('capabilities') - self.availability_zone = node.properties.get('availability_zone') + self.availability_zone = node.properties.get('availability_zone') \ + or CONF.engine.default_schedule_zone self.instance_type = node.properties.get('instance_type') diff --git a/nimble/tests/unit/engine/test_engine_api.py b/nimble/tests/unit/engine/test_engine_api.py index ea868e2c..e067d775 100644 --- a/nimble/tests/unit/engine/test_engine_api.py +++ b/nimble/tests/unit/engine/test_engine_api.py @@ -16,6 +16,7 @@ """Unit tests for engine API.""" import mock +from oslo_config import cfg from oslo_context import context from nimble.common import exception @@ -120,6 +121,47 @@ class ComputeAPIUnitTest(base.DbTestCase): self.assertTrue(mock_create.called) self.assertTrue(mock_get_image.called) + @mock.patch.object(engine_rpcapi.EngineAPI, 'create_instance') + @mock.patch('nimble.engine.api.API._provision_instances') + @mock.patch('nimble.engine.api.API._get_image') + @mock.patch('nimble.engine.api.API._validate_and_build_base_options') + @mock.patch.object(engine_rpcapi.EngineAPI, 'list_availability_zones') + def test_create_default_az(self, mock_list_az, mock_validate, + mock_get_image, mock_provision, mock_create): + cfg.CONF.set_override('default_schedule_zone', 'default_az', 'engine') + instance_type = self._create_instance_type() + + base_options = {'image_uuid': 'fake-uuid', + 'status': status.BUILDING, + 'user_id': 'fake-user', + 'project_id': 'fake-project', + 'instance_type_uuid': 'fake-type-uuid', + 'name': 'fake-name', + 'description': 'fake-description', + 'extra': {'k1', 'v1'}, + 'availability_zone': 'default_az'} + mock_validate.return_value = base_options + mock_get_image.side_effect = None + mock_create.return_value = mock.MagicMock() + + self.engine_api.create( + self.context, + instance_type=instance_type, + image_uuid='fake-uuid', + name='fake-name', + description='fake-descritpion', + availability_zone=None, + extra={'k1', 'v1'}, + requested_networks=[{'uuid': 'fake'}]) + + self.assertFalse(mock_list_az.called) + mock_validate.assert_called_once_with( + self.context, instance_type, 'fake-uuid', 'fake-name', + 'fake-descritpion', 'default_az', {'k1', 'v1'}) + mock_provision.assert_called_once_with(self.context, base_options) + self.assertTrue(mock_create.called) + self.assertTrue(mock_get_image.called) + @mock.patch.object(engine_rpcapi.EngineAPI, 'list_availability_zones') def test_create_with_invalid_az(self, mock_list_az): instance_type = mock.MagicMock()