Merge "yardstick env influxdb/grafana cmd support centos"
[yardstick.git] / api / resources / v2 / testcases.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 import errno
11 import os
12
13 from api import ApiResource
14 from yardstick.common.utils import result_handler
15 from yardstick.common import constants as consts
16 from yardstick.benchmark.core import Param
17 from yardstick.benchmark.core.testcase import Testcase
18
19 LOG = logging.getLogger(__name__)
20 LOG.setLevel(logging.DEBUG)
21
22
23 class V2Testcases(ApiResource):
24
25     def get(self):
26         param = Param({})
27         testcase_list = Testcase().list_all(param)
28         return result_handler(consts.API_SUCCESS, testcase_list)
29
30     def post(self):
31         return self._dispatch_post()
32
33     def upload_case(self, args):
34         try:
35             upload_file = args['file']
36         except KeyError:
37             return result_handler(consts.API_ERROR, 'file must be provided')
38
39         case_name = os.path.join(consts.TESTCASE_DIR, upload_file.filename)
40
41         LOG.info('save case file')
42         upload_file.save(case_name)
43
44         return result_handler(consts.API_SUCCESS, {'testcase': upload_file.filename})
45
46
47 class V2Testcase(ApiResource):
48
49     def get(self, case_name):
50         case_path = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(case_name))
51
52         try:
53             with open(case_path) as f:
54                 data = f.read()
55         except IOError as e:
56             if e.errno == errno.ENOENT:
57                 return result_handler(consts.API_ERROR, 'case does not exist')
58
59         return result_handler(consts.API_SUCCESS, {'testcase': data})
60
61     def delete(self, case_name):
62         case_path = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(case_name))
63
64         try:
65             os.remove(case_path)
66         except IOError as e:
67             if e.errno == errno.ENOENT:
68                 return result_handler(consts.API_ERROR, 'case does not exist')
69
70         return result_handler(consts.API_SUCCESS, {'testcase': case_name})