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: Ic5b9f9fe9a275a05feb71ea849af5c8a8dd087de
This commit is contained in:
James Page 2016-04-12 14:35:59 +01:00
parent 66ef94b78b
commit 54207d774a
3 changed files with 26 additions and 2 deletions

View File

@ -188,9 +188,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 glance --bind "public=public-space internal=internal-space admin=admin-space"
juju deploy glance --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:
@ -201,6 +203,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

@ -41,6 +41,7 @@ from charmhelpers.core.hookenv import (
unit_get,
UnregisteredHookError,
status_set,
network_get_primary_address,
)
from charmhelpers.core.host import (
# restart_on_change,
@ -136,7 +137,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')
relation_set(database=config('database'),
username=config('database-user'),
hostname=host)

View File

@ -43,6 +43,7 @@ TO_PATCH = [
'relation_get',
'service_name',
'unit_get',
'network_get_primary_address',
# charmhelpers.core.host
'apt_install',
'apt_update',
@ -86,6 +87,7 @@ class GlanceRelationTests(CharmTestCase):
def setUp(self):
super(GlanceRelationTests, self).setUp(relations, TO_PATCH)
self.config.side_effect = self.test_config.get
self.network_get_primary_address.side_effect = NotImplementedError
@patch.object(utils, 'git_install_requested')
def test_install_hook(self, git_requested):
@ -154,6 +156,17 @@ class GlanceRelationTests(CharmTestCase):
hostname='glance.foohost.com')
self.unit_get.assert_called_with('private-address')
def test_db_joined_network_spaces(self):
self.network_get_primary_address.side_effect = None
self.network_get_primary_address.return_value = '192.168.20.1'
self.unit_get.return_value = 'glance.foohost.com'
self.is_relation_made.return_value = False
relations.db_joined()
self.relation_set.assert_called_with(database='glance',
username='glance',
hostname='192.168.20.1')
self.assertFalse(self.unit_get.called)
def test_db_joined_with_ipv6(self):
self.test_config.set('prefer-ipv6', True)
self.is_relation_made.return_value = False