Add token based authentication for post/update/delete
[releng.git] / utils / test / testapi / opnfv_testapi / common / config.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
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 # feng.xiaowei@zte.com.cn remove prepare_put_request            5-30-2016
9 ##############################################################################
10 import ConfigParser
11
12
13 class ParseError(Exception):
14     """
15     Custom exception class for config file
16     """
17
18     def __init__(self, message):
19         self.msg = message
20
21     def __str__(self):
22         return 'error parsing config file : %s' % self.msg
23
24
25 class APIConfig:
26     """
27     The purpose of this class is to load values correctly from the config file.
28     Each key is declared as an attribute in __init__() and linked in parse()
29     """
30
31     def __init__(self):
32         self._default_config_location = "/etc/opnfv_testapi/config.ini"
33         self.mongo_url = None
34         self.mongo_dbname = None
35         self.api_port = None
36         self.api_debug_on = None
37         self.api_authenticate_on = None
38         self._parser = None
39         self.swagger_base_url = None
40
41     def _get_parameter(self, section, param):
42         try:
43             return self._parser.get(section, param)
44         except ConfigParser.NoOptionError:
45             raise ParseError("[%s.%s] parameter not found" % (section, param))
46
47     def _get_int_parameter(self, section, param):
48         try:
49             return int(self._get_parameter(section, param))
50         except ValueError:
51             raise ParseError("[%s.%s] not an int" % (section, param))
52
53     def _get_bool_parameter(self, section, param):
54         result = self._get_parameter(section, param)
55         if str(result).lower() == 'true':
56             return True
57         if str(result).lower() == 'false':
58             return False
59
60         raise ParseError(
61             "[%s.%s : %s] not a boolean" % (section, param, result))
62
63     @staticmethod
64     def parse(config_location=None):
65         obj = APIConfig()
66
67         if config_location is None:
68             config_location = obj._default_config_location
69
70         obj._parser = ConfigParser.SafeConfigParser()
71         obj._parser.read(config_location)
72         if not obj._parser:
73             raise ParseError("%s not found" % config_location)
74
75         # Linking attributes to keys from file with their sections
76         obj.mongo_url = obj._get_parameter("mongo", "url")
77         obj.mongo_dbname = obj._get_parameter("mongo", "dbname")
78
79         obj.api_port = obj._get_int_parameter("api", "port")
80         obj.api_debug_on = obj._get_bool_parameter("api", "debug")
81         obj.api_authenticate_on = obj._get_bool_parameter("api",
82                                                           "authenticate")
83
84         obj.swagger_base_url = obj._get_parameter("swagger", "base_url")
85
86         return obj
87
88     def __str__(self):
89         return "mongo_url = %s \n" \
90                "mongo_dbname = %s \n" \
91                "api_port = %s \n" \
92                "api_debug_on = %s \n" \
93                "swagger_base_url = %s \n" % (self.mongo_url,
94                                              self.mongo_dbname,
95                                              self.api_port,
96                                              self.api_debug_on,
97                                              self.api_authenticate_on,
98                                              self.swagger_base_url)