Use Ceph helpers from ``charms.openstack``

The Ceph relation adapter and keyring helper code was moved to
``charms.openstack`` as part of development of the
``ceph-rbd-mirror`` charm.

Port the Gnocchi charm to re-use this code.

Depends-On: If1d645f4708e27b724f93cac0e14431137c885d7
Depends-On: I97b4c30dabfcf65ec3fb876c2a09a44172be85e7
Change-Id: If0da7f1efcfa6b61aa210edcd76e9c3392e0e8e9
This commit is contained in:
Frode Nordahl 2019-04-23 15:35:37 +02:00
parent baca1527e4
commit 5b21b6cea5
No known key found for this signature in database
GPG Key ID: 6A5D59A3BA48373F
4 changed files with 13 additions and 97 deletions

View File

@ -23,6 +23,7 @@ import charmhelpers.core.host as host
import charms_openstack.charm
import charms_openstack.adapters as adapters
import charms_openstack.ip as os_ip
import charms_openstack.plugins
GNOCCHI_DIR = '/etc/gnocchi'
@ -75,28 +76,6 @@ def ceph_config(config):
return CEPH_CONF
# TODO(jamespage): charms.openstack
class StorageCephRelationAdapter(adapters.OpenStackRelationAdapter):
"""
Adapter for the CephClientRequires relation interface.
"""
interface_type = "ceph-client"
@property
def monitors(self):
"""
Comma separated list of hosts that should be used
to access Ceph.
"""
hosts = sorted(self.relation.mon_hosts())
if len(hosts) > 0:
return ','.join(hosts)
else:
return None
class GnocchiCharmRelationAdapaters(adapters.OpenStackAPIRelationAdapters):
"""
@ -104,14 +83,15 @@ class GnocchiCharmRelationAdapaters(adapters.OpenStackAPIRelationAdapters):
"""
relation_adapters = {
'storage_ceph': StorageCephRelationAdapter,
'storage_ceph': charms_openstack.plugins.CephRelationAdapter,
'shared_db': adapters.DatabaseRelationAdapter,
'cluster': adapters.PeerHARelationAdapter,
'coordinator_memcached': adapters.MemcacheRelationAdapter,
}
class GnochiCharmBase(charms_openstack.charm.HAOpenStackCharm):
class GnochiCharmBase(charms_openstack.charm.HAOpenStackCharm,
charms_openstack.plugins.BaseOpenStackCephCharm):
"""
Base class for shared charm functions for all package types

View File

@ -12,16 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import charms_openstack.charm as charm
import charms.reactive as reactive
import charm.openstack.gnocchi as gnocchi # noqa
import charmhelpers.contrib.storage.linux.ceph as ceph_helper
import charmhelpers.core.hookenv as hookenv
import charmhelpers.core.host as host
charm.use_defaults(
'charm.installed',
@ -85,22 +81,14 @@ def storage_ceph_connected(ceph):
@reactive.when('storage-ceph.available')
def configure_ceph(ceph):
with charm.provide_charm_instance() as charm_class:
# TODO(jamespage): refactor to avoid massaging helper
ceph_helper.KEYRING = charm_class.ceph_keyring
host.mkdir(os.path.dirname(charm_class.ceph_keyring))
ceph_helper.ensure_ceph_keyring(service=hookenv.service_name(),
key=ceph.key(),
user=charm_class.gnocchi_user,
group=charm_class.gnocchi_group)
with charm.provide_charm_instance() as charm_instance:
charm_instance.configure_ceph_keyring(ceph.key())
@reactive.when_not('storage-ceph.connected')
def storage_ceph_disconnected():
with charm.provide_charm_instance() as charm_class:
# TODO(jamespage): refactor to avoid massaging helper
ceph_helper.KEYRING = charm_class.ceph_keyring
ceph_helper.delete_keyring(hookenv.service_name())
with charm.provide_charm_instance() as charm_instance:
charm_instance.delete_ceph_keyring()
@reactive.when('metric-service.connected')

View File

@ -1,40 +0,0 @@
# Copyright 2017 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import print_function
import mock
import charms_openstack.test_utils as test_utils
import charm.openstack.gnocchi as gnocchi
class TestAdapters(test_utils.PatchHelper):
_mons = [
'1.2.3.4:123',
'1.2.3.5:123',
'1.2.3.6:123',
]
def test_storage_ceph(self):
adapter = gnocchi.StorageCephRelationAdapter()
adapter.relation = mock.MagicMock()
adapter.relation.mon_hosts.return_value = self._mons
self.assertEqual(adapter.monitors,
','.join(self._mons))
adapter.relation.mon_hosts.return_value = []
self.assertEqual(adapter.monitors, None)

View File

@ -114,29 +114,17 @@ class TestHandlers(test_utils.PatchHelper):
'mygnocchi',
)
@mock.patch.object(handlers, 'os')
@mock.patch.object(handlers, 'hookenv')
@mock.patch.object(handlers, 'ceph_helper')
def test_configure_ceph(self, mock_ceph_helper, mock_hookenv, mock_os):
def test_configure_ceph(self):
mock_ceph = mock.MagicMock()
mock_ceph.key.return_value = 'testkey'
mock_hookenv.service_name.return_value = 'gnocchi'
handlers.configure_ceph(mock_ceph)
mock_ceph_helper.ensure_ceph_keyring.assert_called_once_with(
service='gnocchi',
key='testkey',
user='gnocchi',
group='gnocchi'
)
self.gnocchi_charm.configure_ceph_keyring.assert_called_once_with(
'testkey')
mock_ceph.key.assert_called_once_with()
@mock.patch.object(handlers, 'hookenv')
@mock.patch.object(handlers, 'ceph_helper')
def test_storage_ceph_disconnected(self, mock_ceph_helper,
mock_hookenv):
mock_hookenv.service_name.return_value = 'gnocchi'
def test_storage_ceph_disconnected(self):
handlers.storage_ceph_disconnected()
mock_ceph_helper.delete_keyring.assert_called_once_with('gnocchi')
self.gnocchi_charm.delete_ceph_keyring.assert_called_once_with()
@mock.patch.object(handlers.reactive.flags, 'is_flag_set')
def test_provide_gnocchi_url(self, mock_is_flag_set):