Merge "Read env vars instead of using CONST in API"
[functest.git] / functest / api / resources / v1 / tiers.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Resources to handle tier related requests
12 """
13
14 import pkg_resources
15 import re
16
17 from flask import jsonify
18 from flasgger.utils import swag_from
19
20 from functest.api.base import ApiResource
21 from functest.api.common import api_utils
22 from functest.cli.commands.cli_tier import Tier
23
24
25 class V1Tiers(ApiResource):
26     """ V1Tiers Resource class """
27
28     @swag_from(pkg_resources.resource_filename(
29         'functest', 'api/swagger/tiers.yaml'))
30     def get(self):
31         # pylint: disable=no-self-use
32         """ GET all tiers """
33         tiers_list = Tier().list()
34         data = re.split("[\n\t]", tiers_list)
35         data = [i.strip() for i in data if i != '']
36         data_dict = dict()
37         for i in range(len(data) / 2):
38             one_data = {data[i * 2].lstrip('- ').rstrip(':'): data[i * 2 + 1]}
39             if i == 0:
40                 data_dict = one_data
41             else:
42                 data_dict.update(one_data)
43         result = {'tiers': data_dict}
44         return jsonify(result)
45
46
47 class V1Tier(ApiResource):
48     """ V1Tier Resource class """
49
50     @swag_from(pkg_resources.resource_filename(
51         'functest', 'api/swagger/tier.yaml'))
52     def get(self, tier_name):  # pylint: disable=no-self-use
53         """ GET the info of one tier """
54         tier_info = Tier().show(tier_name)
55         if not tier_info:
56             return api_utils.result_handler(
57                 status=1,
58                 data="The tier with name '%s' does not exist." % tier_name)
59         tier_info.__dict__.pop('name')
60         tier_info.__dict__.pop('tests_array')
61         tier_info.__dict__.pop('skipped_tests_array')
62         testcases = Tier().gettests(tier_name)
63         result = {'tier': tier_name, 'testcases': testcases}
64         result.update(tier_info.__dict__)
65         return jsonify(result)
66
67
68 class V1TestcasesinTier(ApiResource):
69     """ V1TestcasesinTier Resource class """
70
71     @swag_from(pkg_resources.resource_filename(
72         'functest', 'api/swagger/testcases_in_tier.yaml'))
73     def get(self, tier_name):  # pylint: disable=no-self-use
74         """ GET all testcases within given tier """
75         tier_info = Tier().show(tier_name)
76         if not tier_info:
77             return api_utils.result_handler(
78                 status=1,
79                 data="The tier with name '%s' does not exist." % tier_name)
80         testcases = Tier().gettests(tier_name)
81         result = {'tier': tier_name, 'testcases': testcases}
82         return jsonify(result)