Add Docker automated build hooks for Parser
[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]: 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         testcases = Tier().gettests(tier_name)
49         if not testcases:
50             return api_utils.result_handler(
51                 status=1,
52                 data="The tier with name '%s' does not exist." % tier_name)
53         tier_info = Tier().show(tier_name)
54         tier_info.__dict__.pop('name')
55         tier_info.__dict__.pop('tests_array')
56         result = {'tier': tier_name, 'testcases': testcases}
57         result.update(tier_info.__dict__)
58         return jsonify(result)
59
60
61 class V1TestcasesinTier(ApiResource):
62     """ V1TestcasesinTier Resource class """
63
64     def get(self, tier_name):  # pylint: disable=no-self-use
65         """ GET all testcases within given tier """
66         testcases = Tier().gettests(tier_name)
67         if not testcases:
68             return api_utils.result_handler(
69                 status=1,
70                 data="The tier with name '%s' does not exist." % tier_name)
71         result = {'tier': tier_name, 'testcases': testcases}
72         return jsonify(result)