Fix minor bugs for 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   - json args validation with schemes
19   - count cases for GET on test_projects
20   - count results for GET on cases
21   - provide filtering on requests
22   - include objects
23   - logging
24   - external configuration file
25   - setup file
26   - results pagination
27   - POST/PUT/DELETE for PODs
28   - POST/PUT/GET/DELETE for installers, platforms (enrich results info)
29
30 """
31
32 import tornado.ioloop
33 import motor
34
35 from resources.handlers import VersionHandler, PodHandler, \
36     TestProjectHandler, TestCasesHandler, TestResultsHandler
37 from common.constants import API_LISTENING_PORT, MONGO_URL
38
39 # connecting to MongoDB server, and choosing database
40 db = motor.MotorClient(MONGO_URL).test_results_collection
41
42
43 def make_app():
44     return tornado.web.Application(
45         [
46             # GET /version => GET API version
47             (r"/version", VersionHandler),
48
49             # few examples:
50             # GET /pods => Get all pods
51             # GET /pods/1 => Get details on POD 1
52             (r"/pods", PodHandler),
53             (r"/pods/(\d*)", PodHandler),
54
55             # few examples:
56             # GET /test_projects
57             # GET /test_projects/yardstick
58             (r"/test_projects", TestProjectHandler),
59             (r"/test_projects/([^/]+)", TestProjectHandler),
60
61             # few examples
62             # GET /test_projects/qtip/cases => Get cases for qtip
63             #
64             (r"/test_projects/([^/]+)/cases", TestCasesHandler),
65             (r"/test_projects/([^/]+)/cases/([^/]+)", TestCasesHandler),
66             # (r"/test_cases/([^/]+)", TestCasesHandler),
67
68             # new path to avoid a long depth
69             # GET /results?project=functest&case=keystone.catalog&pod=1
70             #   => get results with optional filters
71             # POST /results =>
72             # Push results with mandatory request payload parameters
73             # (project, case, and pod_id)
74             (r"/results", TestResultsHandler),
75             (r"/results([^/]*)", TestResultsHandler),
76             (r"/results/([^/]*)", TestResultsHandler),
77         ],
78         db=db,
79         debug=True,
80     )
81
82
83 def main():
84     application = make_app()
85     application.listen(API_LISTENING_PORT)
86     tornado.ioloop.IOLoop.current().start()
87
88 if __name__ == "__main__":
89     main()