Skip to content

Commit e11b589

Browse files
Fix no propagation of nova context request_id
Nova context request_id is not propagated for port binding operations in neutron. So fix it. Change-Id: I76163c46b1f01ba7ff592d162b106ea2e5bb34cb Closes-Bug: #1829914
1 parent bea9058 commit e11b589

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

nova/network/neutronv2/api.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from neutronclient.common import exceptions as neutron_client_exc
2323
from neutronclient.v2_0 import client as clientv20
2424
from oslo_log import log as logging
25+
from oslo_middleware import request_id
2526
from oslo_utils import excutils
2627
from oslo_utils import strutils
2728
from oslo_utils import uuidutils
@@ -1290,7 +1291,7 @@ def bind_ports_to_host(self, context, instance, host,
12901291
binding['profile'] = port_profiles[port_id]
12911292

12921293
data = dict(binding=binding)
1293-
resp = self._create_port_binding(client, port_id, data)
1294+
resp = self._create_port_binding(context, client, port_id, data)
12941295
if resp:
12951296
bindings_by_port_id[port_id] = resp.json()['binding']
12961297
else:
@@ -1313,9 +1314,10 @@ def bind_ports_to_host(self, context, instance, host,
13131314
return bindings_by_port_id
13141315

13151316
@staticmethod
1316-
def _create_port_binding(client, port_id, data):
1317+
def _create_port_binding(context, client, port_id, data):
13171318
"""Creates a port binding with the specified data.
13181319
1320+
:param context: The request context for the operation.
13191321
:param client: keystoneauth1.adapter.Adapter
13201322
:param port_id: The ID of the port on which to create the binding.
13211323
:param data: dict of port binding data (requires at least the host),
@@ -1324,8 +1326,9 @@ def _create_port_binding(client, port_id, data):
13241326
{'binding': {'host': 'dest.host.com'}}
13251327
:return: requests.Response object
13261328
"""
1327-
return client.post('/v2.0/ports/%s/bindings' % port_id,
1328-
json=data, raise_exc=False)
1329+
return client.post(
1330+
'/v2.0/ports/%s/bindings' % port_id, json=data, raise_exc=False,
1331+
headers={request_id.INBOUND_HEADER: context.global_id})
13291332

13301333
def delete_port_binding(self, context, port_id, host):
13311334
"""Delete the port binding for the given port ID and host
@@ -1340,7 +1343,7 @@ def delete_port_binding(self, context, port_id, host):
13401343
response is received from neutron.
13411344
"""
13421345
client = _get_ksa_client(context, admin=True)
1343-
resp = self._delete_port_binding(client, port_id, host)
1346+
resp = self._delete_port_binding(context, client, port_id, host)
13441347
if resp:
13451348
LOG.debug('Deleted binding for port %s and host %s.',
13461349
port_id, host)
@@ -1357,16 +1360,18 @@ def delete_port_binding(self, context, port_id, host):
13571360
port_id=port_id, host=host)
13581361

13591362
@staticmethod
1360-
def _delete_port_binding(client, port_id, host):
1363+
def _delete_port_binding(context, client, port_id, host):
13611364
"""Deletes the binding for the given host on the given port.
13621365
1366+
:param context: The request context for the operation.
13631367
:param client: keystoneauth1.adapter.Adapter
13641368
:param port_id: ID of the port from which to delete the binding
13651369
:param host: A string name of the host on which the port is bound
13661370
:return: requests.Response object
13671371
"""
1368-
return client.delete('/v2.0/ports/%s/bindings/%s' % (port_id, host),
1369-
raise_exc=False)
1372+
return client.delete(
1373+
'/v2.0/ports/%s/bindings/%s' % (port_id, host), raise_exc=False,
1374+
headers={request_id.INBOUND_HEADER: context.global_id})
13701375

13711376
def activate_port_binding(self, context, port_id, host):
13721377
"""Activates an inactive port binding.
@@ -1387,7 +1392,8 @@ def activate_port_binding(self, context, port_id, host):
13871392
# to ACTIVE, it's more like a POST action method in the compute API.
13881393
resp = client.put(
13891394
'/v2.0/ports/%s/bindings/%s/activate' % (port_id, host),
1390-
raise_exc=False)
1395+
raise_exc=False,
1396+
headers={request_id.INBOUND_HEADER: context.global_id})
13911397
if resp:
13921398
LOG.debug('Activated binding for port %s and host %s.',
13931399
port_id, host)
@@ -2693,8 +2699,10 @@ def migrate_instance_start(self, context, instance, migration):
26932699
# Not all compute migration flows use the port binding-extended
26942700
# API yet, so first check to see if there is a binding for the
26952701
# port and destination host.
2696-
resp = client.get('/v2.0/ports/%s/bindings/%s' %
2697-
(vif['id'], dest_host), raise_exc=False)
2702+
resp = client.get(
2703+
'/v2.0/ports/%s/bindings/%s' % (vif['id'], dest_host),
2704+
raise_exc=False,
2705+
headers={request_id.INBOUND_HEADER: context.global_id})
26982706
if resp:
26992707
if resp.json()['binding']['status'] != 'ACTIVE':
27002708
self.activate_port_binding(context, vif['id'], dest_host)

nova/tests/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,13 +1503,13 @@ def setUp(self):
15031503
lambda *args, **kwargs: self)
15041504

15051505
@staticmethod
1506-
def fake_create_port_binding(client, port_id, data):
1506+
def fake_create_port_binding(context, client, port_id, data):
15071507
# TODO(mriedem): Make this smarter by keeping track of our bindings
15081508
# per port so we can reflect the status accurately.
15091509
return fake_requests.FakeResponse(200, content=jsonutils.dumps(data))
15101510

15111511
@staticmethod
1512-
def fake_delete_port_binding(client, port_id, host):
1512+
def fake_delete_port_binding(context, client, port_id, host):
15131513
# TODO(mriedem): Make this smarter by keeping track of our bindings
15141514
# per port so we can reflect the status accurately.
15151515
return fake_requests.FakeResponse(204)

nova/tests/unit/network/test_neutronv2.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5714,7 +5714,8 @@ def test_migrate_instance_start_activate(self, get_client_mock):
57145714
self.context, instance, migration)
57155715
activate.assert_called_once_with(self.context, uuids.port_id, 'dest')
57165716
get_client_mock.return_value.get.assert_called_once_with(
5717-
'/v2.0/ports/%s/bindings/dest' % uuids.port_id, raise_exc=False)
5717+
'/v2.0/ports/%s/bindings/dest' % uuids.port_id, raise_exc=False,
5718+
headers={'X-Openstack-Request-Id': self.context.global_id})
57185719

57195720
@mock.patch('nova.network.neutronv2.api._get_ksa_client')
57205721
def test_migrate_instance_start_already_active(self, get_client_mock):
@@ -5736,7 +5737,8 @@ def test_migrate_instance_start_already_active(self, get_client_mock):
57365737
self.api.migrate_instance_start(
57375738
self.context, instance, migration)
57385739
get_client_mock.return_value.get.assert_called_once_with(
5739-
'/v2.0/ports/%s/bindings/dest' % uuids.port_id, raise_exc=False)
5740+
'/v2.0/ports/%s/bindings/dest' % uuids.port_id, raise_exc=False,
5741+
headers={'X-Openstack-Request-Id': self.context.global_id})
57405742

57415743
@mock.patch('nova.network.neutronv2.api._get_ksa_client')
57425744
def test_migrate_instance_start_no_bindings(self, get_client_mock):
@@ -5760,7 +5762,8 @@ def test_migrate_instance_start_no_bindings(self, get_client_mock):
57605762
self.api.migrate_instance_start(
57615763
self.context, instance, migration)
57625764
get_client_mock.return_value.get.assert_called_once_with(
5763-
'/v2.0/ports/%s/bindings/dest' % uuids.port1, raise_exc=False)
5765+
'/v2.0/ports/%s/bindings/dest' % uuids.port1, raise_exc=False,
5766+
headers={'X-Openstack-Request-Id': self.context.global_id})
57645767

57655768
@mock.patch('nova.network.neutronv2.api._get_ksa_client')
57665769
def test_migrate_instance_start_get_error(self, get_client_mock):
@@ -5782,10 +5785,14 @@ def test_migrate_instance_start_get_error(self, get_client_mock):
57825785
self.context, instance, migration)
57835786
self.assertEqual(2, get_client_mock.return_value.get.call_count)
57845787
get_client_mock.return_value.get.assert_has_calls([
5785-
mock.call('/v2.0/ports/%s/bindings/dest' % uuids.port1,
5786-
raise_exc=False),
5787-
mock.call('/v2.0/ports/%s/bindings/dest' % uuids.port2,
5788-
raise_exc=False)])
5788+
mock.call(
5789+
'/v2.0/ports/%s/bindings/dest' % uuids.port1,
5790+
raise_exc=False,
5791+
headers={'X-Openstack-Request-Id': self.context.global_id}),
5792+
mock.call(
5793+
'/v2.0/ports/%s/bindings/dest' % uuids.port2,
5794+
raise_exc=False,
5795+
headers={'X-Openstack-Request-Id': self.context.global_id})])
57895796

57905797

57915798
class TestNeutronv2ModuleMethods(test.NoDBTestCase):
@@ -6286,7 +6293,8 @@ def test_activate_port_binding(self, mock_client):
62866293
'fake-host')
62876294
mock_client.return_value.put.assert_called_once_with(
62886295
'/v2.0/ports/%s/bindings/fake-host/activate' % uuids.port_id,
6289-
raise_exc=False)
6296+
raise_exc=False,
6297+
headers={'X-Openstack-Request-Id': self.context.global_id})
62906298

62916299
@mock.patch('nova.network.neutronv2.api._get_ksa_client')
62926300
@mock.patch('nova.network.neutronv2.api.LOG.warning')
@@ -6298,7 +6306,8 @@ def test_activate_port_binding_already_active(
62986306
'fake-host')
62996307
mock_client.return_value.put.assert_called_once_with(
63006308
'/v2.0/ports/%s/bindings/fake-host/activate' % uuids.port_id,
6301-
raise_exc=False)
6309+
raise_exc=False,
6310+
headers={'X-Openstack-Request-Id': self.context.global_id})
63026311
self.assertEqual(1, mock_log_warning.call_count)
63036312
self.assertIn('is already active', mock_log_warning.call_args[0][0])
63046313

@@ -6311,7 +6320,8 @@ def test_activate_port_binding_fails(self, mock_client):
63116320
self.context, uuids.port_id, 'fake-host')
63126321
mock_client.return_value.put.assert_called_once_with(
63136322
'/v2.0/ports/%s/bindings/fake-host/activate' % uuids.port_id,
6314-
raise_exc=False)
6323+
raise_exc=False,
6324+
headers={'X-Openstack-Request-Id': self.context.global_id})
63156325

63166326

63176327
class TestAllocateForInstance(test.NoDBTestCase):

0 commit comments

Comments
 (0)