Adds network creation to ShareManager

Partially implements: bp join-tenant-network

Change-Id: I85a69e882ad299b059e40bfea850a169e6e4660d
This commit is contained in:
Andrei V. Ostapenko 2013-12-20 13:40:20 +02:00 committed by Aleks Chirko
parent c81ad66e7e
commit b92c6f4595
7 changed files with 103 additions and 3 deletions

View File

@ -521,3 +521,7 @@ class ShareNetworkSecurityServiceAssociationError(ManilaException):
class ShareNetworkSecurityServiceDissociationError(ManilaException):
message = _("Failed to dissociate share network %(share_network_id)s"
" and security service %(security_service_id)s: %(reason)s.")
class InvalidShareNetwork(ManilaException):
message = _("Invalid share network: %(reason)s")

View File

@ -140,6 +140,14 @@ class ShareDriver(object):
return self._stats
def get_network_allocations_number(self):
"""Returns number of network allocations for creating VIFs"""
pass
def setup_network(self, network_info):
"""Set up and configures VIFs with given network parameters"""
pass
def _update_share_status(self):
"""Retrieve status info from share group."""

View File

@ -286,3 +286,11 @@ class GlusterfsShareDriver(driver.ExecuteMixin, driver.ShareDriver):
self._manage_access(context, share, access,
lambda dl, acc:
True if acc not in dl else dl.remove(acc))
def get_network_allocations_number(self):
"""GlusterFS driver does not need to create VIFS"""
return 0
def setup_network(self, network_info):
"""Nothing to set up"""
pass

View File

@ -325,6 +325,14 @@ class LVMShareDriver(driver.ExecuteMixin, driver.ShareDriver):
'count=%d' % (size_in_g * 1024), 'bs=1M',
*extra_flags, run_as_root=True)
def get_network_allocations_number(self):
"""LVM driver does not need to create VIFS"""
return 0
def setup_network(self, network_info):
"""Nothing to set up"""
pass
class NASHelperBase(object):
"""Interface to work with share."""

View File

@ -291,6 +291,14 @@ class NetAppShareDriver(driver.ShareDriver):
self._stats = data
def get_network_allocations_number(self):
"""7mode driver does not need to create VIFS"""
return 0
def setup_network(self, network_info):
"""Nothing to set up"""
pass
def _check_response(request, response):
"""Checks RPC responses from NetApp devices."""

View File

@ -22,9 +22,11 @@
:class:`manila.share.drivers.lvm.LVMShareDriver`.
"""
from manila.common import constants
from manila import context
from manila import exception
from manila import manager
from manila import network
from manila.openstack.common import excutils
from manila.openstack.common import importutils
from manila.openstack.common import log as logging
@ -62,9 +64,8 @@ class ShareManager(manager.SchedulerDependentManager):
if not share_driver:
share_driver = self.configuration.share_driver
self.driver = importutils.import_object(
share_driver,
self.db,
configuration=self.configuration)
share_driver, self.db, configuration=self.configuration)
self.network_api = network.API()
def init_host(self):
"""Initialization for a standalone service."""
@ -92,6 +93,18 @@ class ShareManager(manager.SchedulerDependentManager):
self.publish_service_capabilities(ctxt)
def _setup_share_network(self, context, network_ref):
allocation_number = self.driver.get_network_allocations_number()
if allocation_number:
network_info = self.network_api.allocate_network(
context, network_ref, count=allocation_number)
try:
self.driver.setup_network(network_info)
except exception.ManilaException as e:
with excutils.save_and_reraise_exception():
self.db.share_network_update(context, network_ref['id'],
{'status': 'error'})
def create_share(self, context, share_id, request_spec=None,
filter_properties=None, snapshot_id=None):
"""Creates a share."""
@ -105,6 +118,23 @@ class ShareManager(manager.SchedulerDependentManager):
else:
snapshot_ref = None
network_id = share_ref.get('share_network_id', None)
if network_id:
network_ref = self.db.share_network_get(
context, share_ref['share_network_id'])
if network_ref['status'] != constants.STATUS_ACTIVE:
if network_ref['status'] in [constants.STATUS_INACTIVE,
constants.STATUS_NEW]:
self._setup_share_network(context, network_ref)
else:
msg = _("Network status should be ACTIVE, INACTIVE or NEW")
LOG.error(msg)
raise exception.InvalidShareNetwork(reason=msg)
else:
network_ref = {}
share_ref['network_info'] = network_ref
try:
if snapshot_ref:
export_location = self.driver.create_share_from_snapshot(

View File

@ -70,6 +70,12 @@ class FakeShareDriver(object):
def do_setup(self, context):
pass
def setup_network(self, context, network, policy=None):
pass
def get_network_allocations_number(self):
pass
class ShareTestCase(test.TestCase):
"""Test Case for shares."""
@ -355,3 +361,31 @@ class ShareTestCase(test.TestCase):
'name',
'description',
metadata=test_meta)
def test_setup_share_network(self):
network_info = {'fake': 'fake'}
self.share.driver.get_network_allocations_number = mock.Mock(
return_value=555)
self.share.network_api.allocate_network = mock.Mock(
return_value={'network_info': 'network_info'})
self.share.driver.setup_network = mock.Mock()
self.share._setup_share_network(self.context, network_info)
self.share.network_api.allocate_network.assert_called_once_with(
self.context, network_info, count=555)
self.share.driver.setup_network.assert_called_once_with(
{'network_info': 'network_info'})
def test_setup_share_network_error(self):
network_info = {'fake': 'fake', 'id': 'fakeid'}
self.share.driver.get_network_allocations_number = mock.Mock(
return_value=555)
self.share.network_api.allocate_network = mock.Mock(
return_value={'network_info': 'network_info'})
self.share.driver.setup_network = mock.Mock(
side_effect=exception.Invalid)
self.share.db.share_network_update = mock.Mock()
self.assertRaises(exception.Invalid,
self.share._setup_share_network,
self.context, network_info)
self.share.db.share_network_update.assert_called_once_with(
self.context, 'fakeid', {'status': 'error'})