General Plugin Cleanups

Begin cleaning up all plugins to follow similar models/patterns.

Change-Id: I6dd31ade4de310a4db9487a628b20d25a133bb97
This commit is contained in:
Kiall Mac Innes 2014-02-10 10:55:31 +00:00
parent a2934600e8
commit 2a15d52f3d
15 changed files with 165 additions and 146 deletions

View File

@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from designate.openstack.common import log as logging
from designate.plugin import Plugin
from designate.backend.base import Backend
LOG = logging.getLogger(__name__)
@ -22,9 +22,6 @@ LOG = logging.getLogger(__name__)
def get_backend(backend_driver, central_service):
LOG.debug("Loading backend driver: %s" % backend_driver)
invoke_kwds = {
'central_service': central_service
}
cls = Backend.get_driver(backend_driver)
return Plugin.get_plugin(backend_driver, ns=__name__, invoke_on_load=True,
invoke_kwds=invoke_kwds)
return cls(central_service=central_service)

View File

@ -17,13 +17,13 @@ import abc
from designate.openstack.common import log as logging
from designate import exceptions
from designate.context import DesignateContext
from designate.plugin import Plugin
from designate.plugin import DriverPlugin
LOG = logging.getLogger(__name__)
class Backend(Plugin):
class Backend(DriverPlugin):
""" Base class for backend implementations """
__plugin_type__ = 'backend'
__plugin_ns__ = 'designate.backend'
@ -34,6 +34,12 @@ class Backend(Plugin):
self.admin_context = DesignateContext.get_admin_context()
self.admin_context.all_tenants = True
def start(self):
pass
def stop(self):
pass
def create_tsigkey(self, context, tsigkey):
""" Create a TSIG Key """
raise exceptions.NotImplemented(

View File

@ -68,7 +68,7 @@ class Service(rpc_service.Service):
# Get a quota manager instance
self.quota = quota.get_quota()
self.network_api = network_api.get_api(cfg.CONF.network_api)
self.network_api = network_api.get_network_api(cfg.CONF.network_api)
def start(self):
# Check to see if there are any TLDs in the database

View File

@ -13,103 +13,20 @@
# 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 dns import reversename
import logging
from oslo.config import cfg
from stevedore import driver
from designate import exceptions
from designate.openstack.common import log as logging
from designate.network_api.base import NetworkAPI
LOG = logging.getLogger(__name__)
cfg.CONF.register_opts([
cfg.StrOpt('network_api', default='neutron', help='Which API to use.')
])
LOG = logging.getLogger()
def get_network_api(network_api_driver):
LOG.debug("Loading network_api driver: %s" % network_api_driver)
cls = NetworkAPI.get_driver(network_api_driver)
def get_api(api):
mngr = driver.DriverManager('designate.network_api', api)
return mngr.driver()
class BaseAPI(object):
"""
Base API
"""
def _endpoints(self, service_catalog=None, service_type=None,
endpoint_type='publicURL', config_section=None,
region=None):
if service_catalog is not None:
endpoints = self._endpoints_from_catalog(
service_catalog, service_type, endpoint_type,
region=region)
elif config_section is not None:
endpoints = []
for u in cfg.CONF[config_section].endpoints:
e_region, e = u.split('|')
# Filter if region is given
if (e_region and region) and e_region != region:
continue
endpoints.append((e, e_region))
if not endpoints:
msg = 'Endpoints are not configured'
raise exceptions.ConfigurationError(msg)
else:
msg = 'No service_catalog and no configured endpoints'
raise exceptions.ConfigurationError(msg)
LOG.debug('Returning endpoints: %s' % endpoints)
return endpoints
def _endpoints_from_catalog(self, service_catalog, service_type,
endpoint_type, region=None):
"""
Return the endpoints for the given service from the context's sc
or lookup towards the configured keystone.
return [('http://endpoint', 'region')]
"""
urls = []
for svc in service_catalog:
if svc['type'] != service_type:
continue
for url in svc['endpoints']:
if endpoint_type in url:
if region is not None and url['region'] != region:
continue
urls.append((url[endpoint_type], url['region']))
if not urls:
raise exceptions.NetworkEndpointNotFound
return urls
def list_floatingips(self, context, region=None):
"""
List Floating IPs.
Should return something like:
[{
'address': '<ip address'>,
'region': '<region where this belongs>',
'id': '<id of the FIP>'
}]
"""
raise NotImplementedError
@staticmethod
def address_zone(address):
"""
Get the zone a address belongs to.
"""
parts = reversed(address.split('.')[:-1])
return '%s.in-addr.arpa.' % ".".join(parts)
@staticmethod
def address_name(address):
"""
Get the name for the address
"""
return reversename.from_address(address).to_text()
return cls()

View File

@ -0,0 +1,108 @@
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Author: Endre Karlson <endre.karlson@hp.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.
from dns import reversename
from oslo.config import cfg
from designate import exceptions
from designate.plugin import DriverPlugin
from designate.openstack.common import log as logging
LOG = logging.getLogger(__name__)
class NetworkAPI(DriverPlugin):
"""
Base API
"""
__plugin_ns__ = 'designate.network_api'
__plugin_type__ = 'network_api'
def _endpoints(self, service_catalog=None, service_type=None,
endpoint_type='publicURL', config_section=None,
region=None):
if service_catalog is not None:
endpoints = self._endpoints_from_catalog(
service_catalog, service_type, endpoint_type,
region=region)
elif config_section is not None:
endpoints = []
for u in cfg.CONF[config_section].endpoints:
e_region, e = u.split('|')
# Filter if region is given
if (e_region and region) and e_region != region:
continue
endpoints.append((e, e_region))
if not endpoints:
msg = 'Endpoints are not configured'
raise exceptions.ConfigurationError(msg)
else:
msg = 'No service_catalog and no configured endpoints'
raise exceptions.ConfigurationError(msg)
LOG.debug('Returning endpoints: %s' % endpoints)
return endpoints
def _endpoints_from_catalog(self, service_catalog, service_type,
endpoint_type, region=None):
"""
Return the endpoints for the given service from the context's sc
or lookup towards the configured keystone.
return [('http://endpoint', 'region')]
"""
urls = []
for svc in service_catalog:
if svc['type'] != service_type:
continue
for url in svc['endpoints']:
if endpoint_type in url:
if region is not None and url['region'] != region:
continue
urls.append((url[endpoint_type], url['region']))
if not urls:
raise exceptions.NetworkEndpointNotFound
return urls
def list_floatingips(self, context, region=None):
"""
List Floating IPs.
Should return something like:
[{
'address': '<ip address'>,
'region': '<region where this belongs>',
'id': '<id of the FIP>'
}]
"""
raise NotImplementedError
@staticmethod
def address_zone(address):
"""
Get the zone a address belongs to.
"""
parts = reversed(address.split('.')[:-1])
return '%s.in-addr.arpa.' % ".".join(parts)
@staticmethod
def address_name(address):
"""
Get the name for the address
"""
return reversename.from_address(address).to_text()

View File

@ -15,7 +15,7 @@
# under the License.
import uuid
from designate.openstack.common import log
from designate.network_api import BaseAPI
from designate.network_api.base import NetworkAPI
LOG = log.getLogger(__name__)
@ -66,7 +66,9 @@ def reset_floatingips():
POOL[key] = allocated.pop(key)
class API(BaseAPI):
class FakeNetworkAPI(NetworkAPI):
__plugin_name__ = 'fake'
def list_floatingips(self, context, region=None):
if context.is_admin:
data = []

View File

@ -25,7 +25,7 @@ from designate import exceptions
from designate.openstack.common import log as logging
from designate.openstack.common import threadgroup
from designate.network_api import BaseAPI
from designate.network_api.base import NetworkAPI
CONF = cfg.CONF
@ -85,10 +85,12 @@ def get_client(context, endpoint):
return clientv20.Client(**params)
class API(BaseAPI):
class NeutronNetworkAPI(NetworkAPI):
"""
Interact with the Neutron API
"""
__plugin_name__ = 'neutron'
def list_floatingips(self, context, region=None):
"""
Get floating ips based on the current context from Neutron

View File

@ -33,30 +33,6 @@ class Plugin(object):
self.name = self.get_canonical_name()
LOG.debug("Loaded plugin %s", self.name)
def is_enabled(self):
"""
Is this Plugin enabled?
:retval: Boolean
"""
return True
@classmethod
def get_plugin(cls, name, ns=None, invoke_on_load=False,
invoke_args=(), invoke_kwds={}):
"""
Load a plugin from namespace
"""
ns = ns or cls.__plugin_ns__
if ns is None:
raise RuntimeError('No namespace provided or __plugin_ns__ unset')
LOG.debug('Looking for plugin %s in %s', name, ns)
mgr = driver.DriverManager(ns, name)
return mgr.driver(*invoke_args, **invoke_kwds) if invoke_on_load \
else mgr.driver
@classmethod
def get_canonical_name(cls):
"""
@ -74,12 +50,14 @@ class Plugin(object):
def get_plugin_type(cls):
return cls.__plugin_type__
def start(self):
"""
Start this plugin
"""
def stop(self):
"""
Stop this plugin from doing anything
"""
class DriverPlugin(Plugin):
@classmethod
def get_driver(cls, name):
""" Load a driver """
LOG.debug('Looking for driver %s in %s', name, cls.__plugin_ns__)
mgr = driver.DriverManager(cls.__plugin_ns__, name)
return mgr.driver

View File

@ -34,4 +34,10 @@ cfg.CONF.register_opts([
def get_quota():
return Quota.get_plugin(cfg.CONF.quota_driver, invoke_on_load=True)
quota_driver = cfg.CONF.quota_driver
LOG.debug("Loading quota driver: %s" % quota_driver)
cls = Quota.get_driver(quota_driver)
return cls()

View File

@ -16,10 +16,10 @@
import abc
from oslo.config import cfg
from designate import exceptions
from designate.plugin import Plugin
from designate.plugin import DriverPlugin
class Quota(Plugin):
class Quota(DriverPlugin):
""" Base class for quota plugins """
__metaclass__ = abc.ABCMeta
__plugin_ns__ = 'designate.quota'

View File

@ -23,4 +23,7 @@ LOG = logging.getLogger(__name__)
def get_storage():
""" Return the engine class from the provided engine name """
storage_driver = cfg.CONF['service:central'].storage_driver
return Storage.get_plugin(storage_driver, invoke_on_load=True)
cls = Storage.get_driver(storage_driver)
return cls()

View File

@ -14,10 +14,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import abc
from designate.plugin import Plugin
from designate.plugin import DriverPlugin
class Storage(Plugin):
class Storage(DriverPlugin):
""" Base class for storage plugins """
__metaclass__ = abc.ABCMeta
__plugin_ns__ = 'designate.storage'

View File

@ -122,7 +122,7 @@ class DatabaseFixture(fixtures.Fixture):
class NetworkAPIFixture(fixtures.Fixture):
def setUp(self):
super(NetworkAPIFixture, self).setUp()
self.api = network_api.get_api(cfg.CONF.network_api)
self.api = network_api.get_network_api(cfg.CONF.network_api)
self.fake = fake_network_api
self.addCleanup(self.fake.reset_floatingips)

View File

@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from designate import exceptions
from designate.network_api import get_api
from designate.network_api import get_network_api
from designate.tests import TestCase
from neutronclient.v2_0 import client as clientv20
@ -33,7 +33,7 @@ class NeutronAPITest(TestCase):
super(NeutronAPITest, self).setUp()
self.config(endpoints=['RegionOne|http://localhost:9696'],
group='network_api:neutron')
self.api = get_api('neutron')
self.api = get_network_api('neutron')
@patch.object(clientv20.Client, 'list_floatingips',
side_effect=neutron_exceptions.Unauthorized)

View File

@ -69,8 +69,8 @@ designate.backend =
multi = designate.backend.impl_multi:MultiBackend
designate.network_api =
fake = designate.network_api.fake:API
neutron = designate.network_api.neutron:API
fake = designate.network_api.fake:FakeNetworkAPI
neutron = designate.network_api.neutron:NeutronNetworkAPI
designate.quota =
noop = designate.quota.impl_noop:NoopQuota