065de673bf8d18fa468fab7a731ffa27d99e268c
[yardstick.git] / api / views.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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 logging
10
11 from flask import request
12 from flask_restful import Resource
13
14 from api.utils import common as common_utils
15 from api.actions import test as test_action
16 from api.actions import result as result_action
17 from api.actions import env as env_action
18
19 logger = logging.getLogger(__name__)
20
21
22 class Release(Resource):
23     def post(self):
24         action = common_utils.translate_to_str(request.json.get('action', ''))
25         args = common_utils.translate_to_str(request.json.get('args', {}))
26         logger.debug('Input args is: action: %s, args: %s', action, args)
27
28         try:
29             return getattr(test_action, action)(args)
30         except AttributeError:
31             return common_utils.error_handler('Wrong action')
32
33
34 class Results(Resource):
35     def get(self):
36         args = common_utils.translate_to_str(request.args)
37         action = args.get('action', '')
38         logger.debug('Input args is: action: %s, args: %s', action, args)
39
40         try:
41             return getattr(result_action, action)(args)
42         except AttributeError:
43             return common_utils.error_handler('Wrong action')
44
45
46 class Env(Resource):
47     def post(self):
48         action = common_utils.translate_to_str(request.json.get('action', ''))
49         args = common_utils.translate_to_str(request.json.get('args', {}))
50         logger.debug('Input args is: action: %s, args: %s', action, args)
51
52         try:
53             return getattr(env_action, action)(args)
54         except AttributeError:
55             return common_utils.error_handler('Wrong action')