7189967849c122360c78bd979fc96f13759f5443
[releng.git] / utils / test / result_collection_api / resources / pod_handler.py
1 from tornado import gen
2 from tornado.web import HTTPError, asynchronous
3
4 from tornado_swagger_ui.tornado_swagger import swagger
5 from handlers import GenericApiHandler
6 from common.constants import HTTP_BAD_REQUEST, HTTP_FORBIDDEN
7 from pod_models import Pod
8
9
10 class PodCLHandler(GenericApiHandler):
11     def initialize(self):
12         super(PodCLHandler, self).initialize()
13
14     @swagger.operation(nickname='list-all')
15     def get(self):
16         """
17             @description: list all PODs
18             @return 200: list all pods, empty list is no pod exist
19             @rtype: L{Pods}
20         """
21         self._list('pods', Pod)
22
23     # @asynchronous
24     @gen.coroutine
25     @swagger.operation(nickname='create')
26     def post(self):
27         """
28             @description: Create a POD
29             @param body: pod information to be created
30             @type body: L{PodCreateRequest}
31             @in body: body
32             @return 200: pod is created.
33             @rtype: L{Pod}
34             @raise 403: already exists as a pod
35             @raise 400: bad request
36         """
37         if self.json_args is None:
38             raise HTTPError(HTTP_BAD_REQUEST, 'no payload')
39
40         pod = Pod.from_dict(self.json_args)
41         name = pod.name
42         if name is None or name == '':
43             raise HTTPError(HTTP_BAD_REQUEST, 'pod name missing')
44
45         the_pod = yield self.db.pods.find_one({'name': name})
46         if the_pod is not None:
47             raise HTTPError(HTTP_FORBIDDEN,
48                             "{} already exists as a pod".format(
49                                 self.json_args.get("name")))
50         self._create('pods', pod, name)
51
52
53 class PodGURHandler(GenericApiHandler):
54     def initialize(self):
55         super(PodGURHandler, self).initialize()
56
57     @swagger.operation(nickname='get-one')
58     def get(self, pod_name=None):
59         """
60             @description: Get a single pod by pod_name
61             @return 200: pod exist
62             @raise 404: pod not exist
63             @rtype: L{Pod}
64         """
65         query = dict()
66         query["name"] = pod_name
67         self._get_one('pods', Pod, query)
68
69     @asynchronous
70     @gen.coroutine
71     def delete(self, pod_name):
72         """ Remove a POD
73
74         # check for an existing pod to be deleted
75         mongo_dict = yield self.db.pods.find_one(
76             {'name': pod_name})
77         pod = TestProject.pod(mongo_dict)
78         if pod is None:
79             raise HTTPError(HTTP_NOT_FOUND,
80                             "{} could not be found as a pod to be deleted"
81                             .format(pod_name))
82
83         # just delete it, or maybe save it elsewhere in a future
84         res = yield self.db.projects.remove(
85             {'name': pod_name})
86
87         self.finish_request(answer)
88         """
89         pass