1
from charmhelpers.core.hookenv import (
5
from charmhelpers.contrib.network.ip import (
6
get_address_in_network,
11
from charmhelpers.contrib.hahelpers.cluster import is_clustered
19
'config': 'os-public-network',
20
'fallback': 'public-address'
23
'config': 'os-internal-network',
24
'fallback': 'private-address'
27
'config': 'os-admin-network',
28
'fallback': 'private-address'
33
def canonical_url(configs, endpoint_type=PUBLIC):
34
"""Returns the correct HTTP URL to this host given the state of HTTPS
35
configuration, hacluster and charm configuration.
37
:param configs: OSTemplateRenderer config templating object to inspect
38
for a complete https context.
39
:param endpoint_type: str endpoint type to resolve.
40
:param returns: str base URL for services on the current service unit.
43
if 'https' in configs.complete_contexts():
45
address = resolve_address(endpoint_type)
47
address = "[{}]".format(address)
48
return '%s://%s' % (scheme, address)
51
def resolve_address(endpoint_type=PUBLIC):
52
"""Return unit address depending on net config.
54
If unit is clustered with vip(s) and has net splits defined, return vip on
55
correct network. If clustered with no nets defined, return primary vip.
57
If not clustered, return unit address ensuring address is on configured net
58
split if one is configured.
60
:param endpoint_type: Network endpoing type
62
resolved_address = None
67
net_type = ADDRESS_MAP[endpoint_type]['config']
68
net_addr = config(net_type)
69
net_fallback = ADDRESS_MAP[endpoint_type]['fallback']
70
clustered = is_clustered()
73
# If no net-splits defined, we expect a single vip
74
resolved_address = vips[0]
77
if is_address_in_network(net_addr, vip):
78
resolved_address = vip
81
if config('prefer-ipv6'):
82
fallback_addr = get_ipv6_addr(exc_list=vips)[0]
84
fallback_addr = unit_get(net_fallback)
86
resolved_address = get_address_in_network(net_addr, fallback_addr)
88
if resolved_address is None:
89
raise ValueError("Unable to resolve a suitable IP address based on "
90
"charm state and configuration. (net_type=%s, "
91
"clustered=%s)" % (net_type, clustered))
93
return resolved_address