This repository has been archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requires.py
87 lines (74 loc) · 3.38 KB
/
requires.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from charms.reactive import RelationBase, scopes, hook
from charmhelpers.core import hookenv
from collections import defaultdict
import json
class ProxyConfigError(Exception):
''' Exception raiseed if reverse proxy provider can't apply request configuratin '''
class ReverseProxyRequires(RelationBase):
scope = scopes.UNIT
# auto_accessors=['hostname','ports']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
hookenv.atexit(lambda: self.remove_state('{relation_name}.triggered'))
hookenv.atexit(lambda: self.remove_state('{relation_name}.departed'))
@hook('{requires:reverseproxy}-relation-{joined,changed}')
def changed(self):
self.set_state('{relation_name}.triggered')
hookenv.log('reverseproxy.triggered', 'DEBUG')
# if self.hostname and self.ports:
# hookenv.log('reverseproxy.ready', 'INFO')
self.set_state('{relation_name}.ready')
hookenv.log('reverseproxy.ready', 'DEBUG')
if self.cfg_status is None:
hookenv.log('reverseproxy cfg status not yet set', 'INFO')
elif self.cfg_status.startswith('passed'):
hookenv.log(self.cfg_status, 'INFO')
elif self.cfg_status.startswith('failed'):
hookenv.log(self.cfg_status, 'ERROR')
raise ProxyConfigError(self.cfg_status)
@hook('{requires:reverseproxy}-relation-{departed}')
def departed(self):
self.set_state('{relation_name}.triggered')
self.set_state('{relation_name}.departed')
self.remove_state('{relation_name}.configured')
self.remove_state('{relation_name}.ready')
hookenv.log('reverseproxy.departed', 'INFO')
def configure(self, config):
# If a single config is provided make it a list of one
configs = []
if isinstance(config, dict):
configs.append(config)
else:
configs = config
# Make all configs a defaultdict
configs = [defaultdict(lambda: None, d) for d in configs]
# Valid all configs
required_configs = ('external_port', 'internal_host', 'internal_port')
for entry in configs:
# Error if missing required configs
for rconfig in required_configs:
if not entry[rconfig]:
raise ProxyConfigError('"{}" is required'.format(rconfig))
# Check that mode is valid, set default if not provided
if entry['mode'] not in ('http', 'tcp'):
if not entry['mode']:
entry['mode'] = 'http'
else:
raise ProxyConfigError('"mode" setting must be http or tcp if provided')
# Set default value for 'check' if not set
if entry['check'] is None:
entry['check'] = True
# Check for http required options
if entry['urlbase'] == entry['subdomain'] is None and entry['mode'] == 'http':
raise ProxyConfigError('"urlbase" or "subdomain" must be set in http mode')
self.set_remote('config', json.dumps(configs))
self.set_state('{relation_name}.configured')
@property
def cfg_status(self):
return self.get_remote(hookenv.local_unit() + '.cfg_status')
@property
def hostname(self):
return self.get_remote('hostname')
@property
def ports(self):
return self.get_remote('ports')