Add external config support to result_collection_api
[releng.git] / utils / test / result_collection_api / result_collection_api.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   - use POD name instead of id
19   - logging
20   - json args validation with schemes
21   - POST/PUT/DELETE for PODs
22   - POST/PUT/GET/DELETE for installers, platforms (enrich results info)
23   - count cases for GET on test_projects
24   - count results for GET on cases
25   - provide filtering on requests
26   - include objects
27   - swagger documentation
28   - setup file
29   - results pagination
30   - unit tests
31
32 """
33
34 import tornado.ioloop
35 import motor
36 import argparse
37
38 from resources.handlers import VersionHandler, PodHandler, \
39     TestProjectHandler, TestCasesHandler, TestResultsHandler
40 from common.config import APIConfig
41
42
43 # optionally get config file from command line
44 parser = argparse.ArgumentParser()
45 parser.add_argument("-c", "--config-file", dest='config_file',
46                     help="Config file location")
47 args = parser.parse_args()
48 CONF = APIConfig().parse(args.config_file)
49
50 # connecting to MongoDB server, and choosing database
51 db = motor.MotorClient(CONF.mongo_url)
52
53
54 def make_app():
55     return tornado.web.Application(
56         [
57             # GET /version => GET API version
58             (r"/version", VersionHandler),
59
60             # few examples:
61             # GET /pods => Get all pods
62             # GET /pods/1 => Get details on POD 1
63             (r"/pods", PodHandler),
64             (r"/pods/(\d*)", PodHandler),
65
66             # few examples:
67             # GET /test_projects
68             # GET /test_projects/yardstick
69             (r"/test_projects", TestProjectHandler),
70             (r"/test_projects/([^/]+)", TestProjectHandler),
71
72             # few examples
73             # GET /test_projects/qtip/cases => Get cases for qtip
74             #
75             (r"/test_projects/([^/]+)/cases", TestCasesHandler),
76             (r"/test_projects/([^/]+)/cases/([^/]+)", TestCasesHandler),
77             # (r"/test_cases/([^/]+)", TestCasesHandler),
78
79             # new path to avoid a long depth
80             # GET /results?project=functest&case=keystone.catalog&pod=1
81             #   => get results with optional filters
82             # POST /results =>
83             # Push results with mandatory request payload parameters
84             # (project, case, and pod_id)
85             (r"/results", TestResultsHandler),
86             (r"/results([^/]*)", TestResultsHandler),
87             (r"/results/([^/]*)", TestResultsHandler),
88         ],
89         db=db,
90         debug=CONF.api_debug_on,
91     )
92
93
94 def main():
95     application = make_app()
96     application.listen(CONF.api_port)
97     tornado.ioloop.IOLoop.current().start()
98
99
100 if __name__ == "__main__":
101     main()