Initiate packetization of Testing reporting
[releng.git] / utils / test / testapi / opnfv_testapi / cmd / server.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 """
11 Pre-requisites:
12     pip install motor
13     pip install tornado
14
15 We can launch the API with this file
16
17 TODOs :
18   - logging
19   - json args validation with schemes
20   - POST/PUT/DELETE for PODs
21   - POST/PUT/GET/DELETE for installers, platforms (enrich results info)
22   - count cases for GET on projects
23   - count results for GET on cases
24   - include objects
25   - swagger documentation
26   - setup file
27   - results pagination
28   - unit tests
29
30 """
31
32 import argparse
33 import sys
34
35 import motor
36 import tornado.ioloop
37
38 from opnfv_testapi.common import config
39 from opnfv_testapi.router import url_mappings
40 from opnfv_testapi.tornado_swagger import swagger
41
42 CONF = None
43
44
45 def parse_config(argv=[]):
46     global CONF
47     parser = argparse.ArgumentParser()
48     parser.add_argument("-c", "--config-file", dest='config_file',
49                         help="Config file location")
50     args = parser.parse_args(argv)
51     if args.config_file:
52         config.Config.CONFIG = args.config_file
53     CONF = config.Config()
54
55
56 def get_db():
57     return motor.MotorClient(CONF.mongo_url)[CONF.mongo_dbname]
58
59
60 def make_app():
61     swagger.docs(base_url=CONF.swagger_base_url,
62                  static_path=CONF.static_path)
63     return swagger.Application(
64         url_mappings.mappings,
65         db=get_db(),
66         debug=CONF.api_debug,
67         auth=CONF.api_authenticate,
68         cookie_secret='opnfv-testapi',
69     )
70
71
72 def main():
73     parse_config(sys.argv[1:])
74     application = make_app()
75     application.listen(CONF.api_port)
76     tornado.ioloop.IOLoop.current().start()
77
78
79 if __name__ == "__main__":
80     main()