From 0a46c8542817b3f4a5f88e298696072f4708671a Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 11 Jun 2015 02:05:22 -0700 Subject: [PATCH] Add startup hook after pecan init for plugins Add a startup method to initialize all of the plugins after the pecan app is configured but before it is returned to guaruntee that everything is ready before the app starts taking requests. Change-Id: Id18ac5ab979b5f2cb49b69d8650248010e02fe76 Partially-Implements: blueprint wsgi-pecan-switch --- neutron/newapi/app.py | 3 +- neutron/newapi/startup.py | 37 +++++++++++++++++++ .../functional/newapi/test_functional.py | 4 ++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 neutron/newapi/startup.py diff --git a/neutron/newapi/app.py b/neutron/newapi/app.py index 517f2690e..448896cd9 100644 --- a/neutron/newapi/app.py +++ b/neutron/newapi/app.py @@ -19,7 +19,7 @@ from oslo_middleware import request_id import pecan from neutron.common import exceptions as n_exc - +from neutron.newapi import startup CONF = cfg.CONF CONF.import_opt('bind_host', 'neutron.common.config') @@ -50,6 +50,7 @@ def setup_app(*args, **kwargs): hooks=app_hooks, guess_content_type_from_ext=True ) + startup.initialize_all() return app diff --git a/neutron/newapi/startup.py b/neutron/newapi/startup.py new file mode 100644 index 000000000..e2e3f7fa2 --- /dev/null +++ b/neutron/newapi/startup.py @@ -0,0 +1,37 @@ +# Copyright (c) 2015 Mirantis, Inc. +# All Rights Reserved. +# +# 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 neutron.api import extensions +from neutron.api.v2 import attributes +from neutron import policy + + +def initialize_all(): + ext_mgr = extensions.PluginAwareExtensionManager.get_instance() + ext_mgr.extend_resources("2.0", attributes.RESOURCE_ATTRIBUTE_MAP) + for ext in ext_mgr.extensions.values(): + # make each extension populate its plurals + if hasattr(ext, 'get_resources'): + ext.get_resources() + if hasattr(ext, 'get_extended_resources'): + ext.get_extended_resources('v2.0') + # Certain policy checks require that the extensions are loaded + # and the RESOURCE_ATTRIBUTE_MAP populated before they can be + # properly initialized. This can only be claimed with certainty + # once this point in the code has been reached. In the event + # that the policies have been initialized before this point, + # calling reset will cause the next policy check to + # re-initialize with all of the required data in place. + policy.reset() diff --git a/neutron/tests/functional/newapi/test_functional.py b/neutron/tests/functional/newapi/test_functional.py index 0dde0bf9f..473a7b007 100644 --- a/neutron/tests/functional/newapi/test_functional.py +++ b/neutron/tests/functional/newapi/test_functional.py @@ -22,6 +22,7 @@ from pecan.testing import load_test_app import testtools from neutron.common import exceptions as n_exc +from neutron import manager from neutron.tests.unit import testlib_api @@ -64,6 +65,9 @@ class TestV2Controller(PecanFunctionalTest): response = self.app.delete('/v2.0/ports/44.json') self.assertEqual(response.status_int, 200) + def test_plugin_initialized(self): + self.assertIsNotNone(manager.NeutronManager._instance) + class TestErrors(PecanFunctionalTest): -- 2.45.2