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