Merge "swagger-ize result-apis of testAPI"
[releng.git] / utils / test / result_collection_api / result_collection_api.py
index 71b3267..652aa58 100644 (file)
@@ -14,70 +14,96 @@ Pre-requisites:
 
 We can launch the API with this file
 
-TODOS :
+TODOs :
+  - logging
   - json args validation with schemes
-  - count cases for GET on test_projects
+  - POST/PUT/DELETE for PODs
+  - POST/PUT/GET/DELETE for installers, platforms (enrich results info)
+  - count cases for GET on projects
   - count results for GET on cases
-  - add meta object to json response
-  - provide filtering on requests
   - include objects
+  - swagger documentation
+  - setup file
+  - results pagination
+  - unit tests
 
 """
 
+import argparse
+
 import tornado.ioloop
 import motor
 
-from resources.handlers import VersionHandler, PodHandler, \
-    TestProjectHandler, TestCasesHandler, TestResultsHandler
-from common.constants import API_LISTENING_PORT, MONGO_URL
+from resources.handlers import VersionHandler, DashboardHandler
+from resources.testcase_handlers import TestcaseCLHandler, TestcaseGURHandler
+from resources.pod_handlers import PodCLHandler, PodGURHandler
+from resources.project_handlers import ProjectCLHandler, ProjectGURHandler
+from resources.result_handlers import ResultsCLHandler, ResultsGURHandler
+from common.config import APIConfig
+from tornado_swagger_ui.tornado_swagger import swagger
+
+# optionally get config file from command line
+parser = argparse.ArgumentParser()
+parser.add_argument("-c", "--config-file", dest='config_file',
+                    help="Config file location")
+args = parser.parse_args()
+CONF = APIConfig().parse(args.config_file)
 
 # connecting to MongoDB server, and choosing database
-db = motor.MotorClient(MONGO_URL).test_results_collection
+client = motor.MotorClient(CONF.mongo_url)
+db = client[CONF.mongo_dbname]
 
 
 def make_app():
-    return tornado.web.Application(
+    return swagger.Application(
         [
             # GET /version => GET API version
-            (r"/version", VersionHandler),
+            (r"/versions", VersionHandler),
 
             # few examples:
-            # GET /pods => Get all pods
-            # GET /pods/1 => Get details on POD 1
-            (r"/pods", PodHandler),
-            (r"/pods/(\d*)", PodHandler),
+            # GET /api/v1/pods => Get all pods
+            # GET /api/v1/pods/1 => Get details on POD 1
+            (r"/api/v1/pods", PodCLHandler),
+            (r"/api/v1/pods/([^/]+)", PodGURHandler),
 
             # few examples:
-            # GET /test_projects
-            # GET /test_projects/yardstick
-            (r"/test_projects", TestProjectHandler),
-            (r"/test_projects/([^/]+)", TestProjectHandler),
+            # GET /projects
+            # GET /projects/yardstick
+            (r"/api/v1/projects", ProjectCLHandler),
+            (r"/api/v1/projects/([^/]+)", ProjectGURHandler),
 
             # few examples
-            # GET /test_projects/qtip/cases => Get cases for qtip
-            #
-            (r"/test_projects/([^/]+)/cases", TestCasesHandler),
-            (r"/test_projects/([^/]+)/cases/([^/]+)", TestCasesHandler),
-            # (r"/test_cases/([^/]+)", TestCasesHandler),
+            # GET /projects/qtip/cases => Get cases for qtip
+            (r"/api/v1/projects/([^/]+)/cases", TestcaseCLHandler),
+            (r"/api/v1/projects/([^/]+)/cases/([^/]+)", TestcaseGURHandler),
 
             # new path to avoid a long depth
             # GET /results?project=functest&case=keystone.catalog&pod=1
             #   => get results with optional filters
             # POST /results =>
             # Push results with mandatory request payload parameters
-            # (project, case, and pod_id)
-            (r"/results([^/]*)", TestResultsHandler),
-            (r"/results/([^/]*)", TestResultsHandler),
+            # (project, case, and pod)
+            (r"/api/v1/results", ResultsCLHandler),
+            (r"/api/v1/results/([^/]+)", ResultsGURHandler),
+
+            # Method to manage Dashboard ready results
+            # GET /dashboard?project=functest&case=vPing&pod=opnfv-jump2
+            #  => get results in dasboard ready format
+            # get /dashboard
+            #  => get the list of project with dashboard ready results
+            (r"/dashboard/v1/results", DashboardHandler),
+            (r"/dashboard/v1/results([^/]*)", DashboardHandler),
         ],
         db=db,
-        debug=True,
+        debug=CONF.api_debug_on,
     )
 
 
 def main():
     application = make_app()
-    application.listen(API_LISTENING_PORT)
+    application.listen(CONF.api_port)
     tornado.ioloop.IOLoop.current().start()
 
+
 if __name__ == "__main__":
     main()