From 8ac7c65cffac34e412ab117f8e86b4dd2c973ac9 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Wed, 1 Dec 2021 00:43:43 +0900 Subject: [PATCH] Load api-paste.ini from configuration directories first The api-paste.ini file defines WSGI definition of API. This file is regarded as one configuration file and sometimes users need to modify the file, for example to enable an additional middleware. However currently aodh-api by default expects the file is located in the aodh source directory and users should set the [api] paste_config parameter in addition to copy and modify the file. This change makes the aodh-api process search the api-paste.ini file from configuration directories automatically, so that users can more easily customize the file by putting the modified file in /etc/aodh (or any other config directories). If the paste_config parameter is defined as a full path then aodh-api directly loads the file from the specified path. If the file is not found then the default file in the aodh source directory is used. Change-Id: I4a6194fbf6a5317a523d75e3fb6bd2fd055f5100 --- aodh/api/__init__.py | 6 +-- aodh/api/app.py | 6 ++- aodh/tests/functional/api/test_app.py | 38 ------------------- ...ini-from-config-dirs-69480861a9633df4.yaml | 8 ++++ 4 files changed, 13 insertions(+), 45 deletions(-) delete mode 100644 aodh/tests/functional/api/test_app.py create mode 100644 releasenotes/notes/load-api-paste-ini-from-config-dirs-69480861a9633df4.yaml diff --git a/aodh/api/__init__.py b/aodh/api/__init__.py index f2a76d59c..712d657cc 100644 --- a/aodh/api/__init__.py +++ b/aodh/api/__init__.py @@ -12,16 +12,12 @@ # 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 os - from oslo_config import cfg # Register options for the service OPTS = [ cfg.StrOpt('paste_config', - default=os.path.abspath( - os.path.join( - os.path.dirname(__file__), "api-paste.ini")), + default='api-paste.ini', help="Configuration file for WSGI definition of API."), cfg.StrOpt( 'auth_mode', diff --git a/aodh/api/app.py b/aodh/api/app.py index b7c090014..8588e32de 100644 --- a/aodh/api/app.py +++ b/aodh/api/app.py @@ -17,7 +17,6 @@ import os import uuid -from oslo_config import cfg from oslo_log import log from paste import deploy import pecan @@ -62,7 +61,10 @@ def load_app(conf): cfg_path = conf.find_file(cfg_path) if cfg_path is None or not os.path.exists(cfg_path): - raise cfg.ConfigFilesNotFoundError([conf.api.paste_config]) + LOG.debug("No api-paste configuration file found! Using default.") + cfg_path = os.path.abspath( + os.path.join( + os.path.dirname(__file__), "api-paste.ini")) config = dict(conf=conf) configkey = str(uuid.uuid4()) diff --git a/aodh/tests/functional/api/test_app.py b/aodh/tests/functional/api/test_app.py deleted file mode 100644 index 04ce0b776..000000000 --- a/aodh/tests/functional/api/test_app.py +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright 2014 IBM Corp. All Rights Reserved. -# Copyright 2015 Red Hat, Inc. -# -# 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 unittest import mock - -from oslo_config import cfg -from oslo_config import fixture as fixture_config - -from aodh.api import app -from aodh import service -from aodh.tests import base - - -class TestApp(base.BaseTestCase): - - def setUp(self): - super(TestApp, self).setUp() - conf = service.prepare_service(argv=[], config_files=[]) - self.CONF = self.useFixture(fixture_config.Config(conf)).conf - - def test_api_paste_file_not_exist(self): - self.CONF.set_override('paste_config', 'non-existent-file', "api") - with mock.patch.object(self.CONF, 'find_file') as ff: - ff.return_value = None - self.assertRaises(cfg.ConfigFilesNotFoundError, - app.load_app, self.CONF) diff --git a/releasenotes/notes/load-api-paste-ini-from-config-dirs-69480861a9633df4.yaml b/releasenotes/notes/load-api-paste-ini-from-config-dirs-69480861a9633df4.yaml new file mode 100644 index 000000000..f64223f02 --- /dev/null +++ b/releasenotes/notes/load-api-paste-ini-from-config-dirs-69480861a9633df4.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Now the ``aodh-api`` service look for the paste config file (a.k.a. + ``api-paste.ini`` from configruation directories like ``/etc/aodh``. + If the file is not found in the configuration directories, it uses + the default file. To use only a specific file, use a full file path for + the ``[api] paste_confing`` parameter