Add manila share protectable plugin for karbor

Change-Id: Ifd9039a1b0629983538cab4e1f1d53911eda6581
blueprint: manila-share-proection-plugin
This commit is contained in:
chenying 2017-03-31 17:28:18 +08:00
parent f3e3110a27
commit 83f519c691
4 changed files with 202 additions and 0 deletions

View File

@ -34,10 +34,12 @@ RESOURCE_TYPES = (PROJECT_RESOURCE_TYPE,
SERVER_RESOURCE_TYPE,
VOLUME_RESOURCE_TYPE,
IMAGE_RESOURCE_TYPE,
SHARE_RESOURCE_TYPE,
) = ('OS::Keystone::Project',
'OS::Nova::Server',
'OS::Cinder::Volume',
'OS::Glance::Image',
'OS::Manila::Share'
)
# plan status
PLAN_STATUS_SUSPENDED = 'suspended'

View File

@ -0,0 +1,92 @@
# 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 six
from karbor.common import constants
from karbor import exception
from karbor import resource
from karbor.services.protection.client_factory import ClientFactory
from karbor.services.protection import protectable_plugin
from oslo_log import log as logging
LOG = logging.getLogger(__name__)
INVALID_SHARE_STATUS = ['deleting', 'deleted', 'error', 'error_deleting',
'manage_error', 'unmanage_error', 'extending_error',
'shrinking_error', 'reverting_error']
class ShareProtectablePlugin(protectable_plugin.ProtectablePlugin):
"""Manila share protectable plugin"""
_SUPPORT_RESOURCE_TYPE = constants.SHARE_RESOURCE_TYPE
def _client(self, context):
self._client_instance = ClientFactory.create_client(
"manila",
context)
return self._client_instance
def get_resource_type(self):
return self._SUPPORT_RESOURCE_TYPE
def get_parent_resource_types(self):
return (constants.PROJECT_RESOURCE_TYPE, )
def list_resources(self, context, parameters=None):
try:
shares = self._client(context).shares.list(detailed=True)
except Exception as e:
LOG.exception("List all summary shares from manila failed.")
raise exception.ListProtectableResourceFailed(
type=self._SUPPORT_RESOURCE_TYPE,
reason=six.text_type(e))
else:
return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE,
id=share.id, name=share.name)
for share in shares
if share.status not in INVALID_SHARE_STATUS]
def show_resource(self, context, resource_id, parameters=None):
try:
share = self._client(context).shares.get(resource_id)
except Exception as e:
LOG.exception("Show a summary share from manila failed.")
raise exception.ProtectableResourceNotFound(
id=resource_id,
type=self._SUPPORT_RESOURCE_TYPE,
reason=six.text_type(e))
else:
if share.status in INVALID_SHARE_STATUS:
raise exception.ProtectableResourceInvalidStatus(
id=resource_id, type=self._SUPPORT_RESOURCE_TYPE,
status=share.status)
return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE,
id=share.id, name=share.name)
def get_dependent_resources(self, context, parent_resource):
try:
shares = self._client(context).shares.list()
except Exception as e:
LOG.exception("List all shares from manila failed.")
raise exception.ListProtectableResourceFailed(
type=self._SUPPORT_RESOURCE_TYPE,
reason=six.text_type(e))
else:
return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE,
id=share.id,
name=share.name)
for share in shares
if share.project_id == parent_resource.id
and share.status not in INVALID_SHARE_STATUS]

View File

@ -0,0 +1,107 @@
# 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 collections
from karbor.context import RequestContext
from karbor.resource import Resource
from karbor.services.protection.protectable_plugins.share \
import ShareProtectablePlugin
from karbor.tests import base
import mock
from manilaclient.v2 import shares
from oslo_config import cfg
class ShareProtectablePluginTest(base.TestCase):
def setUp(self):
super(ShareProtectablePluginTest, self).setUp()
service_catalog = [
{'type': 'sharev2',
'endpoints': [{'publicURL': 'http://127.0.0.1:8774/v2.1/abcd'}],
},
]
self._context = RequestContext(user_id='demo',
project_id='abcd',
auth_token='efgh',
service_catalog=service_catalog)
def test_create_client_by_endpoint(self):
cfg.CONF.set_default('manila_endpoint',
'http://127.0.0.1:8774/v2.1',
'manila_client')
plugin = ShareProtectablePlugin(self._context)
self.assertEqual(
'http://127.0.0.1:8774/v2.1/abcd',
plugin._client(self._context).client.endpoint_url)
def test_create_client_by_catalog(self):
plugin = ShareProtectablePlugin(self._context)
self.assertEqual(
'http://127.0.0.1:8774/v2.1/abcd',
plugin._client(self._context).client.endpoint_url)
def test_get_resource_type(self):
plugin = ShareProtectablePlugin(self._context)
self.assertEqual("OS::Manila::Share", plugin.get_resource_type())
def test_get_parent_resource_types(self):
plugin = ShareProtectablePlugin(self._context)
self.assertEqual(("OS::Keystone::Project", ),
plugin.get_parent_resource_types())
@mock.patch.object(shares.ShareManager, 'list')
def test_list_resources(self, mock_share_list):
plugin = ShareProtectablePlugin(self._context)
share_info = collections.namedtuple('share_info', ['id', 'name',
'status'])
mock_share_list.return_value = [
share_info(id='123', name='name123', status='available'),
share_info(id='456', name='name456', status='available')]
self.assertEqual([Resource('OS::Manila::Share', '123', 'name123'),
Resource('OS::Manila::Share', '456', 'name456')],
plugin.list_resources(self._context))
@mock.patch.object(shares.ShareManager, 'get')
def test_show_resource(self, mock_share_get):
plugin = ShareProtectablePlugin(self._context)
share_info = collections.namedtuple('share_info', ['id', 'name',
'status'])
mock_share_get.return_value = share_info(id='123', name='name123',
status='available')
self.assertEqual(Resource('OS::Manila::Share', '123', 'name123'),
plugin.show_resource(self._context, '123'))
@mock.patch.object(shares.ShareManager, 'list')
def test_get_dependent_resources(self, mock_share_list):
plugin = ShareProtectablePlugin(self._context)
share_info = collections.namedtuple(
'share_info', ['id', 'name', 'status', 'project_id'])
project_info = collections.namedtuple(
'share_info', ['id', 'name', 'status'])
mock_share_list.return_value = [
share_info(id='123', name='name123', status='available',
project_id='abcd'),
share_info(id='456', name='name456', status='available',
project_id='abcd')]
project = project_info(id='abcd', name='name456', status='available')
self.assertEqual([Resource('OS::Manila::Share', '123', 'name123'),
Resource('OS::Manila::Share', '456', 'name456')],
plugin.get_dependent_resources(
self._context, project))

View File

@ -52,6 +52,7 @@ karbor.protectables =
server = karbor.services.protection.protectable_plugins.server:ServerProtectablePlugin
volume = karbor.services.protection.protectable_plugins.volume:VolumeProtectablePlugin
image = karbor.services.protection.protectable_plugins.image:ImageProtectablePlugin
share = karbor.services.protection.protectable_plugins.share:ShareProtectablePlugin
karbor.operationengine.engine.timetrigger.time_format =
crontab = karbor.services.operationengine.engine.triggers.timetrigger.timeformats.crontab_time:Crontab
karbor.operationengine.engine.executor =