Fix some errors about getting tiers resources
[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 re
15
16 from flask import jsonify
17
18 from functest.api.base import ApiResource
19 from functest.api.common import api_utils
20 from functest.cli.commands.cli_tier import Tier
21
22
23 class V1Tiers(ApiResource):
24     """ V1Tiers Resource class """
25
26     def get(self):
27         # pylint: disable=no-self-use
28         """ GET all tiers """
29         tiers_list = Tier().list()
30         data = re.split("[\n\t]", tiers_list)
31         data = [i.strip() for i in data if i != '']
32         data_dict = dict()
33         for i in range(len(data) / 2):
34             one_data = {data[i * 2].lstrip('- ').rstrip(':'): data[i * 2 + 1]}
35             if i == 0:
36                 data_dict = one_data
37             else:
38                 data_dict.update(one_data)
39         result = {'tiers': data_dict}
40         return jsonify(result)
41
42
43 class V1Tier(ApiResource):
44     """ V1Tier Resource class """
45
46     def get(self, tier_name):  # pylint: disable=no-self-use
47         """ GET the info of one tier """
48         tier_info = Tier().show(tier_name)
49         if not tier_info:
50             return api_utils.result_handler(
51                 status=1,
52                 data="The tier with name '%s' does not exist." % tier_name)
53         tier_info.__dict__.pop('name')
54         tier_info.__dict__.pop('tests_array')
55         tier_info.__dict__.pop('skipped_tests_array')
56         testcases = Tier().gettests(tier_name)
57         result = {'tier': tier_name, 'testcases': testcases}
58         result.update(tier_info.__dict__)
59         return jsonify(result)
60
61
62 class V1TestcasesinTier(ApiResource):
63     """ V1TestcasesinTier Resource class """
64
65     def get(self, tier_name):  # pylint: disable=no-self-use
66         """ GET all testcases within given tier """
67         tier_info = Tier().show(tier_name)
68         if not tier_info:
69             return api_utils.result_handler(
70                 status=1,
71                 data="The tier with name '%s' does not exist." % tier_name)
72         testcases = Tier().gettests(tier_name)
73         result = {'tier': tier_name, 'testcases': testcases}
74         return jsonify(result)