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