diff --git a/README.md b/README.md index eb98cdd7..0e8560bd 100644 --- a/README.md +++ b/README.md @@ -124,9 +124,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 neutron-api --bind "public=public-space internal=internal-space admin=admin-space" + juju deploy neutron-api --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: @@ -137,6 +139,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. diff --git a/hooks/neutron_api_hooks.py b/hooks/neutron_api_hooks.py index 9dd6bcd7..1d929fe6 100755 --- a/hooks/neutron_api_hooks.py +++ b/hooks/neutron_api_hooks.py @@ -22,6 +22,7 @@ from charmhelpers.core.hookenv import ( status_set, open_port, unit_get, + network_get_primary_address, ) from charmhelpers.core.host import ( @@ -330,7 +331,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) diff --git a/unit_tests/test_neutron_api_hooks.py b/unit_tests/test_neutron_api_hooks.py index 7bc3508a..5d853f56 100644 --- a/unit_tests/test_neutron_api_hooks.py +++ b/unit_tests/test_neutron_api_hooks.py @@ -74,6 +74,7 @@ TO_PATCH = [ 'IdentityServiceContext', 'force_etcd_restart', 'status_set', + 'network_get_primary_address', ] NEUTRON_CONF_DIR = "/etc/neutron" @@ -117,6 +118,7 @@ class NeutronAPIHooksTests(CharmTestCase): self.test_config.set('openstack-origin', 'distro') self.test_config.set('neutron-plugin', 'ovs') self.neutron_plugin_attribute.side_effect = _mock_nuage_npa + self.network_get_primary_address.side_effect = NotImplementedError def _fake_relids(self, rel_name): return [randrange(100) for _count in range(2)] @@ -341,6 +343,18 @@ class NeutronAPIHooksTests(CharmTestCase): hostname='myhostname', ) + def test_db_joined_spaces(self): + self.network_get_primary_address.side_effect = None + self.network_get_primary_address.return_value = '192.168.20.1' + self.is_relation_made.return_value = False + self.unit_get.return_value = 'myhostname' + self._call_hook('shared-db-relation-joined') + self.relation_set.assert_called_with( + username='neutron', + database='neutron', + hostname='192.168.20.1', + ) + def test_db_joined_with_postgresql(self): self.is_relation_made.return_value = True