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
This commit is contained in:
Zhenguo Niu 2016-12-28 18:01:27 +08:00
parent 17ff537060
commit c840aabc95
4 changed files with 51 additions and 2 deletions

View File

@ -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."))
]

View File

@ -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,

View File

@ -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')

View File

@ -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()