Add service and core factory classes

Change-Id: Ic5bbe30ec13cd8ee95c4aad0dc58f53fa1d6d159
This commit is contained in:
Frédéric Guillot 2016-12-21 11:27:37 -05:00
parent e95a61f402
commit 7fd58f7d8f
7 changed files with 188 additions and 42 deletions

View File

@ -12,21 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from flask import Flask
from oslo_log import log
import sys
from almanach.api import auth_adapter
from almanach.api.v1 import routes
from almanach.core.controllers import application_controller
from almanach.core.controllers import entity_controller
from almanach.core.controllers import instance_controller
from almanach.core.controllers import volume_controller
from almanach.core.controllers import volume_type_controller
from almanach.core import factory as core_factory
from almanach.core import opts
from almanach.storage import storage_driver
LOG = log.getLogger(__name__)
@ -38,15 +31,13 @@ def main():
try:
opts.CONF(sys.argv[1:])
config = opts.CONF
factory = core_factory.Factory(config)
database_driver = storage_driver.StorageDriver(config).get_database_driver()
database_driver.connect()
routes.instance_ctl = instance_controller.InstanceController(config, database_driver)
routes.volume_ctl = volume_controller.VolumeController(config, database_driver)
routes.volume_type_ctl = volume_type_controller.VolumeTypeController(database_driver)
routes.entity_ctl = entity_controller.EntityController(database_driver)
routes.app_ctl = application_controller.ApplicationController(database_driver)
routes.instance_ctl = factory.get_instance_controller()
routes.volume_ctl = factory.get_volume_controller()
routes.volume_type_ctl = factory.get_volume_type_controller()
routes.entity_ctl = factory.get_entity_controller()
routes.app_ctl = factory.get_application_controller()
routes.auth_adapter = auth_adapter.AuthenticationAdapter(config).get_authentication_adapter()
LOG.info('Listening on %s:%d', config.api.bind_ip, config.api.bind_port)

View File

@ -16,17 +16,9 @@ from oslo_log import log
from oslo_service import service
import sys
from almanach.collector.handlers import instance_handler
from almanach.collector.handlers import volume_handler
from almanach.collector.handlers import volume_type_handler
from almanach.collector import messaging
from almanach.collector import notification
from almanach.collector import service as collector_service
from almanach.core.controllers import instance_controller
from almanach.core.controllers import volume_controller
from almanach.core.controllers import volume_type_controller
from almanach.core import factory as core_factory
from almanach.core import opts
from almanach.storage import storage_driver
LOG = log.getLogger(__name__)
@ -35,22 +27,8 @@ def main():
opts.CONF(sys.argv[1:])
config = opts.CONF
database_driver = storage_driver.StorageDriver(config).get_database_driver()
database_driver.connect()
messaging_factory = messaging.MessagingFactory(config)
instance_ctl = instance_controller.InstanceController(config, database_driver)
volume_ctl = volume_controller.VolumeController(config, database_driver)
volume_type_ctl = volume_type_controller.VolumeTypeController(database_driver)
notification_handler = notification.NotificationHandler(config, messaging_factory)
notification_handler.add_event_handler(instance_handler.InstanceHandler(instance_ctl))
notification_handler.add_event_handler(volume_handler.VolumeHandler(volume_ctl))
notification_handler.add_event_handler(volume_type_handler.VolumeTypeHandler(volume_type_ctl))
listener = messaging_factory.get_listener(notification_handler)
launcher = service.launch(config, collector_service.CollectorService(listener))
service_factory = collector_service.ServiceFactory(config, core_factory.Factory(config))
launcher = service.launch(config, service_factory.get_service())
launcher.wait()
if __name__ == '__main__':

View File

@ -15,6 +15,12 @@
from oslo_log import log as logging
from oslo_service import service
from almanach.collector.handlers import instance_handler
from almanach.collector.handlers import volume_handler
from almanach.collector.handlers import volume_type_handler
from almanach.collector import messaging
from almanach.collector import notification
LOG = logging.getLogger(__name__)
@ -37,3 +43,30 @@ class CollectorService(service.ServiceBase):
def reset(self):
pass
class ServiceFactory(object):
def __init__(self, config, core_factory):
self.config = config
self.core_factory = core_factory
def get_service(self):
messaging_factory = messaging.MessagingFactory(self.config)
notification_handler = notification.NotificationHandler(self.config, messaging_factory)
notification_handler.add_event_handler(self._get_instance_handler())
notification_handler.add_event_handler(self._get_volume_handler())
notification_handler.add_event_handler(self._get_volume_type_handler())
listener = messaging_factory.get_listener(notification_handler)
return CollectorService(listener)
def _get_instance_handler(self):
return instance_handler.InstanceHandler(self.core_factory.get_instance_controller())
def _get_volume_handler(self):
return volume_handler.VolumeHandler(self.core_factory.get_volume_controller())
def _get_volume_type_handler(self):
return volume_type_handler.VolumeTypeHandler(self.core_factory.get_volume_type_controller())

49
almanach/core/factory.py Normal file
View File

@ -0,0 +1,49 @@
# Copyright 2016 Internap.
#
# 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 almanach.core.controllers import application_controller
from almanach.core.controllers import entity_controller
from almanach.core.controllers import instance_controller
from almanach.core.controllers import volume_controller
from almanach.core.controllers import volume_type_controller
from almanach.storage import storage_driver
class Factory(object):
def __init__(self, config, storage=None):
self.config = config
self.database_driver = None
self.storage_driver = storage or storage_driver.StorageDriver(self.config).get_database_driver()
def get_instance_controller(self):
return instance_controller.InstanceController(self.config, self._get_database_driver())
def get_volume_controller(self):
return volume_controller.VolumeController(self.config, self._get_database_driver())
def get_volume_type_controller(self):
return volume_type_controller.VolumeTypeController(self._get_database_driver())
def get_entity_controller(self):
return entity_controller.EntityController(self._get_database_driver())
def get_application_controller(self):
return application_controller.ApplicationController(self._get_database_driver())
def _get_database_driver(self):
if not self.database_driver:
self.database_driver = self.storage_driver
self.database_driver.connect()
return self.database_driver

View File

@ -0,0 +1,34 @@
# Copyright 2016 Internap.
#
# 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 almanach.collector import service
import mock
from almanach.tests.unit import base
class ServiceFactoryTest(base.BaseTestCase):
def setUp(self):
super(ServiceFactoryTest, self).setUp()
self.core_factory = mock.Mock()
self.factory = service.ServiceFactory(self.config, self.core_factory)
def test_get_service(self):
self.assertIsInstance(self.factory.get_service(),
service.CollectorService)
self.core_factory.get_instance_controller.assert_called_once()
self.core_factory.get_volume_controller.assert_called_once()
self.core_factory.get_volume_type_controller.assert_called_once()

View File

@ -0,0 +1,60 @@
# Copyright 2016 Internap.
#
# 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 almanach.core.controllers import application_controller
from almanach.core.controllers import entity_controller
from almanach.core.controllers import instance_controller
from almanach.core.controllers import volume_controller
from almanach.core.controllers import volume_type_controller
from almanach.core import factory
import mock
from almanach.tests.unit import base
class FactoryTest(base.BaseTestCase):
def setUp(self):
super(FactoryTest, self).setUp()
self.storage = mock.Mock()
self.factory = factory.Factory(self.config, self.storage)
def test_get_instance_controller(self):
self.assertIsInstance(self.factory.get_instance_controller(),
instance_controller.InstanceController)
def test_get_volume_controller(self):
self.assertIsInstance(self.factory.get_volume_controller(),
volume_controller.VolumeController)
def test_get_volume_type_controller(self):
self.assertIsInstance(self.factory.get_volume_type_controller(),
volume_type_controller.VolumeTypeController)
def test_get_entity_controller(self):
self.assertIsInstance(self.factory.get_entity_controller(),
entity_controller.EntityController)
def test_get_application_controller(self):
self.assertIsInstance(self.factory.get_application_controller(),
application_controller.ApplicationController)
def test_bootstrap_storage(self):
self.assertIsInstance(self.factory.get_instance_controller(),
instance_controller.InstanceController)
self.assertIsInstance(self.factory.get_volume_controller(),
volume_controller.VolumeController)
self.storage.connect.assert_called_once()

View File

@ -20,6 +20,7 @@ from almanach.tests.unit import base
class StorageDriverTest(base.BaseTestCase):
def setUp(self):
super(StorageDriverTest, self).setUp()
self.storage_driver = storage_driver.StorageDriver(self.config)