messaging: remove most oslo.context usage

The RequestContextSerializer of oslo.messaging has been deprecated, and
actually we don't need it. Let's drop it and remove the oslo.context
usage in most use cases.

Change-Id: Ib70062d314f88b10880ae32f2831c88cb2cfe402
This commit is contained in:
Julien Danjou 2015-11-30 10:56:21 +01:00
parent e8a0be43d2
commit 3c08a6e586
8 changed files with 26 additions and 54 deletions

View File

@ -25,7 +25,6 @@ import uuid
import croniter
from oslo_config import cfg
from oslo_context import context
from oslo_log import log
from oslo_utils import netutils
from oslo_utils import timeutils
@ -488,7 +487,7 @@ def _send_notification(event, payload):
notifier = messaging.get_notifier(transport, publisher_id="aodh.api")
# FIXME(sileht): perhaps we need to copy some infos from the
# pecan request headers like nova does
notifier.info(context.RequestContext(), notification, payload)
notifier.info({}, notification, payload)
def stringify_timestamps(data):

View File

@ -1,7 +1,5 @@
#
# Copyright 2013 eNovance <licensing@enovance.com>
#
# Authors: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
# Copyright 2013-2015 eNovance <licensing@enovance.com>
#
# 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
@ -22,7 +20,6 @@ import json
import croniter
from oslo_config import cfg
from oslo_context import context
from oslo_log import log
from oslo_service import service as os_service
from oslo_utils import timeutils
@ -109,7 +106,7 @@ class Evaluator(object):
self._alarm_change_notifier = messaging.get_notifier(
transport, publisher_id="aodh.evaluator")
notification = "alarm.state_transition"
self._alarm_change_notifier.info(context.RequestContext(),
self._alarm_change_notifier.info({},
notification, payload)
def _refresh(self, alarm, state, reason, reason_data):

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2013 eNovance <licensing@enovance.com>
# Copyright 2013-2015 eNovance <licensing@enovance.com>
#
# 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
@ -18,6 +18,7 @@ from oslo_messaging import serializer as oslo_serializer
DEFAULT_URL = "__default__"
TRANSPORTS = {}
_SERIALIZER = oslo_serializer.JsonPayloadSerializer()
def setup():
@ -47,20 +48,16 @@ def get_transport(conf, url=None, optional=False, cache=True):
def get_rpc_server(conf, transport, topic, endpoint):
"""Return a configured oslo_messaging rpc server."""
target = oslo_messaging.Target(server=conf.host, topic=topic)
serializer = oslo_serializer.RequestContextSerializer(
oslo_serializer.JsonPayloadSerializer())
return oslo_messaging.get_rpc_server(transport, target,
[endpoint], executor='threading',
serializer=serializer)
serializer=_SERIALIZER)
def get_rpc_client(transport, retry=None, **kwargs):
"""Return a configured oslo_messaging RPCClient."""
target = oslo_messaging.Target(**kwargs)
serializer = oslo_serializer.RequestContextSerializer(
oslo_serializer.JsonPayloadSerializer())
return oslo_messaging.RPCClient(transport, target,
serializer=serializer,
serializer=_SERIALIZER,
retry=retry)
@ -74,7 +71,5 @@ def get_notification_listener(transport, targets, endpoints,
def get_notifier(transport, publisher_id):
"""Return a configured oslo_messaging notifier."""
serializer = oslo_serializer.RequestContextSerializer(
oslo_serializer.JsonPayloadSerializer())
notifier = oslo_messaging.Notifier(transport, serializer=serializer)
notifier = oslo_messaging.Notifier(transport, serializer=_SERIALIZER)
return notifier.prepare(publisher_id=publisher_id)

View File

@ -12,7 +12,6 @@
# under the License.
from oslo_config import cfg
from oslo_context import context
from oslo_log import log
import oslo_messaging
import six
@ -32,7 +31,6 @@ LOG = log.getLogger(__name__)
class AlarmNotifier(object):
def __init__(self, conf):
self.ctxt = context.get_admin_context().to_dict()
self.notifier = oslo_messaging.Notifier(
messaging.get_transport(conf),
driver='messagingv2',
@ -57,4 +55,4 @@ class AlarmNotifier(object):
'current': alarm.state,
'reason': six.text_type(reason),
'reason_data': reason_data}
self.notifier.sample(self.ctxt, 'alarm.update', payload)
self.notifier.sample({}, 'alarm.update', payload)

View File

@ -1,7 +1,5 @@
#
# Copyright 2013 eNovance <licensing@enovance.com>
#
# Authors: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
# Copyright 2013-2015 eNovance <licensing@enovance.com>
#
# 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
@ -16,7 +14,6 @@
# under the License.
from oslo_config import cfg
from oslo_context import context
from oslo_log import log
import six
@ -36,7 +33,6 @@ LOG = log.getLogger(__name__)
class RPCAlarmNotifier(object):
def __init__(self, conf):
self.ctxt = context.get_admin_context()
transport = messaging.get_transport(conf)
self.client = messaging.get_rpc_client(
transport, topic=conf.notifier_rpc_topic,
@ -52,7 +48,7 @@ class RPCAlarmNotifier(object):
'previous': previous,
'state': alarm.state})
return
self.client.cast(self.ctxt,
self.client.cast({},
'notify_alarm', data={
'actions': actions,
'alarm_id': alarm.alarm_id,

View File

@ -1631,18 +1631,7 @@ class TestAlarms(TestAlarmsBase):
'user_id']).issubset(payload.keys()))
endpoint.info.assert_called_once_with(
{'resource_uuid': None,
'domain': None,
'project_domain': None,
'auth_token': None,
'is_admin': False,
'user': None,
'tenant': None,
'read_only': False,
'show_deleted': False,
'user_identity': '- - - - -',
'request_id': mock.ANY,
'user_domain': None},
{},
'aodh.api', 'alarm.creation',
PayloadMatcher(), mock.ANY)

View File

@ -240,8 +240,7 @@ class TestEvaluate(base.TestEvaluatorBase):
def test_alarm_change_record(self, get_notifier, utcnow, mock_uuid):
# the context.RequestContext() method need to generate uuid,
# so we need to provide 'fake_uuid_0' and 'fake_uuid_1' for that.
mock_uuid.side_effect = ['fake_event_id_0', 'fake_uuid_0',
'fake_event_id_1', 'fake_uuid_1']
mock_uuid.side_effect = ['fake_event_id_0', 'fake_event_id_1']
change_notifier = mock.MagicMock()
get_notifier.return_value = change_notifier
utcnow.return_value = datetime.datetime(2015, 7, 26, 3, 33, 21, 876795)

View File

@ -1,5 +1,5 @@
#
# Copyright 2013-2014 eNovance
# Copyright 2013-2015 eNovance
#
# 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
@ -15,7 +15,6 @@
import mock
from oslo_config import fixture as fixture_config
from oslo_context import context
from oslo_serialization import jsonutils
from oslotest import mockpatch
import requests
@ -84,7 +83,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
'reason': 'Everything is on fire',
'reason_data': {'fire': 'everywhere'}
}
self.service.notify_alarm(context.get_admin_context(), data)
self.service.notify_alarm({}, data)
notifications = self.service.notifiers['test'].obj.notifications
self.assertEqual(1, len(notifications))
self.assertEqual((urlparse.urlsplit(data['actions'][0]),
@ -98,10 +97,10 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
notifications[0])
def test_notify_alarm_no_action(self):
self.service.notify_alarm(context.get_admin_context(), {})
self.service.notify_alarm({}, {})
def test_notify_alarm_log_action(self):
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
{
'actions': ['log://'],
'alarm_id': 'foobar',
@ -124,7 +123,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
action = 'http://host/action'
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
poster.assert_called_with(action, data=mock.ANY,
headers=mock.ANY)
@ -139,7 +138,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
self.CONF.set_override("rest_notifier_certificate_file", certificate)
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
poster.assert_called_with(action, data=mock.ANY,
headers=mock.ANY,
@ -157,7 +156,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
self.CONF.set_override("rest_notifier_certificate_key", key)
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
poster.assert_called_with(action, data=mock.ANY,
headers=mock.ANY,
@ -172,7 +171,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
self.CONF.set_override("rest_notifier_ssl_verify", False)
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
poster.assert_called_with(action, data=mock.ANY,
headers=mock.ANY,
@ -185,7 +184,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
action = 'https://host/action?aodh-alarm-ssl-verify=0'
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
poster.assert_called_with(action, data=mock.ANY,
headers=mock.ANY,
@ -200,7 +199,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
self.CONF.set_override("rest_notifier_ssl_verify", False)
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
poster.assert_called_with(action, data=mock.ANY,
headers=mock.ANY,
@ -219,7 +218,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
LOG = mock.MagicMock()
with mock.patch('aodh.notifier.LOG', LOG):
self.service.notify_alarm(
context.get_admin_context(),
{},
{
'actions': ['no-such-action-i-am-sure'],
'alarm_id': 'foobar',
@ -231,7 +230,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
LOG = mock.MagicMock()
with mock.patch('aodh.notifier.LOG', LOG):
self.service.notify_alarm(
context.get_admin_context(),
{},
{
'actions': ['no-such-action-i-am-sure://'],
'alarm_id': 'foobar',
@ -252,7 +251,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
lambda **kwargs: client))
with mock.patch.object(requests.Session, 'post') as poster:
self.service.notify_alarm(context.get_admin_context(),
self.service.notify_alarm({},
self._notification(action))
headers = {'X-Auth-Token': 'token_1234'}
headers.update(self.HTTP_HEADERS)