25a670c323f41daa4ab0a798d741c75a935a3640
[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   - 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
34 import tornado.ioloop
35 import motor
36
37 from resources.handlers import VersionHandler, \
38     TestResultsHandler, DashboardHandler
39 from resources.testcase_handlers import TestcaseCLHandler, TestcaseGURHandler
40 from resources.pod_handlers import PodCLHandler, PodGURHandler
41 from resources.project_handlers import ProjectCLHandler, ProjectGURHandler
42 from common.config import APIConfig
43 from tornado_swagger_ui.tornado_swagger import swagger
44
45 # optionally get config file from command line
46 parser = argparse.ArgumentParser()
47 parser.add_argument("-c", "--config-file", dest='config_file',
48                     help="Config file location")
49 args = parser.parse_args()
50 CONF = APIConfig().parse(args.config_file)
51
52 # connecting to MongoDB server, and choosing database
53 client = motor.MotorClient(CONF.mongo_url)
54 db = client[CONF.mongo_dbname]
55
56
57 def make_app():
58     return swagger.Application(
59         [
60             # GET /version => GET API version
61             (r"/versions", VersionHandler),
62
63             # few examples:
64             # GET /api/v1/pods => Get all pods
65             # GET /api/v1/pods/1 => Get details on POD 1
66             (r"/api/v1/pods", PodCLHandler),
67             (r"/api/v1/pods/([^/]+)", PodGURHandler),
68
69             # few examples:
70             # GET /projects
71             # GET /projects/yardstick
72             (r"/api/v1/projects", ProjectCLHandler),
73             (r"/api/v1/projects/([^/]+)", ProjectGURHandler),
74
75             # few examples
76             # GET /projects/qtip/cases => Get cases for qtip
77             (r"/api/v1/projects/([^/]+)/cases", TestcaseCLHandler),
78             (r"/api/v1/projects/([^/]+)/cases/([^/]+)", TestcaseGURHandler),
79
80             # new path to avoid a long depth
81             # GET /results?project=functest&case=keystone.catalog&pod=1
82             #   => get results with optional filters
83             # POST /results =>
84             # Push results with mandatory request payload parameters
85             # (project, case, and pod)
86             (r"/api/v1/results", TestResultsHandler),
87             (r"/api/v1/results([^/]*)", TestResultsHandler),
88             (r"/api/v1/results/([^/]*)", TestResultsHandler),
89
90             # Method to manage Dashboard ready results
91             # GET /dashboard?project=functest&case=vPing&pod=opnfv-jump2
92             #  => get results in dasboard ready format
93             # get /dashboard
94             #  => get the list of project with dashboard ready results
95             (r"/dashboard/v1/results", DashboardHandler),
96             (r"/dashboard/v1/results([^/]*)", DashboardHandler),
97         ],
98         db=db,
99         debug=CONF.api_debug_on,
100     )
101
102
103 def main():
104     application = make_app()
105     application.listen(CONF.api_port)
106     tornado.ioloop.IOLoop.current().start()
107
108
109 if __name__ == "__main__":
110     main()