swagger-ize project-apis of testAPI
[releng.git] / utils / test / result_collection_api / resources / pod_handlers.py
@@ -1,70 +1,56 @@
 from tornado import gen
-from tornado.web import HTTPError, asynchronous
+from tornado.web import asynchronous
 
 from tornado_swagger_ui.tornado_swagger import swagger
 from handlers import GenericApiHandler
-from common.constants import HTTP_BAD_REQUEST, HTTP_FORBIDDEN
 from pod_models import Pod
 
 
-class PodCLHandler(GenericApiHandler):
-    def initialize(self):
-        super(PodCLHandler, self).initialize()
+class GenericPodHandler(GenericApiHandler):
+    def __init__(self, application, request, **kwargs):
+        super(GenericPodHandler, self).__init__(application, request, **kwargs)
+        self.table = 'pods'
+        self.table_cls = Pod
 
+
+class PodCLHandler(GenericPodHandler):
     @swagger.operation(nickname='list-all')
     def get(self):
         """
-            @description: list all PODs
+            @description: list all pods
             @return 200: list all pods, empty list is no pod exist
             @rtype: L{Pods}
         """
-        self._list('pods', Pod)
+        self._list()
 
-    # @asynchronous
     @gen.coroutine
     @swagger.operation(nickname='create')
     def post(self):
         """
-            @description: Create a POD
-            @param body: pod information to be created
+            @description: create a pod
+            @param body: pod to be created
             @type body: L{PodCreateRequest}
             @in body: body
-            @return 200: pod is created.
             @rtype: L{Pod}
-            @raise 403: already exists as a pod
-            @raise 400: bad request
+            @return 200: pod is created.
+            @raise 403: pod already exists
+            @raise 400: post without body
         """
-        if self.json_args is None:
-            raise HTTPError(HTTP_BAD_REQUEST, 'no payload')
-
-        pod = Pod.from_dict(self.json_args)
-        name = pod.name
-        if name is None or name == '':
-            raise HTTPError(HTTP_BAD_REQUEST, 'pod name missing')
+        self._create('{} already exists as a {}')
 
-        the_pod = yield self.db.pods.find_one({'name': name})
-        if the_pod is not None:
-            raise HTTPError(HTTP_FORBIDDEN,
-                            "{} already exists as a pod".format(
-                                self.json_args.get("name")))
-        self._create('pods', pod, name)
-
-
-class PodGURHandler(GenericApiHandler):
-    def initialize(self):
-        super(PodGURHandler, self).initialize()
 
+class PodGURHandler(GenericPodHandler):
     @swagger.operation(nickname='get-one')
-    def get(self, pod_name=None):
+    def get(self, pod_name):
         """
-            @description: Get a single pod by pod_name
+            @description: get a single pod by pod_name
+            @rtype: L{Pod}
             @return 200: pod exist
             @raise 404: pod not exist
-            @rtype: L{Pod}
         """
         query = dict()
-        query["name"] = pod_name
-        self._get_one('pods', Pod, query)
+        query['name'] = pod_name
+        self._get_one(query)
 
     @asynchronous
     @gen.coroutine