plugins/ceph: Pass key data instead of interface object

The helper functions for configuring keys will be used with both
RelationBase- and Endpoint-based interfaces.  They have different
mechanics to get to relation attributes (function calls vs.
properties).

The only interface attribute we use is the key data, so just pass
that instead of the interace object and let it be the callers job
to unpack the data as the interface they use require.

Change-Id: If1d645f4708e27b724f93cac0e14431137c885d7
This commit is contained in:
Frode Nordahl 2019-04-23 14:39:15 +02:00
parent 0e618a7ee5
commit d3117e21aa
2 changed files with 14 additions and 17 deletions

View File

@ -121,12 +121,11 @@ class BaseOpenStackCephCharm(object):
return os.path.join(self.snap_path_prefix,
self.ceph_keyring_path_prefix)
def configure_ceph_keyring(self, interface,
cluster_name=None):
def configure_ceph_keyring(self, key, cluster_name=None):
"""Creates or updates a Ceph keyring file.
:param interface: Interface with ``key`` property.
:type interface: Any class that has a property named ``key``.
:param key: Key data
:type key: str
:param cluster_name: (Optional) Name of Ceph cluster to operate on.
Defaults to value of ``self.ceph_cluster_name``.
:type cluster_name: str
@ -145,7 +144,7 @@ class BaseOpenStackCephCharm(object):
cmd = [
'ceph-authtool', keyring_absolute_path,
'--create-keyring', '--name={}'.format(self.ceph_key_name),
'--add-key', interface.key, '--mode', '0600',
'--add-key', key, '--mode', '0600',
]
try:
subprocess.check_call(cmd)
@ -231,10 +230,10 @@ class CephCharm(charms_openstack.charm.OpenStackCharm,
self.ceph_keyring_path_prefix,
self.ceph_service_name)
def configure_ceph_keyring(self, interface, cluster_name=None):
def configure_ceph_keyring(self, key, cluster_name=None):
"""Override parent function to add symlink in ``/etc/ceph``."""
keyring_absolute_path = super().configure_ceph_keyring(
interface, cluster_name=cluster_name)
key, cluster_name=cluster_name)
symlink_absolute_path = os.path.join(
'/etc/ceph',
os.path.basename(keyring_absolute_path))

View File

@ -70,9 +70,8 @@ class TestOpenStackCephConsumingCharm(BaseOpenStackCharmTest):
return_value='sarepta')
self.patch_object(cpl.subprocess, 'check_call')
self.patch_object(cpl.shutil, 'chown')
interface = mock.MagicMock()
interface.key = 'KEY'
self.assertEqual(self.target.configure_ceph_keyring(interface),
key = 'KEY'
self.assertEqual(self.target.configure_ceph_keyring(key),
'/etc/ceph/ceph.client.sarepta.keyring')
self.isdir.assert_called_with('/etc/ceph')
self.mkdir.assert_called_with('/etc/ceph',
@ -85,7 +84,7 @@ class TestOpenStackCephConsumingCharm(BaseOpenStackCharmTest):
])
self.target.user = 'ceph'
self.target.group = 'ceph'
self.target.configure_ceph_keyring(interface)
self.target.configure_ceph_keyring(key)
self.chown.assert_called_with(
'/etc/ceph/ceph.client.sarepta.keyring',
user='ceph', group='ceph')
@ -94,11 +93,11 @@ class TestOpenStackCephConsumingCharm(BaseOpenStackCharmTest):
self.check_call.side_effect = [
subprocess.CalledProcessError(42, [], ''), None]
with self.assertRaises(subprocess.CalledProcessError):
self.target.configure_ceph_keyring(interface)
self.target.configure_ceph_keyring(key)
self.check_call.reset_mock()
self.check_call.side_effect = [
subprocess.CalledProcessError(1, [], ''), None]
self.target.configure_ceph_keyring(interface)
self.target.configure_ceph_keyring(key)
self.check_call.assert_has_calls([
mock.call([
'ceph-authtool',
@ -140,13 +139,12 @@ class TestCephCharm(BaseOpenStackCharmTest):
self.patch_object(cpl.subprocess, 'check_call')
self.patch_object(cpl.shutil, 'chown')
self.patch_object(cpl.os, 'symlink')
interface = mock.MagicMock()
interface.key = 'KEY'
key = 'KEY'
self.patch_object(cpl.os.path, 'exists', return_value=True)
self.patch_object(cpl.os, 'readlink')
self.patch_object(cpl.os, 'remove')
self.readlink.side_effect = OSError
self.target.configure_ceph_keyring(interface)
self.target.configure_ceph_keyring(key)
self.isdir.assert_called_with('/var/lib/ceph/sarepta')
self.mkdir.assert_called_with('/var/lib/ceph/sarepta',
owner='root', group='root', perms=0o750)
@ -166,7 +164,7 @@ class TestCephCharm(BaseOpenStackCharmTest):
'/etc/ceph/ceph.client.sarepta.keyring')
self.readlink.side_effect = None
self.readlink.return_value = '/some/where/else'
self.target.configure_ceph_keyring(interface)
self.target.configure_ceph_keyring(key)
self.remove.assert_called_with('/etc/ceph/ceph.client.sarepta.keyring')
def test_install(self):