|
13 | 13 | # License for the specific language governing permissions and limitations |
14 | 14 | # under the License. |
15 | 15 |
|
16 | | -import json |
17 | | -import logging |
18 | | -import os |
| 16 | +DEFAULT_CONF_PATH = "/etc/availability/config.json" |
19 | 17 |
|
20 | | -import jsonschema |
21 | | - |
22 | | - |
23 | | -CONF = None |
24 | | - |
25 | | -DEFAULT_CONF = { |
| 18 | +DEFAULT = { |
26 | 19 | "backend": { |
27 | 20 | "type": "elastic", |
28 | | - "connection": [{"host": "127.0.0.1", "port": 9200}] |
| 21 | + "connection": [ |
| 22 | + {"host": "127.0.0.1", "port": 9200}, |
| 23 | + ], |
29 | 24 | }, |
30 | | - "regions": [] |
| 25 | + "regions": [], |
| 26 | + "period": 60, |
| 27 | + "connection_timeout": 1, |
| 28 | + "read_timeout": 10, |
31 | 29 | } |
32 | 30 |
|
33 | | -CONF_SCHEMA = { |
34 | | - "type": "object", |
35 | | - "$schema": "http://json-schema.org/draft-04/schema", |
36 | | - "properties": { |
37 | | - "backend": { |
| 31 | +SCHEMA = { |
| 32 | + "backend": { |
| 33 | + "type": "object", |
| 34 | + "properties": { |
| 35 | + "type": {"type": "string"}, |
| 36 | + "connection": { |
| 37 | + "type": "array", |
| 38 | + "items": { |
| 39 | + # TODO(akscram): Here should be enum. |
| 40 | + "type": "object", |
| 41 | + "properties": { |
| 42 | + "host": {"type": "string"}, |
| 43 | + "port": {"type": "integer"}, |
| 44 | + }, |
| 45 | + "required": ["host"], |
| 46 | + "additionalProperties": False, |
| 47 | + }, |
| 48 | + "minItems": 1, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "required": ["type", "connection"], |
| 52 | + "additionalProperties": False, |
| 53 | + }, |
| 54 | + "regions": { |
| 55 | + "type": "array", |
| 56 | + "items": { |
38 | 57 | "type": "object", |
39 | 58 | "properties": { |
40 | | - "type": {"type": "string"}, |
41 | | - "connection": { |
| 59 | + "name": {"type": "string"}, |
| 60 | + "services": { |
42 | 61 | "type": "array", |
43 | 62 | "items": { |
44 | 63 | "type": "object", |
45 | 64 | "properties": { |
46 | | - "host": {"type": "string"}, |
47 | | - "port": {"type": "integer"} |
| 65 | + "name": {"type": "string"}, |
| 66 | + "url": {"type": "string"} |
48 | 67 | }, |
49 | | - "required": ["host"] |
| 68 | + "required": ["name", "url"], |
| 69 | + "additionalProperties": False, |
50 | 70 | }, |
51 | | - "minItems": 1 |
52 | | - } |
| 71 | + "minItems": 1, |
| 72 | + }, |
53 | 73 | }, |
54 | | - "required": ["type", "connection"] |
55 | | - }, |
56 | | - "regions": { |
57 | | - "type": "array", |
58 | | - "items": { |
59 | | - "type": "object", |
60 | | - "properties": { |
61 | | - "name": {"type": "string"}, |
62 | | - "services": { |
63 | | - "type": "array", |
64 | | - "items": { |
65 | | - "type": "object", |
66 | | - "properties": { |
67 | | - "name": {"type": "string"}, |
68 | | - "url": {"type": "string"} |
69 | | - }, |
70 | | - "required": ["name", "url"] |
71 | | - }, |
72 | | - "minItems": 1 |
73 | | - } |
74 | | - } |
75 | | - } |
| 74 | + "additionalProperties": False, |
76 | 75 | }, |
77 | | - "period": {"type": "number", "minimum": 5}, |
78 | | - "connection_timeout": {"type": "number"}, |
79 | | - "read_timeout": {"type": "number"}, |
80 | 76 | }, |
81 | | - "required": ["backend", "regions"] |
| 77 | + "period": {"type": "number", "minimum": 5}, |
| 78 | + "connection_timeout": {"type": "number"}, |
| 79 | + "read_timeout": {"type": "number"}, |
82 | 80 | } |
83 | | - |
84 | | - |
85 | | -def get_config(): |
86 | | - """Get cached configuration. |
87 | | -
|
88 | | - :returns: application config |
89 | | - :rtype: dict |
90 | | - """ |
91 | | - global CONF |
92 | | - if not CONF: |
93 | | - path = os.environ.get("AVAILABILITY_CONF", |
94 | | - "/etc/availability/config.json") |
95 | | - try: |
96 | | - config = json.load(open(path)) |
97 | | - logging.info("Config is '%s'" % path) |
98 | | - jsonschema.validate(config, CONF_SCHEMA) |
99 | | - CONF = config |
100 | | - except IOError as exc: |
101 | | - logging.warning("Failed to load config from '%s': %s", path, exc) |
102 | | - CONF = DEFAULT_CONF |
103 | | - except jsonschema.exceptions.ValidationError as exc: |
104 | | - logging.error("Configuration file %s is not valid: %s", path, exc) |
105 | | - raise |
106 | | - return CONF |
0 commit comments