replace self-defined http codes with standard definitions
[releng.git] / utils / test / testapi / opnfv_testapi / resources / pod_handlers.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 import httplib
10
11 import handlers
12 from opnfv_testapi.tornado_swagger import swagger
13 import pod_models
14
15
16 class GenericPodHandler(handlers.GenericApiHandler):
17     def __init__(self, application, request, **kwargs):
18         super(GenericPodHandler, self).__init__(application, request, **kwargs)
19         self.table = 'pods'
20         self.table_cls = pod_models.Pod
21
22
23 class PodCLHandler(GenericPodHandler):
24     @swagger.operation(nickname='listAllPods')
25     def get(self):
26         """
27             @description: list all pods
28             @return 200: list all pods, empty list is no pod exist
29             @rtype: L{Pods}
30         """
31         self._list()
32
33     @swagger.operation(nickname='createPod')
34     def post(self):
35         """
36             @description: create a pod
37             @param body: pod to be created
38             @type body: L{PodCreateRequest}
39             @in body: body
40             @rtype: L{CreateResponse}
41             @return 200: pod is created.
42             @raise 403: pod already exists
43             @raise 400: body or name not provided
44         """
45         def query(data):
46             return {'name': data.name}
47
48         def error(data):
49             message = '{} already exists as a pod'.format(data.name)
50             return httplib.FORBIDDEN, message
51
52         miss_checks = ['name']
53         db_checks = [(self.table, False, query, error)]
54         self._create(miss_checks, db_checks)
55
56
57 class PodGURHandler(GenericPodHandler):
58     @swagger.operation(nickname='getPodByName')
59     def get(self, pod_name):
60         """
61             @description: get a single pod by pod_name
62             @rtype: L{Pod}
63             @return 200: pod exist
64             @raise 404: pod not exist
65         """
66         query = dict()
67         query['name'] = pod_name
68         self._get_one(query)
69
70     def delete(self, pod_name):
71         """ Remove a POD
72
73         # check for an existing pod to be deleted
74         mongo_dict = yield self.db.pods.find_one(
75             {'name': pod_name})
76         pod = TestProject.pod(mongo_dict)
77         if pod is None:
78             raise HTTPError(HTTP_NOT_FOUND,
79                             "{} could not be found as a pod to be deleted"
80                             .format(pod_name))
81
82         # just delete it, or maybe save it elsewhere in a future
83         res = yield self.db.projects.remove(
84             {'name': pod_name})
85
86         self.finish_request(answer)
87         """
88         pass