"Fossies" - the Fresh Open Source Software Archive

Member "monasca-log-api-2.9.0/monasca_log_api/app/api.py" (1 Apr 2019, 3530 Bytes) of package /linux/misc/openstack/monasca-log-api-2.9.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "api.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.7.0_vs_2.9.0.

    1 # Copyright 2017 FUJITSU LIMITED
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 """
   16 Module contains factories to initializes various applications
   17 of monasca-log-api
   18 """
   19 
   20 import six
   21 
   22 import falcon
   23 from oslo_log import log
   24 
   25 from monasca_log_api.app.base import error_handlers
   26 from monasca_log_api.app.base import request
   27 from monasca_log_api.app.controller import healthchecks
   28 from monasca_log_api.app.controller.v2 import logs as v2_logs
   29 from monasca_log_api.app.controller.v3 import logs as v3_logs
   30 from monasca_log_api.app.controller import versions
   31 from monasca_log_api import config
   32 
   33 
   34 def error_trap(app_name):
   35     """Decorator trapping any error during application boot time"""
   36 
   37     @six.wraps(error_trap)
   38     def _wrapper(func):
   39 
   40         @six.wraps(_wrapper)
   41         def _inner_wrapper(*args, **kwargs):
   42             try:
   43                 return func(*args, **kwargs)
   44             except Exception:
   45                 logger = log.getLogger(__name__)
   46                 logger.exception('Failed to load application \'%s\'', app_name)
   47                 raise
   48 
   49         return _inner_wrapper
   50 
   51     return _wrapper
   52 
   53 
   54 def singleton_config(func):
   55     """Decorator ensuring that configuration is loaded only once."""
   56 
   57     @six.wraps(singleton_config)
   58     def _wrapper(global_config, **local_conf):
   59         config.parse_args()
   60         return func(global_config, **local_conf)
   61 
   62     return _wrapper
   63 
   64 
   65 @error_trap('version')
   66 def create_version_app(global_conf, **local_conf):
   67     """Creates Version application"""
   68 
   69     ctrl = versions.Versions()
   70     controllers = {
   71         '/': ctrl,   # redirect http://host:port/ down to Version app
   72                      # avoid conflicts with actual pipelines and 404 error
   73         '/version': ctrl,  # list all the versions
   74         '/version/{version_id}': ctrl  # display details of the version
   75     }
   76 
   77     wsgi_app = falcon.API(
   78         request_type=request.Request
   79     )
   80     for route, ctrl in controllers.items():
   81         wsgi_app.add_route(route, ctrl)
   82     return wsgi_app
   83 
   84 
   85 @error_trap('healthcheck')
   86 def create_healthcheck_app(global_conf, **local_conf):
   87     """Creates Healthcheck application"""
   88 
   89     ctrl = healthchecks.HealthChecks()
   90     controllers = {
   91         '/': ctrl
   92     }
   93 
   94     wsgi_app = falcon.API(
   95         request_type=request.Request
   96     )
   97     for route, ctrl in controllers.items():
   98         wsgi_app.add_route(route, ctrl)
   99     return wsgi_app
  100 
  101 
  102 @error_trap('api')
  103 @singleton_config
  104 def create_api_app(global_conf, **local_conf):
  105     """Creates MainAPI application"""
  106 
  107     controllers = {}
  108     api_version = global_conf.get('api_version')
  109 
  110     if api_version == 'v2.0':
  111         controllers.update({
  112             '/log/single': v2_logs.Logs()
  113         })
  114     elif api_version == 'v3.0':
  115         controllers.update({
  116             '/logs': v3_logs.Logs()
  117         })
  118 
  119     wsgi_app = falcon.API(
  120         request_type=request.Request
  121     )
  122 
  123     for route, ctrl in controllers.items():
  124         wsgi_app.add_route(route, ctrl)
  125 
  126     error_handlers.register_error_handlers(wsgi_app)
  127 
  128     return wsgi_app