Added request_id and global_request_id to basic notifications

Change-Id: I667e82d3239ced3908a6bad7402f521b8e3ea564
Related-Bug: #1801095
This commit is contained in:
Artem Vasilyev 2019-02-04 13:46:56 +03:00
parent 30445574fc
commit 51a68525c6
3 changed files with 41 additions and 6 deletions

View File

@ -145,7 +145,8 @@ class Audit(object):
operation, operation,
resource_type, resource_type,
resource_id, resource_id,
actor_dict, initiator=initiator,
actor_dict=actor_dict,
public=public) public=public)
if CONF.notification_format == 'cadf' and public: if CONF.notification_format == 'cadf' and public:
@ -444,8 +445,8 @@ def _create_cadf_payload(operation, resource_type, resource_id,
target, event_type, reason=reason, **audit_kwargs) target, event_type, reason=reason, **audit_kwargs)
def _send_notification(operation, resource_type, resource_id, actor_dict=None, def _send_notification(operation, resource_type, resource_id, initiator=None,
public=True): actor_dict=None, public=True):
"""Send notification to inform observers about the affected resource. """Send notification to inform observers about the affected resource.
This method doesn't raise an exception when sending the notification fails. This method doesn't raise an exception when sending the notification fails.
@ -453,6 +454,7 @@ def _send_notification(operation, resource_type, resource_id, actor_dict=None,
:param operation: operation being performed (created, updated, or deleted) :param operation: operation being performed (created, updated, or deleted)
:param resource_type: type of resource being operated on :param resource_type: type of resource being operated on
:param resource_id: ID of resource being operated on :param resource_id: ID of resource being operated on
:param initiator: representation of the user that created the request
:param actor_dict: a dictionary containing the actor's ID and type :param actor_dict: a dictionary containing the actor's ID and type
:param public: if True (default), the event will be sent :param public: if True (default), the event will be sent
to the notifier API. to the notifier API.
@ -466,6 +468,12 @@ def _send_notification(operation, resource_type, resource_id, actor_dict=None,
payload['actor_type'] = actor_dict['type'] payload['actor_type'] = actor_dict['type']
payload['actor_operation'] = actor_dict['actor_operation'] payload['actor_operation'] = actor_dict['actor_operation']
if initiator:
payload['request_id'] = initiator.request_id
global_request_id = getattr(initiator, 'global_request_id', None)
if global_request_id:
payload['global_request_id'] = global_request_id
notify_event_callbacks(SERVICE, resource_type, operation, payload) notify_event_callbacks(SERVICE, resource_type, operation, payload)
# Only send this notification if the 'basic' format is used, otherwise # Only send this notification if the 'basic' format is used, otherwise

View File

@ -244,12 +244,13 @@ class BaseNotificationTest(test_v3.RestfulTestCase):
self._notifications = [] self._notifications = []
self._audits = [] self._audits = []
def fake_notify(operation, resource_type, resource_id, def fake_notify(operation, resource_type, resource_id, initiator=None,
actor_dict=None, public=True): actor_dict=None, public=True):
note = { note = {
'resource_id': resource_id, 'resource_id': resource_id,
'operation': operation, 'operation': operation,
'resource_type': resource_type, 'resource_type': resource_type,
'initiator': initiator,
'send_notification_called': True, 'send_notification_called': True,
'public': public} 'public': public}
if actor_dict: if actor_dict:
@ -364,7 +365,8 @@ class BaseNotificationTest(test_v3.RestfulTestCase):
'send_notification_called': True, 'send_notification_called': True,
'public': public} 'public': public}
for note in self._notifications: for note in self._notifications:
if expected == note: # compare only expected fields
if all(note.get(k) == v for k, v in expected.items()):
break break
else: else:
self.fail("Notification not sent.") self.fail("Notification not sent.")
@ -717,6 +719,30 @@ class NotificationsForEntities(BaseNotificationTest):
actor_id=user_ref['id'], actor_type='user', actor_id=user_ref['id'], actor_type='user',
actor_operation='removed') actor_operation='removed')
def test_initiator_request_id(self):
ref = unit.new_domain_ref()
self.post('/domains', body={'domain': ref})
note = self._notifications[-1]
initiator = note['initiator']
self.assertIsNotNone(initiator.request_id)
def test_initiator_global_request_id(self):
global_request_id = 'req-%s' % uuid.uuid4()
ref = unit.new_domain_ref()
self.post('/domains', body={'domain': ref},
headers={'X-OpenStack-Request-Id': global_request_id})
note = self._notifications[-1]
initiator = note['initiator']
self.assertEqual(
initiator.global_request_id, global_request_id)
def test_initiator_global_request_id_not_set(self):
ref = unit.new_domain_ref()
self.post('/domains', body={'domain': ref})
note = self._notifications[-1]
initiator = note['initiator']
self.assertFalse(hasattr(initiator, 'global_request_id'))
class CADFNotificationsForPCIDSSEvents(BaseNotificationTest): class CADFNotificationsForPCIDSSEvents(BaseNotificationTest):

View File

@ -2,4 +2,5 @@
features: features:
- > - >
[`bug 1801095 <https://bugs.launchpad.net/keystone/+bug/1801095>`_] [`bug 1801095 <https://bugs.launchpad.net/keystone/+bug/1801095>`_]
Request ID and global request ID have been added to CADF notifications. Request ID and global request ID have been added to both basic and CADF
notifications.