Merge "Support Cinder API version 2"
This commit is contained in:
commit
c6ac4acaa2
@ -457,6 +457,14 @@
|
||||
#enable_notifications=false
|
||||
|
||||
|
||||
#
|
||||
# Options defined in sahara.utils.openstack.cinder
|
||||
#
|
||||
|
||||
# Version of the Cinder API to use. (integer value)
|
||||
#cinder_api_version=2
|
||||
|
||||
|
||||
#
|
||||
# Options defined in sahara.utils.openstack.keystone
|
||||
#
|
||||
|
@ -37,6 +37,7 @@ from sahara.service.edp import api as edp_api
|
||||
from sahara.service import ops as service_ops
|
||||
from sahara.service import periodic
|
||||
from sahara.utils import api as api_utils
|
||||
from sahara.utils.openstack import cinder
|
||||
from sahara.utils import remote
|
||||
from sahara.utils import rpc as messaging
|
||||
|
||||
@ -75,6 +76,9 @@ def setup_common(possible_topdir, service_name):
|
||||
|
||||
LOG.info(_LI('Starting Sahara %s'), service_name)
|
||||
|
||||
# Validate other configurations (that may produce logs) here
|
||||
cinder.validate_config()
|
||||
|
||||
messaging.setup()
|
||||
|
||||
if service_name != 'all-in-one':
|
||||
|
@ -38,6 +38,7 @@ detach_timeout_opt = cfg.IntOpt(
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.register_opt(detach_timeout_opt)
|
||||
CONF.import_opt('cinder_api_version', 'sahara.utils.openstack.cinder')
|
||||
|
||||
|
||||
def attach_to_instances(instances):
|
||||
@ -80,9 +81,12 @@ def _attach_volumes_to_node(node_group, instance):
|
||||
_mount_volumes_to_node(instance, devices)
|
||||
|
||||
|
||||
def _create_attach_volume(ctx, instance, size, display_name=None):
|
||||
volume = cinder.client().volumes.create(size=size,
|
||||
display_name=display_name)
|
||||
def _create_attach_volume(ctx, instance, size, name=None):
|
||||
if CONF.cinder_api_version == 1:
|
||||
kwargs = {'size': size, 'display_name': name}
|
||||
else:
|
||||
kwargs = {'size': size, 'name': name}
|
||||
volume = cinder.client().volumes.create(**kwargs)
|
||||
conductor.append_volume(ctx, instance, volume.id)
|
||||
|
||||
while volume.status != 'available':
|
||||
|
@ -30,12 +30,13 @@ class SaharaTestCase(base.BaseTestCase):
|
||||
|
||||
def setup_context(self, username="test_user", tenant_id="tenant_1",
|
||||
token="test_auth_token", tenant_name='test_tenant',
|
||||
**kwargs):
|
||||
service_catalog=None, **kwargs):
|
||||
self.addCleanup(context.set_ctx,
|
||||
context.ctx() if context.has_ctx() else None)
|
||||
|
||||
context.set_ctx(context.Context(
|
||||
username=username, tenant_id=tenant_id,
|
||||
token=token, service_catalog={},
|
||||
token=token, service_catalog=service_catalog or {},
|
||||
tenant_name=tenant_name, **kwargs))
|
||||
|
||||
def override_config(self, name, override, group=None):
|
||||
|
@ -13,7 +13,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from cinderclient.v1 import volumes as v
|
||||
from cinderclient.v1 import volumes as vol_v1
|
||||
from cinderclient.v2 import volumes as vol_v2
|
||||
import mock
|
||||
|
||||
from sahara.conductor import resource as r
|
||||
@ -25,8 +26,7 @@ from sahara.utils import general as g
|
||||
|
||||
class TestAttachVolume(base.SaharaWithDbTestCase):
|
||||
|
||||
@mock.patch(
|
||||
'sahara.service.engine.Engine.get_node_group_image_username')
|
||||
@mock.patch('sahara.service.engine.Engine.get_node_group_image_username')
|
||||
def test_mount_volume(self, p_get_username):
|
||||
p_get_username.return_value = 'root'
|
||||
|
||||
@ -52,8 +52,27 @@ class TestAttachVolume(base.SaharaWithDbTestCase):
|
||||
self.instance_name = 'spam'
|
||||
|
||||
instance = Instance()
|
||||
p_get_volume.return_value = v.Volume(None, {'id': '123',
|
||||
'status': 'available'})
|
||||
p_get_volume.return_value = vol_v1.Volume(None, {'id': '123', 'status':
|
||||
'available'})
|
||||
p_detach.return_value = None
|
||||
p_delete.return_value = None
|
||||
self.assertIsNone(
|
||||
volumes.detach_from_instance(instance))
|
||||
|
||||
@mock.patch('sahara.conductor.manager.ConductorManager.cluster_get')
|
||||
@mock.patch('cinderclient.v2.volumes.Volume.delete')
|
||||
@mock.patch('cinderclient.v2.volumes.Volume.detach')
|
||||
@mock.patch('sahara.utils.openstack.cinder.get_volume')
|
||||
def test_detach_volumes_v2(self, p_get_volume, p_detach, p_delete, p_cond):
|
||||
class Instance:
|
||||
def __init__(self):
|
||||
self.instance_id = '123454321'
|
||||
self.volumes = [123]
|
||||
self.instance_name = 'spam'
|
||||
|
||||
instance = Instance()
|
||||
p_get_volume.return_value = vol_v2.Volume(None, {'id': '123', 'status':
|
||||
'available'})
|
||||
p_detach.return_value = None
|
||||
p_delete.return_value = None
|
||||
self.assertIsNone(
|
||||
@ -63,8 +82,7 @@ class TestAttachVolume(base.SaharaWithDbTestCase):
|
||||
@mock.patch('sahara.service.volumes._mount_volume')
|
||||
@mock.patch('sahara.service.volumes._await_attach_volumes')
|
||||
@mock.patch('sahara.service.volumes._create_attach_volume')
|
||||
def test_attach(self, p_create_attach_vol,
|
||||
p_await, p_mount):
|
||||
def test_attach(self, p_create_attach_vol, p_await, p_mount):
|
||||
p_create_attach_vol.side_effect = ['/dev/vdb', '/dev/vdc'] * 2
|
||||
p_await.return_value = None
|
||||
p_mount.return_value = None
|
||||
|
80
sahara/tests/unit/utils/test_cinder.py
Normal file
80
sahara/tests/unit/utils/test_cinder.py
Normal file
@ -0,0 +1,80 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2014 Adrien Vergé <adrien.verge@numergy.com>
|
||||
#
|
||||
# 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.
|
||||
|
||||
import mock
|
||||
from oslo.config import cfg
|
||||
|
||||
from sahara import main
|
||||
from sahara.tests.unit import base as test_base
|
||||
from sahara.utils.openstack import cinder
|
||||
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class TestCinder(test_base.SaharaTestCase):
|
||||
def setup_context(self, username="test_user", tenant_id="tenant_1",
|
||||
token="test_auth_token", tenant_name='test_tenant',
|
||||
**kwargs):
|
||||
self.override_config('os_region_name', 'RegionOne')
|
||||
|
||||
# Fake service_catalog with both volume and volumev2 services available
|
||||
service_catalog = '''[
|
||||
{ "type": "volume",
|
||||
"endpoints": [ { "region": "RegionOne",
|
||||
"publicURL": "http://localhost/" } ] },
|
||||
{ "type": "volumev2",
|
||||
"endpoints": [ { "region": "RegionOne",
|
||||
"publicURL": "http://localhost/" } ] } ]'''
|
||||
|
||||
super(TestCinder, self).setup_context(
|
||||
username=username, tenant_id=tenant_id, token=token,
|
||||
tenant_name=tenant_name, service_catalog=service_catalog, **kwargs)
|
||||
|
||||
@mock.patch('cinderclient.v2.client.Client')
|
||||
@mock.patch('cinderclient.v1.client.Client')
|
||||
def test_get_cinder_client_api_v1(self, patched1, patched2):
|
||||
self.override_config('cinder_api_version', 1)
|
||||
patched1.return_value = FakeCinderClient(1)
|
||||
patched2.return_value = FakeCinderClient(2)
|
||||
|
||||
client = cinder.client()
|
||||
self.assertEqual(1, client.client.api_version)
|
||||
|
||||
@mock.patch('cinderclient.v2.client.Client')
|
||||
@mock.patch('cinderclient.v1.client.Client')
|
||||
def test_get_cinder_client_api_v2(self, patched1, patched2):
|
||||
self.override_config('cinder_api_version', 2)
|
||||
patched1.return_value = FakeCinderClient(1)
|
||||
patched2.return_value = FakeCinderClient(2)
|
||||
|
||||
client = cinder.client()
|
||||
self.assertEqual(2, client.client.api_version)
|
||||
|
||||
def test_cinder_bad_api_version(self):
|
||||
self.override_config('cinder_api_version', 0)
|
||||
cinder.validate_config()
|
||||
|
||||
# Check bad version falls back to latest supported version
|
||||
self.assertEqual(2, main.CONF.cinder_api_version)
|
||||
|
||||
|
||||
class FakeCinderClient(object):
|
||||
def __init__(self, api_version):
|
||||
class FakeCinderHTTPClient(object):
|
||||
def __init__(self, api_version):
|
||||
self.api_version = api_version
|
||||
self.client = FakeCinderHTTPClient(api_version)
|
@ -1,4 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2013 Mirantis Inc.
|
||||
# Copyright (c) 2014 Adrien Vergé <adrien.verge@numergy.com>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@ -13,19 +15,50 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from cinderclient.v1 import client as cinder_client
|
||||
from cinderclient.v1 import client as cinder_client_v1
|
||||
from cinderclient.v2 import client as cinder_client_v2
|
||||
from oslo.config import cfg
|
||||
|
||||
from sahara import context
|
||||
from sahara.i18n import _
|
||||
from sahara.openstack.common import log as logging
|
||||
from sahara.utils.openstack import base
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
cinder_opt = cfg.IntOpt('cinder_api_version', default=2,
|
||||
help='Version of the Cinder API to use.')
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.register_opt(cinder_opt)
|
||||
|
||||
|
||||
def validate_config():
|
||||
if CONF.cinder_api_version == 1:
|
||||
LOG.warn(_('The Cinder v1 API is deprecated and will be removed after '
|
||||
'the Juno release. You should set cinder_api_version=2 in '
|
||||
'your sahara.conf file.'))
|
||||
elif CONF.cinder_api_version != 2:
|
||||
LOG.warn(_('Unsupported Cinder API version: %(bad)s. Please set a '
|
||||
'correct value for cinder_api_version in your sahara.conf '
|
||||
'file (currently supported versions are: %(supported)s). '
|
||||
'Falling back to Cinder API version 2.'),
|
||||
{'bad': CONF.cinder_api_version, 'supported': [1, 2]})
|
||||
CONF.set_override('cinder_api_version', 2)
|
||||
|
||||
|
||||
def client():
|
||||
ctx = context.current()
|
||||
volume_url = base.url_for(ctx.service_catalog, 'volume')
|
||||
|
||||
cinder = cinder_client.Client(ctx.username,
|
||||
ctx.token,
|
||||
ctx.tenant_id, volume_url)
|
||||
if CONF.cinder_api_version == 1:
|
||||
volume_url = base.url_for(ctx.service_catalog, 'volume')
|
||||
cinder = cinder_client_v1.Client(ctx.username, ctx.token,
|
||||
ctx.tenant_id, volume_url)
|
||||
else:
|
||||
volume_url = base.url_for(ctx.service_catalog, 'volumev2')
|
||||
cinder = cinder_client_v2.Client(ctx.username, ctx.token,
|
||||
ctx.tenant_id, volume_url)
|
||||
|
||||
cinder.client.auth_token = ctx.token
|
||||
cinder.client.management_url = volume_url
|
||||
|
Loading…
Reference in New Issue
Block a user