Delete the old quantum catalog entry if a neutron entry is present

This commit is contained in:
Liam Young 2016-01-12 11:09:46 +00:00
parent 2f874e7c8f
commit afbe72f078
5 changed files with 84 additions and 3 deletions

View File

@ -84,6 +84,8 @@ from keystone_utils import (
REQUIRED_INTERFACES,
check_optional_relations,
ensure_pki_cert_paths,
is_service_present,
delete_service_entry,
)
from charmhelpers.contrib.hahelpers.cluster import (
@ -341,6 +343,8 @@ def identity_changed(relation_id=None, remote_unit=None):
return
add_service_to_keystone(relation_id, remote_unit)
if is_service_present('neutron', 'network'):
delete_service_entry('quantum', 'network')
settings = relation_get(rid=relation_id, unit=remote_unit)
service = settings.get('service', None)
if service:

View File

@ -244,6 +244,10 @@ valid_services = {
"type": "network",
"desc": "Quantum Networking Service"
},
"neutron": {
"type": "network",
"desc": "Neutron Networking Service"
},
"oxygen": {
"type": "oxygen",
"desc": "Oxygen Cloud Image Service"
@ -486,6 +490,28 @@ def get_admin_token():
error_out('Could not find admin_token line in %s' % KEYSTONE_CONF)
def is_service_present(service_name, service_type):
import manager
manager = manager.KeystoneManager(endpoint=get_local_endpoint(),
token=get_admin_token())
service_id = manager.resolve_service_id(service_name, service_type)
return service_id is not None
def delete_service_entry(service_name, service_type):
""" Delete a service from keystone"""
import manager
manager = manager.KeystoneManager(endpoint=get_local_endpoint(),
token=get_admin_token())
service_id = manager.resolve_service_id(service_name, service_type)
if service_id:
print "Hi!"
print service_id
print manager.api.services.delete
manager.api.services.delete(service_id)
log("Deleted service entry '%s'" % service_name, level=DEBUG)
def create_service_entry(service_name, service_type, service_desc, owner=None):
""" Add a new service entry to keystone if one does not already exist """
import manager

View File

@ -28,10 +28,14 @@ class KeystoneManager(object):
if name == u['name']:
return u['id']
def resolve_service_id(self, name):
def resolve_service_id(self, name, service_type=None):
"""Find the service_id of a given service"""
services = [s._info for s in self.api.services.list()]
for s in services:
if service_type:
if name == s['name'] and service_type == s['type']:
return s['id']
else:
if name == s['name']:
return s['id']

View File

@ -71,6 +71,8 @@ TO_PATCH = [
'get_netmask_for_address',
'get_address_in_network',
'git_install',
'is_service_present',
'delete_service_entry',
]
@ -623,6 +625,7 @@ class KeystoneRelationTests(CharmTestCase):
mock_log, mock_is_db_initialised):
mock_is_db_initialised.return_value = True
self.is_db_ready.return_value = True
self.is_service_present.return_value = True
mock_ensure_ssl_cert_master.return_value = False
hooks.identity_changed(
relation_id='identity-service:0',
@ -630,6 +633,28 @@ class KeystoneRelationTests(CharmTestCase):
self.add_service_to_keystone.assert_called_with(
'identity-service:0',
'unit/0')
self.delete_service_entry.assert_called_with(
'quantum',
'network')
@patch.object(hooks, 'is_db_initialised')
@patch('keystone_utils.log')
@patch('keystone_utils.ensure_ssl_cert_master')
@patch.object(hooks, 'hashlib')
@patch.object(hooks, 'send_notifications')
def test_identity_changed_leader_no_neutron(self, mock_send_notifications,
mock_hashlib,
mock_ensure_ssl_cert_master,
mock_log,
mock_is_db_initialised):
mock_is_db_initialised.return_value = True
self.is_db_ready.return_value = True
self.is_service_present.return_value = False
mock_ensure_ssl_cert_master.return_value = False
hooks.identity_changed(
relation_id='identity-service:0',
remote_unit='unit/0')
self.assertFalse(self.delete_service_entry.called)
@patch.object(hooks, 'local_unit')
@patch('keystone_utils.log')

View File

@ -703,3 +703,25 @@ class TestKeystoneUtils(CharmTestCase):
]
self.assertEquals(render.call_args_list, expected)
service_restart.assert_called_with('keystone')
@patch.object(manager, 'KeystoneManager')
def test_is_service_present(self, KeystoneManager):
mock_keystone = MagicMock()
mock_keystone.resolve_service_id.return_value = 'sid1'
KeystoneManager.return_value = mock_keystone
self.assertTrue(utils.is_service_present('bob', 'bill'))
@patch.object(manager, 'KeystoneManager')
def test_is_service_present_false(self, KeystoneManager):
mock_keystone = MagicMock()
mock_keystone.resolve_service_id.return_value = None
KeystoneManager.return_value = mock_keystone
self.assertFalse(utils.is_service_present('bob', 'bill'))
@patch.object(manager, 'KeystoneManager')
def test_delete_service_entry(self, KeystoneManager):
mock_keystone = MagicMock()
mock_keystone.resolve_service_id.return_value = 'sid1'
KeystoneManager.return_value = mock_keystone
utils.delete_service_entry('bob', 'bill')
mock_keystone.api.services.delete.assert_called_with('sid1')