Use network space binding for shared-db relation

To ensure that the charm presents the right IP address for
accessing the MySQL database over the shared-db relation,
ensure that any network space binding provided by the user
is preferred over the default of 'private-address'.

If network spaces is not supported (juju < 2.0), fallback to
using 'private-address'.

Change-Id: Ie020135e716cebfbe5f96759a65d5a082b767dd0
This commit is contained in:
James Page 2016-04-12 14:56:30 +01:00
parent 03c3d96765
commit c67fdf10fd
3 changed files with 26 additions and 2 deletions

View File

@ -220,9 +220,11 @@ This charm supports the use of Juju Network Spaces, allowing the charm to be bou
API endpoints can be bound to distinct network spaces supporting the network separation of public, internal and admin endpoints.
Access to the underlying MySQL instance can also be bound to a specific space using the shared-db relation.
To use this feature, use the --bind option when deploying the charm:
juju deploy cinder --bind "public=public-space internal=internal-space admin=admin-space"
juju deploy cinder --bind "public=public-space internal=internal-space admin=admin-space shared-db=internal-space"
alternatively these can also be provided as part of a juju native bundle configuration:
@ -233,6 +235,7 @@ alternatively these can also be provided as part of a juju native bundle configu
public: public-space
admin: admin-space
internal: internal-space
shared-db: internal-space
NOTE: Spaces must be configured in the underlying provider prior to attempting to use them.

View File

@ -44,6 +44,7 @@ from charmhelpers.core.hookenv import (
log,
ERROR,
status_set,
network_get_primary_address,
)
from charmhelpers.fetch import (
@ -193,7 +194,14 @@ def db_joined():
sync_db_with_multi_ipv6_addresses(config('database'),
config('database-user'))
else:
host = unit_get('private-address')
host = None
try:
# NOTE: try to use network spaces
host = network_get_primary_address('shared-db')
except NotImplementedError:
# NOTE: fallback to private-address
host = unit_get('private-address')
conf = config()
relation_set(database=conf['database'],
username=conf['database-user'],

View File

@ -62,6 +62,7 @@ TO_PATCH = [
'relation_set',
'service_name',
'unit_get',
'network_get_primary_address',
# charmhelpers.core.host
'apt_install',
'apt_update',
@ -410,6 +411,7 @@ class TestJoinedHooks(CharmTestCase):
def setUp(self):
super(TestJoinedHooks, self).setUp(hooks, TO_PATCH)
self.config.side_effect = self.test_config.get
self.network_get_primary_address.side_effect = NotImplementedError
def test_db_joined(self):
'It properly requests access to a shared-db service'
@ -420,6 +422,17 @@ class TestJoinedHooks(CharmTestCase):
'hostname': 'cindernode1', 'database': 'cinder'}
self.relation_set.assert_called_with(**expected)
def test_db_joined_spaces(self):
'Ensure network space binding is used when provided'
self.network_get_primary_address.side_effect = None
self.network_get_primary_address.return_value = '192.168.20.1'
self.unit_get.return_value = 'cindernode1'
self.is_relation_made.return_value = False
hooks.hooks.execute(['hooks/shared-db-relation-joined'])
expected = {'username': 'cinder',
'hostname': '192.168.20.1', 'database': 'cinder'}
self.relation_set.assert_called_with(**expected)
def test_db_joined_with_ipv6(self):
'It properly requests access to a shared-db service'
self.unit_get.return_value = 'cindernode1'