Update for new openstacksdk changes to masakari-monitors

In new openstacksdk release, 'Profile' is removed in favour of
entrypoints plugins. This patch makes masakari-monitor compatible
with such new changes in openstacksdk.

NOTE:
To fix masakari-monitor this patch requires openstacksdk version to
be minimum 0.13.0.

Change-Id: I788e9f24f50e54cf4d1111386fa1392bd1b99e00
This commit is contained in:
Kengo Takahara 2018-03-28 11:40:17 +09:00 committed by dineshbhor
parent 34d185deee
commit db914b4cc7
3 changed files with 22 additions and 80 deletions

View File

@ -17,30 +17,17 @@ from keystoneauth1.identity.generic import password as ks_password
from keystoneauth1 import session as ks_session
from openstack import connection
from openstack import exceptions
from openstack import version
if version.__version__.find('0.9.19') == 0 or \
version.__version__.find('0.10.0') == 0:
from openstack import profile
_new_sdk = False
else:
from masakariclient.sdk.ha.v1 import _proxy
from openstack import service_description
_new_sdk = True
from oslo_log import log as oslo_logging
from masakariclient.sdk.ha import ha_service
import masakarimonitors.conf
LOG = oslo_logging.getLogger(__name__)
CONF = masakarimonitors.conf.CONF
PROFILE_TYPE = "ha"
PROFILE_NAME = "masakari"
class SendNotification(object):
def _make_client_new(self):
def _make_client(self):
auth = ks_password.Password(
auth_url=CONF.api.auth_url,
username=CONF.api.username,
@ -49,51 +36,9 @@ class SendNotification(object):
project_name=CONF.api.project_name,
project_domain_id=CONF.api.project_domain_id)
session = ks_session.Session(auth=auth)
conn = connection.Connection(session=session)
desc = service_description.ServiceDescription(
service_type='ha', proxy_class=_proxy.Proxy)
conn = connection.Connection(
session=session, extra_services=[desc])
conn.add_service(desc)
if version.__version__.find('0.11.0') == 0:
client = conn.ha
else:
client = conn.ha.proxy_class(
session=session, service_type='ha')
return client
def _make_client_old(self):
# Make profile.
prof = profile.Profile()
prof._add_service(ha_service.HAService(
version=CONF.api.api_version))
prof.set_name(PROFILE_TYPE, PROFILE_NAME)
prof.set_region(PROFILE_TYPE, CONF.api.region)
prof.set_version(PROFILE_TYPE, CONF.api.api_version)
prof.set_interface(PROFILE_TYPE, CONF.api.api_interface)
# Make connection.
conn = connection.Connection(
auth_url=CONF.api.auth_url,
project_name=CONF.api.project_name,
username=CONF.api.username,
password=CONF.api.password,
project_domain_id=CONF.api.project_domain_id,
user_domain_id=CONF.api.user_domain_id,
profile=prof)
# Make client.
client = conn.ha
return client
def _make_client(self):
if _new_sdk:
return self._make_client_new()
else:
return self._make_client_old()
return conn.instance_ha
def send_notification(self, api_retry_max, api_retry_interval, event):
"""Send a notification.

View File

@ -21,7 +21,6 @@ from keystoneauth1.identity.generic import password as ks_password
from keystoneauth1 import session as ks_session
from openstack import connection
from openstack import exceptions
from openstack import service_description
from oslo_utils import timeutils
from masakarimonitors.ha import masakari
@ -61,37 +60,35 @@ class TestSendNotification(testtools.TestCase):
}
@mock.patch.object(connection, 'Connection')
@mock.patch.object(service_description, 'ServiceDescription')
@mock.patch.object(ks_session, 'Session')
@mock.patch.object(ks_password, 'Password')
def test_send_notification(self, mock_password, mock_session,
mock_service_description, mock_connection):
def test_send_notification(
self, mock_password, mock_session, mock_connection):
mock_conn = mock.Mock()
mock_client = mock.Mock()
mock_conn.ha.proxy_class.return_value = mock_client
mock_conn.instance_ha.return_value = mock.Mock()
mock_conn.instance_ha.create_notification.return_value = mock.Mock()
mock_connection.return_value = mock_conn
notifier = masakari.SendNotification()
notifier.send_notification(
self.api_retry_max, self.api_retry_interval, self.event)
mock_client.create_notification.assert_called_once_with(
mock_conn.instance_ha.create_notification.assert_called_once_with(
type=self.event['notification']['type'],
hostname=self.event['notification']['hostname'],
generated_time=self.event['notification']['generated_time'],
payload=self.event['notification']['payload'])
@mock.patch.object(connection, 'Connection')
@mock.patch.object(service_description, 'ServiceDescription')
@mock.patch.object(ks_session, 'Session')
@mock.patch.object(ks_password, 'Password')
def test_send_notification_409_error(self, mock_password, mock_session,
mock_service_description, mock_connection):
def test_send_notification_409_error(
self, mock_password, mock_session, mock_connection):
mock_conn = mock.Mock()
mock_client = mock.Mock()
mock_conn.ha.proxy_class.return_value = mock_client
mock_conn.instance_ha.return_value = mock.Mock()
mock_conn.instance_ha.create_notification.return_value = mock.Mock()
mock_connection.return_value = mock_conn
# TODO(samP): Remove attribute check and else case if
@ -103,12 +100,12 @@ class TestSendNotification(testtools.TestCase):
else:
status_ex = exceptions.HttpException(http_status=409)
mock_client.create_notification.side_effect = status_ex
mock_conn.instance_ha.create_notification.side_effect = status_ex
notifier = masakari.SendNotification()
notifier.send_notification(
self.api_retry_max, self.api_retry_interval, self.event)
mock_client.create_notification.assert_called_once_with(
mock_conn.instance_ha.create_notification.assert_called_once_with(
type=self.event['notification']['type'],
hostname=self.event['notification']['hostname'],
generated_time=self.event['notification']['generated_time'],
@ -116,15 +113,14 @@ class TestSendNotification(testtools.TestCase):
@mock.patch.object(eventlet.greenthread, 'sleep')
@mock.patch.object(connection, 'Connection')
@mock.patch.object(service_description, 'ServiceDescription')
@mock.patch.object(ks_session, 'Session')
@mock.patch.object(ks_password, 'Password')
def test_send_notification_500_error(self, mock_password, mock_session,
mock_service_description, mock_connection, mock_sleep):
def test_send_notification_500_error(
self, mock_password, mock_session, mock_connection, mock_sleep):
mock_conn = mock.Mock()
mock_client = mock.Mock()
mock_conn.ha.proxy_class.return_value = mock_client
mock_conn.instance_ha.return_value = mock.Mock()
mock_conn.instance_ha.create_notification.return_value = mock.Mock()
mock_connection.return_value = mock_conn
# TODO(samP): Remove attribute check and else case if
@ -136,17 +132,17 @@ class TestSendNotification(testtools.TestCase):
else:
status_ex = exceptions.HttpException(http_status=500)
mock_client.create_notification.side_effect = status_ex
mock_conn.instance_ha.create_notification.side_effect = status_ex
mock_sleep.return_value = None
notifier = masakari.SendNotification()
notifier.send_notification(
self.api_retry_max, self.api_retry_interval, self.event)
mock_client.create_notification.assert_called_with(
mock_conn.instance_ha.create_notification.assert_called_with(
type=self.event['notification']['type'],
hostname=self.event['notification']['hostname'],
generated_time=self.event['notification']['generated_time'],
payload=self.event['notification']['payload'])
self.assertEqual(self.api_retry_max + 1,
mock_client.create_notification.call_count)
mock_conn.instance_ha.create_notification.call_count)

View File

@ -3,6 +3,7 @@
# process, which may cause wedges in the gate later.
libvirt-python!=4.1.0,>=3.5.0 # LGPLv2+
openstacksdk>=0.13.0 # Apache-2.0
oslo.concurrency>=3.26.0 # Apache-2.0
oslo.config>=5.2.0 # Apache-2.0
oslo.i18n>=3.15.3 # Apache-2.0