1
from charmhelpers.core.hookenv import (
6
from charmhelpers.contrib.network.ip import (
7
get_address_in_network,
12
from charmhelpers.contrib.hahelpers.cluster import is_clustered
20
'config': 'os-public-network',
21
'fallback': 'public-address'
24
'config': 'os-internal-network',
25
'fallback': 'private-address'
28
'config': 'os-admin-network',
29
'fallback': 'private-address'
34
def canonical_url(configs, endpoint_type=PUBLIC):
36
Returns the correct HTTP URL to this host given the state of HTTPS
37
configuration, hacluster and charm configuration.
39
:configs OSTemplateRenderer: A config tempating object to inspect for
40
a complete https context.
41
:endpoint_type str: The endpoint type to resolve.
43
:returns str: Base URL for services on the current service unit.
46
if 'https' in configs.complete_contexts():
48
address = resolve_address(endpoint_type)
50
address = "[{}]".format(address)
51
return '%s://%s' % (scheme, address)
54
def resolve_address(endpoint_type=PUBLIC):
55
resolved_address = None
57
if config(_address_map[endpoint_type]['config']) is None:
58
# Assume vip is simple and pass back directly
59
resolved_address = config('vip')
61
for vip in config('vip').split():
62
if is_address_in_network(
63
config(_address_map[endpoint_type]['config']),
65
resolved_address = vip
67
resolved_address = get_address_in_network(
68
config(_address_map[endpoint_type]['config']),
69
unit_get(_address_map[endpoint_type]['fallback'])
71
if resolved_address is None:
72
raise ValueError('Unable to resolve a suitable IP address'
73
' based on charm state and configuration')
75
return resolved_address