Merge "bugfix: fail to access testapi swagger"
[releng.git] / utils / test / testapi / opnfv_testapi / resources / scenario_handlers.py
1 from opnfv_testapi.common.constants import HTTP_FORBIDDEN
2 from opnfv_testapi.resources.handlers import GenericApiHandler
3 from opnfv_testapi.resources.scenario_models import Scenario
4 from opnfv_testapi.tornado_swagger import swagger
5
6
7 class GenericScenarioHandler(GenericApiHandler):
8     def __init__(self, application, request, **kwargs):
9         super(GenericScenarioHandler, self).__init__(application,
10                                                      request,
11                                                      **kwargs)
12         self.table = self.db_scenarios
13         self.table_cls = Scenario
14
15
16 class ScenariosCLHandler(GenericScenarioHandler):
17     @swagger.operation(nickname="List scenarios by queries")
18     def get(self):
19         """
20             @description: Retrieve scenario(s).
21             @notes: Retrieve scenario(s)
22             @return 200: all scenarios consist with query,
23                          empty list if no scenario is found
24             @rtype: L{Scenarios}
25         """
26         self._list()
27
28     @swagger.operation(nickname="Create a new scenario")
29     def post(self):
30         """
31             @description: create a new scenario by name
32             @param body: scenario to be created
33             @type body: L{ScenarioCreateRequest}
34             @in body: body
35             @rtype: L{CreateResponse}
36             @return 200: scenario is created.
37             @raise 403: scenario already exists
38             @raise 400:  body or name not provided
39         """
40         def query(data):
41             return {'name': data.name}
42
43         def error(data):
44             message = '{} already exists as a scenario'.format(data.name)
45             return HTTP_FORBIDDEN, message
46
47         miss_checks = ['name']
48         db_checks = [(self.table, False, query, error)]
49         self._create(miss_checks=miss_checks, db_checks=db_checks)
50
51
52 class ScenarioGURHandler(GenericScenarioHandler):
53     @swagger.operation(nickname='Get the scenario by name')
54     def get(self, name):
55         """
56             @description: get a single scenario by name
57             @rtype: L{Scenario}
58             @return 200: scenario exist
59             @raise 404: scenario not exist
60         """
61         pass
62
63     @swagger.operation(nickname="Update the scenario by name")
64     def put(self, name):
65         """
66             @description: update a single scenario by name
67             @param body: fields to be updated
68             @type body: L{string}
69             @in body: body
70             @rtype: L{Scenario}
71             @return 200: update success
72             @raise 404: scenario not exist
73             @raise 403: nothing to update
74         """
75         pass