diff --git a/ironic/api/hooks.py b/ironic/api/hooks.py index 3c406e5548..764b0a434f 100644 --- a/ironic/api/hooks.py +++ b/ironic/api/hooks.py @@ -89,6 +89,15 @@ class ContextHook(hooks.PecanHook): show_password=show_password, **creds) + def after(self, state): + if state.request.context == {}: + # An incorrect url path will not create RequestContext + return + # NOTE(lintan): RequestContext will generate a request_id if no one + # passing outside, so it always contain a request_id. + request_id = state.request.context.request_id + state.response.headers['Openstack-Request-Id'] = request_id + class RPCHook(hooks.PecanHook): """Attach the rpcapi object to the request so controllers can get to it.""" diff --git a/ironic/tests/unit/api/test_hooks.py b/ironic/tests/unit/api/test_hooks.py index c4372a3c40..e29ea298f8 100644 --- a/ironic/tests/unit/api/test_hooks.py +++ b/ironic/tests/unit/api/test_hooks.py @@ -285,6 +285,26 @@ class TestContextHook(base.BaseApiTest): is_admin=False, roles=headers['X-Roles'].split(',')) + @mock.patch.object(context, 'RequestContext') + def test_context_hook_after_add_request_id(self, mock_ctx): + headers = fake_headers(admin=True) + reqstate = FakeRequestState(headers=headers) + reqstate.set_context() + reqstate.request.context.request_id = 'fake-id' + context_hook = hooks.ContextHook(None) + context_hook.after(reqstate) + self.assertIn('Openstack-Request-Id', + reqstate.response.headers) + self.assertEqual( + 'fake-id', + reqstate.response.headers['Openstack-Request-Id']) + + def test_context_hook_after_miss_context(self): + response = self.get_json('/bad/path', + expect_errors=True) + self.assertNotIn('Openstack-Request-Id', + response.headers) + class TestTrustedCallHook(base.BaseApiTest): def test_trusted_call_hook_not_admin(self): diff --git a/releasenotes/notes/opentack-baremetal-request-id-daa72b785eaaaa8d.yaml b/releasenotes/notes/opentack-baremetal-request-id-daa72b785eaaaa8d.yaml new file mode 100644 index 0000000000..fcc50f0774 --- /dev/null +++ b/releasenotes/notes/opentack-baremetal-request-id-daa72b785eaaaa8d.yaml @@ -0,0 +1,4 @@ +--- +features: + - Append request_id as ``Openstack-Request-Id`` header to the response. +