Update for upcoming openstacksdk changes to masakari-monitors

In the next openstacksdk release, Profile is going away as are entrypoints
plugins. This handles fixing most of that in a way that should work with
both old and new.

Note: This code has been tested with a version other than
openstacksdk-0.10.0.

Change-Id: Ic6390b0a479529600d246e3ced9e7278f08054df
This commit is contained in:
Kengo Takahara 2018-03-05 17:16:27 +09:00
parent 41653bc953
commit 40ed4d77b5
2 changed files with 97 additions and 119 deletions

View File

@ -13,9 +13,19 @@
# limitations under the License.
import eventlet
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 profile
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
@ -30,29 +40,60 @@ PROFILE_NAME = "masakari"
class SendNotification(object):
def _get_connection(self, api_version, region, interface, auth_url,
project_name, username, password, project_domain_id,
user_domain_id):
def _make_client_new(self):
auth = ks_password.Password(
auth_url=CONF.api.auth_url,
username=CONF.api.username,
password=CONF.api.password,
user_domain_id=CONF.api.user_domain_id,
project_name=CONF.api.project_name,
project_domain_id=CONF.api.project_domain_id)
session = ks_session.Session(auth=auth)
# Create profile object.
prof = profile.Profile()
prof._add_service(ha_service.HAService(version=api_version))
prof.set_name(PROFILE_TYPE, PROFILE_NAME)
prof.set_region(PROFILE_TYPE, region)
prof.set_version(PROFILE_TYPE, api_version)
prof.set_interface(PROFILE_TYPE, interface)
# Get connection.
desc = service_description.ServiceDescription(
service_type='ha', proxy_class=_proxy.Proxy)
conn = connection.Connection(
auth_url=auth_url,
project_name=project_name,
username=username,
password=password,
project_domain_id=project_domain_id,
user_domain_id=user_domain_id,
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)
return conn
# 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()
def send_notification(self, api_retry_max, api_retry_interval, event):
"""Send a notification.
@ -68,23 +109,14 @@ class SendNotification(object):
LOG.info("Send a notification. %s", event)
# Get connection.
conn = self._get_connection(
api_version=CONF.api.api_version,
region=CONF.api.region,
interface=CONF.api.api_interface,
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)
# Get client.
client = self._make_client()
# Send a notification.
retry_count = 0
while True:
try:
response = conn.ha.create_notification(
response = client.create_notification(
type=event['notification']['type'],
hostname=event['notification']['hostname'],
generated_time=event['notification']['generated_time'],

View File

@ -17,12 +17,13 @@ import testtools
import uuid
import eventlet
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 profile
from openstack import service_description
from oslo_utils import timeutils
from masakariclient.sdk.ha import ha_service
from masakarimonitors.ha import masakari
from masakarimonitors.objects import event_constants as ec
@ -60,55 +61,38 @@ class TestSendNotification(testtools.TestCase):
}
@mock.patch.object(connection, 'Connection')
@mock.patch.object(profile, 'Profile')
def test_send_notification(self,
mock_Profile,
mock_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):
mock_prof = mock.Mock()
mock_Profile.return_value = mock_prof
mock_conn = mock.Mock()
mock_Connection.return_value = mock_conn
mock_client = mock.Mock()
mock_conn.ha.proxy_class.return_value = mock_client
mock_connection.return_value = mock_conn
notifier = masakari.SendNotification()
notifier.send_notification(
self.api_retry_max, self.api_retry_interval, self.event)
mock_prof._add_service.assert_called_once_with(
ha_service.HAService(version='v1'))
mock_prof.set_name.assert_called_once_with(
PROFILE_TYPE, PROFILE_NAME)
mock_prof.set_region.assert_called_once_with(
PROFILE_TYPE, 'RegionOne')
mock_prof.set_version.assert_called_once_with(
PROFILE_TYPE, 'v1')
mock_prof.set_interface.assert_called_once_with(
PROFILE_TYPE, 'public')
mock_Connection.assert_called_once_with(
auth_url=None,
project_name=None,
username=None,
password=None,
project_domain_id=None,
user_domain_id=None,
profile=mock_prof)
mock_conn.ha.create_notification.assert_called_once_with(
mock_client.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(profile, 'Profile')
def test_send_notification_409_error(self,
mock_Profile,
mock_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):
mock_prof = mock.Mock()
mock_Profile.return_value = mock_prof
mock_conn = mock.Mock()
mock_Connection.return_value = mock_conn
mock_client = mock.Mock()
mock_conn.ha.proxy_class.return_value = mock_client
mock_connection.return_value = mock_conn
# TODO(samP): Remove attribute check and else case if
# openstacksdk is bumped up from '>=0.9.19' to '>=0.10.0'
@ -119,31 +103,12 @@ class TestSendNotification(testtools.TestCase):
else:
status_ex = exceptions.HttpException(http_status=409)
mock_conn.ha.create_notification.side_effect = status_ex
mock_client.create_notification.side_effect = status_ex
notifier = masakari.SendNotification()
notifier.send_notification(
self.api_retry_max, self.api_retry_interval, self.event)
mock_prof._add_service.assert_called_once_with(
ha_service.HAService(version='v1'))
mock_prof.set_name.assert_called_once_with(
PROFILE_TYPE, PROFILE_NAME)
mock_prof.set_region.assert_called_once_with(
PROFILE_TYPE, 'RegionOne')
mock_prof.set_version.assert_called_once_with(
PROFILE_TYPE, 'v1')
mock_prof.set_interface.assert_called_once_with(
PROFILE_TYPE, 'public')
mock_Connection.assert_called_once_with(
auth_url=None,
project_name=None,
username=None,
password=None,
project_domain_id=None,
user_domain_id=None,
profile=mock_prof)
mock_conn.ha.create_notification.assert_called_once_with(
mock_client.create_notification.assert_called_once_with(
type=self.event['notification']['type'],
hostname=self.event['notification']['hostname'],
generated_time=self.event['notification']['generated_time'],
@ -151,16 +116,16 @@ class TestSendNotification(testtools.TestCase):
@mock.patch.object(eventlet.greenthread, 'sleep')
@mock.patch.object(connection, 'Connection')
@mock.patch.object(profile, 'Profile')
def test_send_notification_500_error(self,
mock_Profile,
mock_Connection,
mock_sleep):
@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):
mock_prof = mock.Mock()
mock_Profile.return_value = mock_prof
mock_conn = mock.Mock()
mock_Connection.return_value = mock_conn
mock_client = mock.Mock()
mock_conn.ha.proxy_class.return_value = mock_client
mock_connection.return_value = mock_conn
# TODO(samP): Remove attribute check and else case if
# openstacksdk is bumped up from '>=0.9.19' to '>=0.10.0'
@ -171,36 +136,17 @@ class TestSendNotification(testtools.TestCase):
else:
status_ex = exceptions.HttpException(http_status=500)
mock_conn.ha.create_notification.side_effect = status_ex
mock_client.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_prof._add_service.assert_called_once_with(
ha_service.HAService(version='v1'))
mock_prof.set_name.assert_called_once_with(
PROFILE_TYPE, PROFILE_NAME)
mock_prof.set_region.assert_called_once_with(
PROFILE_TYPE, 'RegionOne')
mock_prof.set_version.assert_called_once_with(
PROFILE_TYPE, 'v1')
mock_prof.set_interface.assert_called_once_with(
PROFILE_TYPE, 'public')
mock_Connection.assert_called_once_with(
auth_url=None,
project_name=None,
username=None,
password=None,
project_domain_id=None,
user_domain_id=None,
profile=mock_prof)
mock_conn.ha.create_notification.assert_called_with(
mock_client.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_conn.ha.create_notification.call_count)
mock_client.create_notification.call_count)