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