Add trove database instance protectable plugin for karbor

Change-Id: Ib3a9988ded4dadd35e0f1a1053b2700c3a53a555
blueprint: trove-database-proection-plugin
This commit is contained in:
chenying 2017-07-11 18:04:11 +08:00
parent 857cb1d843
commit b7a94c884d
8 changed files with 348 additions and 1 deletions

View File

@ -51,12 +51,14 @@ RESOURCE_TYPES = (PROJECT_RESOURCE_TYPE,
IMAGE_RESOURCE_TYPE,
SHARE_RESOURCE_TYPE,
NETWORK_RESOURCE_TYPE,
DATABASE_RESOURCE_TYPE,
) = ('OS::Keystone::Project',
'OS::Nova::Server',
'OS::Cinder::Volume',
'OS::Glance::Image',
'OS::Manila::Share',
'OS::Neutron::Network',
'OS::Trove::Instance',
)
# plan status
PLAN_STATUS_SUSPENDED = 'suspended'

View File

@ -76,7 +76,7 @@ class RequestContext(context.RequestContext):
('identity', 'compute', 'object-store',
'image', 'volume', 'volumev2', 'network',
'volumev3', 'orchestration',
'share', 'sharev2')]
'share', 'sharev2', 'database')]
else:
# if list is empty or none
self.service_catalog = []

View File

@ -0,0 +1,76 @@
# 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.
from troveclient import client as tc
from oslo_config import cfg
from oslo_log import log as logging
from karbor.common import config
from karbor.services.protection.clients import utils
LOG = logging.getLogger(__name__)
SERVICE = "trove"
trove_client_opts = [
cfg.StrOpt(SERVICE + '_endpoint',
help='URL of the trove endpoint.'),
cfg.StrOpt(SERVICE + '_catalog_info',
default='database:trove:publicURL',
help='Info to match when looking for trove in the service '
'catalog. Format is: separated values of the form: '
'<service_type>:<service_name>:<endpoint_type> - '
'Only used if trove_endpoint is unset'),
cfg.StrOpt(SERVICE + '_ca_cert_file',
default=None,
help='Location of the CA certificate file '
'to use for client requests in SSL connections.'),
cfg.BoolOpt(SERVICE + '_auth_insecure',
default=False,
help='Bypass verification of server certificate when '
'making SSL connection to trove.'),
]
CONFIG_GROUP = '%s_client' % SERVICE
CONF = cfg.CONF
CONF.register_opts(config.service_client_opts, group=CONFIG_GROUP)
CONF.register_opts(trove_client_opts, group=CONFIG_GROUP)
CONF.set_default('service_name', 'trove', CONFIG_GROUP)
CONF.set_default('service_type', 'database', CONFIG_GROUP)
TROVECLIENT_VERSION = '1.0'
def create(context, conf, **kwargs):
conf.register_opts(trove_client_opts, group=CONFIG_GROUP)
client_config = conf[CONFIG_GROUP]
url = utils.get_url(SERVICE, context, client_config,
append_project_fmt='%(url)s/%(project)s', **kwargs)
endpoint = url % {"tenant_id": context.project_id}
LOG.debug('Creating trove client with url %s.', endpoint)
if kwargs.get('session'):
return tc.Client(TROVECLIENT_VERSION, session=kwargs.get('session'),
endpoint_override=endpoint)
args = {
'input_auth_token': context.auth_token,
'project_id': context.project_id,
'service_catalog_url': endpoint,
'cacert': client_config.trove_ca_cert_file,
'insecure': client_config.trove_auth_insecure,
}
client = tc.Client(TROVECLIENT_VERSION, **args)
client.client.auth_token = context.auth_token
client.client.management_url = endpoint
return client

View File

@ -0,0 +1,90 @@
# 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_INSTANCE_STATUS = ['BUILD', 'REBOOT', 'RESIZE', 'ERROR']
class DatabaseInstanceProtectablePlugin(protectable_plugin.ProtectablePlugin):
"""Trove database instances protectable plugin"""
_SUPPORT_RESOURCE_TYPE = constants.DATABASE_RESOURCE_TYPE
def _client(self, context):
self._client_instance = ClientFactory.create_client(
"trove",
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:
instances = self._client(context).instances.list()
except Exception as e:
LOG.exception("List all database instances from trove failed.")
raise exception.ListProtectableResourceFailed(
type=self._SUPPORT_RESOURCE_TYPE,
reason=six.text_type(e))
else:
return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE,
id=instance.id, name=instance.name)
for instance in instances
if instance.status not in INVALID_INSTANCE_STATUS]
def show_resource(self, context, resource_id, parameters=None):
try:
instance = self._client(context).instances.get(resource_id)
except Exception as e:
LOG.exception("Show a database instance from trove failed.")
raise exception.ProtectableResourceNotFound(
id=resource_id,
type=self._SUPPORT_RESOURCE_TYPE,
reason=six.text_type(e))
else:
if instance.status in INVALID_INSTANCE_STATUS:
raise exception.ProtectableResourceInvalidStatus(
id=resource_id, type=self._SUPPORT_RESOURCE_TYPE,
status=instance.status)
return resource.Resource(type=self._SUPPORT_RESOURCE_TYPE,
id=instance.id, name=instance.name)
def get_dependent_resources(self, context, parent_resource):
try:
instances = self._client(context).instances.list()
except Exception as e:
LOG.exception("List all database instances from trove failed.")
raise exception.ListProtectableResourceFailed(
type=self._SUPPORT_RESOURCE_TYPE,
reason=six.text_type(e))
else:
return [resource.Resource(type=self._SUPPORT_RESOURCE_TYPE,
id=instance.id,
name=instance.name)
for instance in instances
if instance.project_id == parent_resource.id
and instance.status not in INVALID_INSTANCE_STATUS]

View File

@ -0,0 +1,69 @@
# 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 keystoneauth1 import session as keystone_session
from oslo_config import cfg
from karbor.context import RequestContext
from karbor.services.protection.clients import trove
from karbor.tests import base
class TroveClientTest(base.TestCase):
def setUp(self):
super(TroveClientTest, self).setUp()
self._public_url = 'http://127.0.0.1:8776/v2/abcd'
service_catalog = [
{'type': 'database',
'name': 'trove',
'endpoints': [{'publicURL': self._public_url}],
},
]
self._context = RequestContext(user_id='demo',
project_id='abcd',
auth_token='efgh',
service_catalog=service_catalog)
@mock.patch('karbor.services.protection.clients.utils.get_url')
@mock.patch('troveclient.client.Client')
def test_create_client(self, create, get_url):
get_url.return_value = self._public_url
client_version = trove.TROVECLIENT_VERSION
session = keystone_session.Session(auth=None)
trove.create(self._context, cfg.CONF, session=session)
create.assert_called_with(client_version,
endpoint_override=self._public_url,
session=session)
@mock.patch('karbor.services.protection.clients.utils.get_url')
@mock.patch('troveclient.client.Client')
def test_create_client_without_session(self, create, get_url):
get_url.return_value = self._public_url
client_config = cfg.CONF[trove.CONFIG_GROUP]
client_version = trove.TROVECLIENT_VERSION
args = {
'input_auth_token': self._context.auth_token,
'project_id': self._context.project_id,
'service_catalog_url': self._public_url,
'cacert': client_config.trove_ca_cert_file,
'insecure': client_config.trove_auth_insecure,
}
trove.create(self._context, cfg.CONF)
create.assert_called_with(client_version, **args)

View File

@ -0,0 +1,108 @@
# 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
# Need to register trove_client
from karbor.services.protection.clients import trove # noqa
from karbor.services.protection.protectable_plugins.database \
import DatabaseInstanceProtectablePlugin
from karbor.tests import base
import mock
from oslo_config import cfg
from troveclient.v1 import instances
class DatabaseInstanceProtectablePluginTest(base.TestCase):
def setUp(self):
super(DatabaseInstanceProtectablePluginTest, self).setUp()
service_catalog = [
{'type': 'database',
'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('trove_endpoint',
'http://127.0.0.1:8774/v2.1',
'trove_client')
plugin = DatabaseInstanceProtectablePlugin(self._context)
self.assertEqual(
'http://127.0.0.1:8774/v2.1/abcd',
plugin._client(self._context).client.management_url)
def test_create_client_by_catalog(self):
plugin = DatabaseInstanceProtectablePlugin(self._context)
self.assertEqual(
'http://127.0.0.1:8774/v2.1/abcd',
plugin._client(self._context).client.management_url)
def test_get_resource_type(self):
plugin = DatabaseInstanceProtectablePlugin(self._context)
self.assertEqual("OS::Trove::Instance", plugin.get_resource_type())
def test_get_parent_resource_types(self):
plugin = DatabaseInstanceProtectablePlugin(self._context)
self.assertEqual(("OS::Keystone::Project", ),
plugin.get_parent_resource_types())
@mock.patch.object(instances.Instances, 'list')
def test_list_resources(self, mock_instance_list):
plugin = DatabaseInstanceProtectablePlugin(self._context)
instance_info = collections.namedtuple('instance_info', ['id', 'name',
'status'])
mock_instance_list.return_value = [
instance_info(id='123', name='name123', status='ACTIVE'),
instance_info(id='456', name='name456', status='ACTIVE')]
self.assertEqual([Resource('OS::Trove::Instance', '123', 'name123'),
Resource('OS::Trove::Instance', '456', 'name456')],
plugin.list_resources(self._context))
@mock.patch.object(instances.Instances, 'get')
def test_show_resource(self, mock_instance_get):
plugin = DatabaseInstanceProtectablePlugin(self._context)
instance_info = collections.namedtuple(
'instance_info', ['id', 'name', 'status'])
mock_instance_get.return_value = instance_info(
id='123', name='name123', status='ACTIVE')
self.assertEqual(Resource('OS::Trove::Instance', '123', 'name123'),
plugin.show_resource(self._context, '123'))
@mock.patch.object(instances.Instances, 'list')
def test_get_dependent_resources(self, mock_instance_list):
plugin = DatabaseInstanceProtectablePlugin(self._context)
instance_info = collections.namedtuple(
'instance_info', ['id', 'name', 'status', 'project_id'])
project_info = collections.namedtuple(
'project_info', ['id', 'name', 'status'])
mock_instance_list.return_value = [
instance_info(id='123', name='name123', status='ACTIVE',
project_id='abcd'),
instance_info(id='456', name='name456', status='ACTIVE',
project_id='abcd')]
project = project_info(id='abcd', name='name456', status='available')
self.assertEqual([Resource('OS::Trove::Instance', '123', 'name123'),
Resource('OS::Trove::Instance', '456', 'name456')],
plugin.get_dependent_resources(
self._context, project))

View File

@ -27,6 +27,7 @@ PasteDeploy>=1.5.0 # MIT
python-glanceclient>=2.7.0 # Apache-2.0
python-novaclient>=9.0.0 # Apache-2.0
python-cinderclient>=2.1.0 # Apache-2.0
python-troveclient>=2.2.0 # Apache-2.0
requests>=2.14.2 # Apache-2.0
Routes>=2.3.1 # MIT
python-neutronclient>=6.3.0 # Apache-2.0

View File

@ -60,6 +60,7 @@ karbor.protectables =
image = karbor.services.protection.protectable_plugins.image:ImageProtectablePlugin
share = karbor.services.protection.protectable_plugins.share:ShareProtectablePlugin
network = karbor.services.protection.protectable_plugins.network:NetworkProtectablePlugin
database = karbor.services.protection.protectable_plugins.database:DatabaseInstanceProtectablePlugin
karbor.operationengine.engine.timetrigger.time_format =
crontab = karbor.services.operationengine.engine.triggers.timetrigger.timeformats.crontab_time:Crontab
calendar = karbor.services.operationengine.engine.triggers.timetrigger.timeformats.calendar_time:ICal