diff --git a/pyms/flask/app/create_app.py b/pyms/flask/app/create_app.py index 0d6859e..acc57ed 100644 --- a/pyms/flask/app/create_app.py +++ b/pyms/flask/app/create_app.py @@ -10,6 +10,7 @@ from pyms.crypt.driver import CryptResource from pyms.flask.app.utils import SingletonMeta, ReverseProxied from pyms.flask.healthcheck import healthcheck_blueprint +from pyms.flask.configreload import configreload_blueprint from pyms.flask.services.driver import ServicesResource, DriverService from pyms.logger import CustomJsonFormatter from pyms.utils import check_package_exists, import_from @@ -218,6 +219,7 @@ def create_app(self): # Initialize Blueprints self.application.register_blueprint(healthcheck_blueprint) + self.application.register_blueprint(configreload_blueprint) self.init_libs() self.add_error_handlers() diff --git a/pyms/flask/configreload/__init__.py b/pyms/flask/configreload/__init__.py new file mode 100644 index 0000000..5629da3 --- /dev/null +++ b/pyms/flask/configreload/__init__.py @@ -0,0 +1,4 @@ +from .configreload import configreload_blueprint + + +__all__ = ['configreload_blueprint'] diff --git a/pyms/flask/configreload/configreload.py b/pyms/flask/configreload/configreload.py new file mode 100644 index 0000000..c18e7cc --- /dev/null +++ b/pyms/flask/configreload/configreload.py @@ -0,0 +1,15 @@ +from __future__ import unicode_literals, print_function, absolute_import, division +from flask import Blueprint +from flask import current_app + +configreload_blueprint = Blueprint('configreload', __name__, static_url_path='/static') + + +@configreload_blueprint.route('/reload-config', methods=['POST']) +def reloadconfig(): + """ + Reread configuration from file. + :return: + """ + current_app.ms.reload_conf() + return "OK" diff --git a/pyms/flask/healthcheck/healthcheck.py b/pyms/flask/healthcheck/healthcheck.py index 55823d1..3022740 100644 --- a/pyms/flask/healthcheck/healthcheck.py +++ b/pyms/flask/healthcheck/healthcheck.py @@ -7,7 +7,7 @@ @healthcheck_blueprint.route('/healthcheck', methods=['GET']) def healthcheck(): - """Set a healtcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc. + """Set a healthcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc. :return: """ return "OK" diff --git a/tests/config-tests-reload1.yml b/tests/config-tests-reload1.yml new file mode 100644 index 0000000..9e9fb2b --- /dev/null +++ b/tests/config-tests-reload1.yml @@ -0,0 +1,5 @@ +pyms: + config: + DEBUG: true + TESTING: true + APP_NAME: "reload1" \ No newline at end of file diff --git a/tests/config-tests-reload2.yml b/tests/config-tests-reload2.yml new file mode 100644 index 0000000..cccafb5 --- /dev/null +++ b/tests/config-tests-reload2.yml @@ -0,0 +1,5 @@ +pyms: + config: + DEBUG: true + TESTING: true + APP_NAME: "reload2" \ No newline at end of file diff --git a/tests/test_flask.py b/tests/test_flask.py index 83e9869..c9215d1 100644 --- a/tests/test_flask.py +++ b/tests/test_flask.py @@ -81,6 +81,31 @@ def test_reverse_proxy_zuul(self): self.assertEqual(200, response.status_code) +class ReloadTests(unittest.TestCase): + """ + Tests for configreload endpoints + """ + + file1 = "config-tests-reload1.yml" + file2 = "config-tests-reload2.yml" + + def setUp(self): + os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)), + self.file1) + ms = MyMicroservice(path=__file__) + self.app = ms.create_app() + self.client = self.app.test_client() + self.assertEqual("reload1", self.app.config["APP_NAME"]) + + def test_configreload(self): + os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)), + self.file2) + response = self.client.post('/reload-config') + self.assertEqual(b"OK", response.data) + self.assertEqual(200, response.status_code) + self.assertEqual("reload2", config()["APP_NAME"]) + + class MicroserviceTest(unittest.TestCase): """ Tests for Singleton