Prepare for refactoring clients used in protect service

This patch has done the preparation works for refactoring clients,
such as initiate keystone plugin, generate session etc. There are
several patches to do for refactoring the clients.

Change-Id: I61d2a42fb6f0de2d98714a7e19c1a220dec95082
Implements: blueprint refactor-clients
Partial-Bug: #1566793
This commit is contained in:
zengchen 2016-11-30 16:41:03 +08:00 committed by chenying
parent e12313b5b8
commit 6386bf5bf5
18 changed files with 118 additions and 28 deletions

View File

@ -55,6 +55,7 @@ class KarborKeystonePlugin(object):
self._client = None
self._auth_uri = ""
self._karbor_user_id = ""
self._service_auth_plugin = None
self._do_init()
@ -64,6 +65,7 @@ class KarborKeystonePlugin(object):
auth_plugin._project_name = "service"
auth_plugin._project_domain_id = "default"
self._service_auth_plugin = auth_plugin
self._client = self._get_keystone_client(auth_plugin)
lcfg = CONF[TRUSTEE_CONF_GROUP]
@ -76,6 +78,10 @@ class KarborKeystonePlugin(object):
msg = 'get keystone auth url failed'
raise exception.AuthorizationFailure(obj=msg)
@property
def service_auth_plugin(self):
return self._service_auth_plugin
def get_service_endpoint(self, service_name, service_type,
region_id, interface='public'):
try:
@ -96,7 +102,7 @@ class KarborKeystonePlugin(object):
msg = ('get service(%s) endpoint failed' % service_name)
raise exception.AuthorizationFailure(obj=msg)
def create_trust_to_karbor(self, context):
def create_user_auth_plugin(self, context):
if not context.auth_token_info:
msg = ("user=%s, project=%s" % (context.user_id,
context.project_id))
@ -104,9 +110,12 @@ class KarborKeystonePlugin(object):
auth_ref = access.create(body=context.auth_token_info,
auth_token=context.auth_token)
user_auth_plugin = access_plugin.AccessInfoPlugin(
return access_plugin.AccessInfoPlugin(
auth_url=self._auth_uri, auth_ref=auth_ref)
l_kc_v3 = self._get_keystone_client(user_auth_plugin)
def create_trust_to_karbor(self, context):
l_kc_v3 = self._get_keystone_client(
self.create_user_auth_plugin(context))
try:
trust = l_kc_v3.trusts.create(trustor_user=context.user_id,
trustee_user=self._karbor_user_id,

View File

@ -10,18 +10,24 @@
# License for the specific language governing permissions and limitations
# under the License.
from karbor.i18n import _LE
import os
from keystoneauth1 import service_token
from keystoneauth1 import session as keystone_session
import os
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
from karbor.common import karbor_keystone_plugin
from karbor import exception
from karbor.i18n import _LE
LOG = logging.getLogger(__name__)
class ClientFactory(object):
_factory = None
_keystone_plugin = None
@staticmethod
def _list_clients():
@ -32,16 +38,50 @@ class ClientFactory(object):
for file in os.listdir(clients_dir):
name, ext = os.path.splitext(file)
if name != '__init__' and ext == '.py':
if name != '__init__' and name != 'utils' and ext == '.py':
LOG.debug('Found client "%s"', name)
yield '%s.clients.%s' % (__package__, name)
@classmethod
def create_client(cls, service, context, conf=cfg.CONF, **kwargs):
if not cls._factory:
cls._factory = {}
for module in cls._list_clients():
module = importutils.import_module(module)
cls._factory[module.SERVICE] = module
def _generate_session(cls, context, service):
plugin = cls._keystone_plugin
try:
auth_plugin = service_token.ServiceTokenAuthWrapper(
plugin.create_user_auth_plugin(context),
plugin.service_auth_plugin)
except Exception:
return None
return cls._factory[service].create(context, conf, **kwargs)
try:
client_conf = cfg.CONF['%s_client' % service]
auth_insecure = client_conf['%s_auth_insecure' % service]
ca_file = client_conf['%s_ca_cert_file' % service]
verify = False if auth_insecure else (ca_file or True)
except Exception:
verify = True
return keystone_session.Session(auth=auth_plugin, verify=verify)
@classmethod
def init(cls):
cls._keystone_plugin = karbor_keystone_plugin.KarborKeystonePlugin()
cls._factory = {}
for module in cls._list_clients():
module = importutils.import_module(module)
cls._factory[module.SERVICE] = module
@classmethod
def create_client(cls, service, context, conf=cfg.CONF, **kwargs):
module = cls._factory.get(service)
if module is None:
raise exception.KarborException(_('Unknown service(%s)') % service)
kwargs['keystone_plugin'] = cls._keystone_plugin
kwargs['session'] = cls._generate_session(context, service)
return module.create(context, conf, **kwargs)
def init():
ClientFactory.init()

View File

@ -14,7 +14,7 @@ from cinderclient import client as cc
from oslo_config import cfg
from oslo_log import log as logging
from karbor.i18n import _LI, _LE
from karbor.services.protection import utils
from karbor.services.protection.clients import utils
LOG = logging.getLogger(__name__)
@ -43,7 +43,7 @@ cfg.CONF.register_opts(cinder_client_opts, group=SERVICE + '_client')
CINDERCLIENT_VERSION = '3'
def create(context, conf):
def create(context, conf, **kwargs):
conf.register_opts(cinder_client_opts, group=SERVICE + '_client')
try:
url = utils.get_url(SERVICE, context, conf,

View File

@ -14,7 +14,7 @@ from glanceclient import client as gc
from oslo_config import cfg
from oslo_log import log as logging
from karbor.i18n import _LE, _LI
from karbor.services.protection import utils
from karbor.services.protection.clients import utils
LOG = logging.getLogger(__name__)
@ -43,7 +43,7 @@ cfg.CONF.register_opts(glance_client_opts, group=SERVICE + '_client')
GLANCECLIENT_VERSION = '2'
def create(context, conf):
def create(context, conf, **kwargs):
conf.register_opts(glance_client_opts, group=SERVICE + '_client')
try:
url = utils.get_url(SERVICE, context, conf)

View File

@ -16,7 +16,7 @@ from oslo_config import cfg
from oslo_log import log as logging
from karbor.i18n import _LE, _LI
from karbor.services.protection import utils
from karbor.services.protection.clients import utils
LOG = logging.getLogger(__name__)

View File

@ -14,7 +14,7 @@ from neutronclient.v2_0 import client as neutron_client
from oslo_config import cfg
from oslo_log import log as logging
from karbor.i18n import _LE, _LI
from karbor.services.protection import utils
from karbor.services.protection.clients import utils
LOG = logging.getLogger(__name__)
@ -41,7 +41,7 @@ neutron_client_opts = [
cfg.CONF.register_opts(neutron_client_opts, group=SERVICE + '_client')
def create(context, conf):
def create(context, conf, **kwargs):
conf.register_opts(neutron_client_opts, group=SERVICE + '_client')
try:
url = utils.get_url(SERVICE, context, conf)

View File

@ -14,7 +14,7 @@ from novaclient import client as nc
from oslo_config import cfg
from oslo_log import log as logging
from karbor.i18n import _LI, _LE
from karbor.services.protection import utils
from karbor.services.protection.clients import utils
LOG = logging.getLogger(__name__)
@ -44,7 +44,7 @@ cfg.CONF.register_opts(nova_client_opts, group=SERVICE + '_client')
NOVACLIENT_VERSION = '2'
def create(context, conf):
def create(context, conf, **kwargs):
conf.register_opts(nova_client_opts, group=SERVICE + '_client')
try:
url = utils.get_url(SERVICE, context, conf,

View File

@ -62,7 +62,7 @@ def register_opts(conf):
conf.register_opts(swift_client_opts, group=SERVICE + '_client')
def create(context, conf):
def create(context, conf, **kwargs):
register_opts(conf)
client_config = conf.swift_client

View File

@ -28,6 +28,7 @@ from karbor import exception
from karbor.i18n import _, _LI, _LE
from karbor import manager
from karbor.resource import Resource
from karbor.services.protection import client_factory
from karbor.services.protection.flows import worker as flow_manager
from karbor.services.protection.protectable_registry import ProtectableRegistry
from karbor import utils
@ -61,6 +62,9 @@ class ProtectionManager(manager.Manager):
def __init__(self, service_name=None,
*args, **kwargs):
super(ProtectionManager, self).__init__(*args, **kwargs)
client_factory.init()
provider_reg = CONF.provider_registry
self.provider_registry = utils.load_plugin(PROVIDER_NAMESPACE,
provider_reg)

View File

@ -15,6 +15,7 @@ from glanceclient.v2 import images
from karbor.common import constants
from karbor.context import RequestContext
from karbor import resource
from karbor.services.protection import client_factory
from karbor.services.protection.protectable_plugins.image import \
ImageProtectablePlugin
from karbor.tests import base
@ -33,6 +34,12 @@ project_info = namedtuple('project_info', field_names=['id', 'type', 'name'])
class ImageProtectablePluginTest(base.TestCase):
def setUp(self):
super(ImageProtectablePluginTest, self).setUp()
with mock.patch.object(
client_factory.karbor_keystone_plugin.KarborKeystonePlugin,
'_do_init'):
client_factory.init()
service_catalog = [{
'type': 'image',
'endpoints': [{'publicURL': 'http://127.0.0.1:9292'}]

View File

@ -13,6 +13,7 @@
import collections
from karbor.context import RequestContext
from karbor.resource import Resource
from karbor.services.protection import client_factory
from karbor.services.protection.protectable_plugins.server \
import ServerProtectablePlugin
@ -25,6 +26,11 @@ from oslo_config import cfg
class ServerProtectablePluginTest(base.TestCase):
def setUp(self):
super(ServerProtectablePluginTest, self).setUp()
with mock.patch.object(
client_factory.karbor_keystone_plugin.KarborKeystonePlugin,
'_do_init'):
client_factory.init()
service_catalog = [
{'type': 'compute',
'endpoints': [{'publicURL': 'http://127.0.0.1:8774/v2.1/abcd'}],

View File

@ -17,6 +17,7 @@ import mock
from karbor.common import constants
from karbor.context import RequestContext
from karbor.resource import Resource
from karbor.services.protection import client_factory
from karbor.services.protection.protectable_plugins.volume \
import VolumeProtectablePlugin
@ -30,6 +31,12 @@ vol_info = namedtuple('vol_info', ['id', 'attachments', 'name'])
class VolumeProtectablePluginTest(base.TestCase):
def setUp(self):
super(VolumeProtectablePluginTest, self).setUp()
with mock.patch.object(
client_factory.karbor_keystone_plugin.KarborKeystonePlugin,
'_do_init'):
client_factory.init()
service_catalog = [
{'type': 'volumev3',
'endpoints': [{'publicURL': 'http://127.0.0.1:8776/v3/abcd'}],

View File

@ -18,7 +18,7 @@ from karbor.context import RequestContext
from karbor import exception
from karbor.resource import Resource
from karbor.services.protection import bank_plugin
from karbor.services.protection.client_factory import ClientFactory
from karbor.services.protection import client_factory
from karbor.services.protection.protection_plugins.volume. \
cinder_protection_plugin import CinderBackupProtectionPlugin
from karbor.services.protection.protection_plugins.volume \
@ -91,6 +91,10 @@ class BackupResponse(object):
class CinderProtectionPluginTest(base.TestCase):
def setUp(self):
super(CinderProtectionPluginTest, self).setUp()
cls = client_factory.karbor_keystone_plugin.KarborKeystonePlugin
with mock.patch.object(cls, '_do_init'):
client_factory.init()
self.plugin = CinderBackupProtectionPlugin()
cfg.CONF.set_default('cinder_endpoint',
'http://127.0.0.1:8776/v2',
@ -99,7 +103,8 @@ class CinderProtectionPluginTest(base.TestCase):
self.cntxt = RequestContext(user_id='admin',
project_id='abcd',
auth_token='efgh')
self.cinder_client = ClientFactory.create_client("cinder", self.cntxt)
self.cinder_client = client_factory.ClientFactory.create_client(
"cinder", self.cntxt)
def _get_checkpoint(self):
fake_bank = bank_plugin.Bank(fakes.FakeBankPlugin())

View File

@ -17,7 +17,7 @@ from karbor.resource import Resource
from karbor.services.protection.bank_plugin import Bank
from karbor.services.protection.bank_plugin import BankPlugin
from karbor.services.protection.bank_plugin import BankSection
from karbor.services.protection.client_factory import ClientFactory
from karbor.services.protection import client_factory
from karbor.services.protection.protection_plugins. \
image.image_protection_plugin import GlanceProtectionPlugin
from karbor.services.protection.protection_plugins.image \
@ -76,6 +76,10 @@ class CheckpointCollection(object):
class GlanceProtectionPluginTest(base.TestCase):
def setUp(self):
super(GlanceProtectionPluginTest, self).setUp()
cls = client_factory.karbor_keystone_plugin.KarborKeystonePlugin
with mock.patch.object(cls, '_do_init'):
client_factory.init()
self.plugin = GlanceProtectionPlugin()
cfg.CONF.set_default('glance_endpoint',
'http://127.0.0.1:9292',
@ -84,7 +88,8 @@ class GlanceProtectionPluginTest(base.TestCase):
self.cntxt = RequestContext(user_id='admin',
project_id='abcd',
auth_token='efgh')
self.glance_client = ClientFactory.create_client("glance", self.cntxt)
self.glance_client = client_factory.ClientFactory.create_client(
"glance", self.cntxt)
self.checkpoint = CheckpointCollection()
def test_get_options_schema(self):

View File

@ -37,7 +37,8 @@ class ProtectionServiceTest(base.TestCase):
flow_manager.Worker._load_engine = mock.Mock()
flow_manager.Worker._load_engine.return_value = fakes.FakeFlowEngine()
super(ProtectionServiceTest, self).setUp()
self.pro_manager = manager.ProtectionManager()
with mock.patch('karbor.services.protection.client_factory.init'):
self.pro_manager = manager.ProtectionManager()
self.protection_plan = fakes.fake_protection_plan()
@mock.patch.object(protectable_registry.ProtectableRegistry,

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from karbor.services.protection import client_factory
from karbor.services.protection.clients import swift
from karbor.tests import base
from karbor.tests.unit.protection.fake_swift_client import FakeSwiftClient
@ -33,6 +34,10 @@ class FakeConf(object):
class SwiftBankPluginTest(base.TestCase):
def setUp(self):
super(SwiftBankPluginTest, self).setUp()
cls = client_factory.karbor_keystone_plugin.KarborKeystonePlugin
with mock.patch.object(cls, '_do_init'):
client_factory.init()
self.conf = FakeConf()
self.fake_connection = FakeSwiftClient.connection()
swift.create = mock.MagicMock()

View File

@ -8,6 +8,7 @@ croniter>=0.3.4 # MIT License
eventlet!=0.18.3,>=0.18.2 # MIT
greenlet>=0.3.2 # MIT
icalendar>=3.10 # BSD
keystoneauth1>=2.18.0 # Apache-2.0
keystonemiddleware>=4.12.0 # Apache-2.0
oslo.config!=3.18.0,>=3.14.0 # Apache-2.0
oslo.concurrency>=3.8.0 # Apache-2.0