-
Notifications
You must be signed in to change notification settings - Fork 0
/
#utils.py#
62 lines (49 loc) · 2.93 KB
/
#utils.py#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# utils.py
import yaml
from models import Server
import random
def load_configuration(path):
with open(path) as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
return config
def transform_backends_from_config(config):
register = {}
for entry in config.get('hosts', []):
register.update({entry['host']: [Server(endpoint) for endpoint in entry['servers']]})
for entry in config.get('paths', []):
register.update({entry['path']: [Server(endpoint) for endpoint in entry['servers']]})
return register
def get_healthy_server(host, register):
try:
return random.choice([server for server in register[host] if server.healthy])
except IndexError:
return None
def healthcheck(register):
for host in register:
for server in register[host]:
server.healthcheck_and_update_status()
return register
def process_header_rules(config, host, rules):
for entry in config.get('hosts', []):
if host == entry['host']:
header_rules = entry.get('header_rules', {})
for instruction, modify_headers in header_rules.items():
if instruction == 'add':
rules.update(modify_headers)
if instruction == 'remove':
for key in modify_headers.keys():
if key in rules:
rules.pop(key)
return rules
def process_rules(config, host, rules, modify):
modify_options = {"header": "header_rules", "param": "param_rules"}
for entry in config.get('hosts', []):
if host == entry['host']:
header_rules = entry.get(modify_options[modify], {})
for instruction, modify_headers in header_rules.items():
if instruction == "add":
rules.update(modify_headers)
if instruction == "remove":
for key in modify_headers.keys():
if key in rules:
rules.pop(key)