Support py3 in uncovered packages
[functest.git] / functest / api / server.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Used to launch Functest RestApi
12
13 """
14
15 import inspect
16 import logging
17 import socket
18 import pkg_resources
19
20 from flask import Flask
21 from flask_restful import Api
22 from flasgger import Swagger
23 import six
24
25 from functest.api.base import ApiResource
26 from functest.api.common import api_utils
27 from functest.api.database.db import BASE
28 from functest.api.database.db import DB_SESSION
29 from functest.api.database.db import ENGINE
30 from functest.api.database.v1 import models
31 from functest.api.urls import URLPATTERNS
32
33
34 LOGGER = logging.getLogger(__name__)
35
36 APP = Flask(__name__)
37 API = Api(APP)
38 Swagger(APP)
39
40
41 @APP.teardown_request
42 def shutdown_session(exception=None):  # pylint: disable=unused-argument
43     """
44     To be called at the end of each request whether it is successful
45     or an exception is raised
46     """
47     DB_SESSION.remove()
48
49
50 def get_resource(resource_name):
51     """ Obtain the required resource according to resource name """
52     name = ''.join(resource_name.split('_'))
53     return next((r for r in api_utils.itersubclasses(ApiResource)
54                  if r.__name__.lower() == name))
55
56
57 def get_endpoint(url):
58     """ Obtain the endpoint of url """
59     address = socket.gethostbyname(socket.gethostname())
60     return six.moves.urllib.parse.urljoin(
61         'http://{}:5000'.format(address), url)
62
63
64 def api_add_resource():
65     """
66     The resource has multiple URLs and you can pass multiple URLs to the
67     add_resource() method on the Api object. Each one will be routed to
68     your Resource
69     """
70     for url_pattern in URLPATTERNS:
71         try:
72             API.add_resource(
73                 get_resource(url_pattern.target), url_pattern.url,
74                 endpoint=get_endpoint(url_pattern.url))
75         except StopIteration:
76             LOGGER.error('url resource not found: %s', url_pattern.url)
77
78
79 def init_db():
80     """
81     Import all modules here that might define models so that
82     they will be registered properly on the metadata, and then
83     create a database
84     """
85     def func(subcls):
86         """ To check the subclasses of BASE"""
87         try:
88             if issubclass(subcls[1], BASE):
89                 return True
90         except TypeError:
91             pass
92         return False
93     # pylint: disable=bad-option-value,bad-builtin,
94     subclses = filter(func, inspect.getmembers(models, inspect.isclass))
95     LOGGER.debug('Import models: %s', [subcls[1] for subcls in subclses])
96     BASE.metadata.create_all(bind=ENGINE)
97
98
99 def main():
100     """Entry point"""
101     logging.config.fileConfig(pkg_resources.resource_filename(
102         'functest', 'ci/logging.ini'))
103     logging.captureWarnings(True)
104     LOGGER.info('Starting Functest server')
105     api_add_resource()
106     init_db()
107     APP.run(host='0.0.0.0')