35
33
def canonical_url(configs, endpoint_type=PUBLIC):
37
Returns the correct HTTP URL to this host given the state of HTTPS
34
"""Returns the correct HTTP URL to this host given the state of HTTPS
38
35
configuration, hacluster and charm configuration.
40
:configs OSTemplateRenderer: A config tempating object to inspect for
41
a complete https context.
42
:endpoint_type str: The endpoint type to resolve.
44
:returns str: Base URL for services on the current service unit.
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.
47
43
if 'https' in configs.complete_contexts():
55
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
56
62
resolved_address = None
58
if config(_address_map[endpoint_type]['config']) is None:
59
# Assume vip is simple and pass back directly
60
resolved_address = config('vip')
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]
62
for vip in config('vip').split():
63
if is_address_in_network(
64
config(_address_map[endpoint_type]['config']),
77
if is_address_in_network(net_addr, vip):
66
78
resolved_address = vip
68
81
if config('prefer-ipv6'):
69
fallback_addr = get_ipv6_addr(exc_list=[config('vip')])[0]
82
fallback_addr = get_ipv6_addr(exc_list=vips)[0]
71
fallback_addr = unit_get(_address_map[endpoint_type]['fallback'])
72
resolved_address = get_address_in_network(
73
config(_address_map[endpoint_type]['config']), fallback_addr)
84
fallback_addr = unit_get(net_fallback)
86
resolved_address = get_address_in_network(net_addr, fallback_addr)
75
88
if resolved_address is None:
76
raise ValueError('Unable to resolve a suitable IP address'
77
' based on charm state and configuration')
79
return resolved_address
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