Skip to content

Commit dc4ca77

Browse files
author
Balazs Gibizer
committed
Add 'resource_request' to neutronv2/constants
This patch replaces of the string usage with referring to the new constant in neutronv2/constants.py Change-Id: I6fc3cb0651f65b2448fbbb58989c920974f076c3
1 parent d939ead commit dc4ca77

File tree

10 files changed

+43
-31
lines changed

10 files changed

+43
-31
lines changed

nova/api/openstack/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import nova.conf
3131
from nova import exception
3232
from nova.i18n import _
33+
from nova.network.neutronv2 import constants
3334
from nova import objects
3435
from nova import quota
3536
from nova import utils
@@ -578,9 +579,9 @@ def instance_has_port_with_resource_request(
578579
# instance has no port with resource request. If the instance is shelve
579580
# offloaded then we still have to hit neutron.
580581
search_opts = {'device_id': instance_uuid,
581-
'fields': ['resource_request']}
582+
'fields': [constants.RESOURCE_REQUEST]}
582583
ports = network_api.list_ports(context, **search_opts).get('ports', [])
583584
for port in ports:
584-
if port.get('resource_request'):
585+
if port.get(constants.RESOURCE_REQUEST):
585586
return True
586587
return False

nova/cmd/manage.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,15 +1676,16 @@ def _get_ports(ctxt, instance, neutron):
16761676
try:
16771677
return neutron.list_ports(
16781678
ctxt, device_id=instance.uuid,
1679-
fields=['id', 'resource_request', constants.BINDING_PROFILE]
1679+
fields=['id', constants.RESOURCE_REQUEST,
1680+
constants.BINDING_PROFILE]
16801681
)['ports']
16811682
except neutron_client_exc.NeutronClientException as e:
16821683
raise exception.UnableToQueryPorts(
16831684
instance_uuid=instance.uuid, error=six.text_type(e))
16841685

16851686
@staticmethod
16861687
def _has_request_but_no_allocation(port):
1687-
request = port.get('resource_request')
1688+
request = port.get(constants.RESOURCE_REQUEST)
16881689
binding_profile = port.get(constants.BINDING_PROFILE, {}) or {}
16891690
allocation = binding_profile.get(constants.ALLOCATION)
16901691
# We are defensive here about 'resources' and 'required' in the
@@ -1774,7 +1775,8 @@ def _get_port_allocation(
17741775
instance allocation dict.
17751776
"""
17761777
matching_rp_uuids = self._get_rps_in_tree_with_required_traits(
1777-
ctxt, node_uuid, port['resource_request']['required'], placement)
1778+
ctxt, node_uuid, port[constants.RESOURCE_REQUEST]['required'],
1779+
placement)
17781780

17791781
if len(matching_rp_uuids) > 1:
17801782
# If there is more than one such RP then it is an ambiguous
@@ -1796,7 +1798,7 @@ def _get_port_allocation(
17961798
raise exception.NoResourceProviderToHealFrom(
17971799
port_id=port['id'],
17981800
instance_uuid=instance_uuid,
1799-
traits=port['resource_request']['required'],
1801+
traits=port[constants.RESOURCE_REQUEST]['required'],
18001802
node_uuid=node_uuid)
18011803

18021804
# We found one RP that matches the traits. Assume that we can allocate
@@ -1806,7 +1808,7 @@ def _get_port_allocation(
18061808

18071809
port_allocation = {
18081810
rp_uuid: {
1809-
'resources': port['resource_request']['resources']
1811+
'resources': port[constants.RESOURCE_REQUEST]['resources']
18101812
}
18111813
}
18121814
return port_allocation
@@ -1880,7 +1882,7 @@ def _get_port_allocations_to_heal(
18801882
"traits for port %(port_uuid)s with resource request "
18811883
"%(request)s attached to instance %(instance_uuid)s") %
18821884
{"rp_uuid": rp_uuid, "port_uuid": port["id"],
1883-
"request": port.get("resource_request"),
1885+
"request": port.get(constants.RESOURCE_REQUEST),
18841886
"instance_uuid": instance.uuid})
18851887

18861888
return allocations, ports_to_heal

nova/compute/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from nova import image
6464
from nova import network
6565
from nova.network import model as network_model
66+
from nova.network.neutronv2 import constants
6667
from nova.network.security_group import openstack_driver
6768
from nova.network.security_group import security_group_base
6869
from nova import objects
@@ -4361,7 +4362,7 @@ def attach_interface(self, context, instance, network_id, port_id,
43614362
# need a new scheduling if resource on this host is not available.
43624363
if port_id:
43634364
port = self.network_api.show_port(context, port_id)
4364-
if port['port'].get('resource_request'):
4365+
if port['port'].get(constants.RESOURCE_REQUEST):
43654366
raise exception.AttachInterfaceWithQoSPolicyNotSupported(
43664367
instance_uuid=instance.uuid)
43674368

nova/network/neutronv2/api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def _create_port_minimal(self, port_client, instance, network_id,
462462
# such ports are currently not supported as they would at least
463463
# need resource allocation manipulation in placement but might also
464464
# need a new scheduling if resource on this host is not available.
465-
if port.get('resource_request', None):
465+
if port.get(constants.RESOURCE_REQUEST, None):
466466
msg = _(
467467
"The auto-created port %(port_id)s is being deleted due "
468468
"to its network having QoS policy.")
@@ -1003,7 +1003,7 @@ def allocate_for_instance(self, context, instance, vpn,
10031003
for port in requested_ports_dict.values():
10041004
# only communicate the allocations if the port has resource
10051005
# requests
1006-
if port.get('resource_request'):
1006+
if port.get(constants.RESOURCE_REQUEST):
10071007
profile = port.get(constants.BINDING_PROFILE, {})
10081008
# NOTE(gibi): In the resource provider mapping there can be
10091009
# more than one RP fulfilling a request group. But resource
@@ -1663,7 +1663,7 @@ def deallocate_port_for_instance(self, context, instance, port_id):
16631663
if port:
16641664
# if there is resource associated to this port then that needs to
16651665
# be deallocated so lets return info about such allocation
1666-
resource_request = port.get('resource_request')
1666+
resource_request = port.get(constants.RESOURCE_REQUEST)
16671667
allocated_rp = port.get(
16681668
constants.BINDING_PROFILE, {}).get(constants.ALLOCATION)
16691669
if resource_request and allocated_rp:
@@ -1950,7 +1950,7 @@ def _get_port_vnic_info(self, context, neutron, port_id):
19501950
port = self._show_port(
19511951
context, port_id, neutron_client=neutron,
19521952
fields=['binding:vnic_type', constants.BINDING_PROFILE,
1953-
'network_id', 'resource_request'])
1953+
'network_id', constants.RESOURCE_REQUEST])
19541954
network_id = port.get('network_id')
19551955
trusted = None
19561956
vnic_type = port.get('binding:vnic_type',
@@ -1962,7 +1962,7 @@ def _get_port_vnic_info(self, context, neutron, port_id):
19621962
# set depending on neutron configuration, e.g. if QoS rules are
19631963
# applied to the port/network and the port-resource-request API
19641964
# extension is enabled.
1965-
resource_request = port.get('resource_request', None)
1965+
resource_request = port.get(constants.RESOURCE_REQUEST, None)
19661966
return vnic_type, trusted, network_id, resource_request
19671967

19681968
def create_resource_requests(self, context, requested_networks,

nova/network/neutronv2/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
MIGRATING_ATTR = 'migrating_to'
2828
L3_NETWORK_TYPES = ['vxlan', 'gre', 'geneve']
2929
ALLOCATION = 'allocation'
30+
RESOURCE_REQUEST = 'resource_request'

nova/tests/fixtures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ class NeutronFixture(fixtures.Fixture):
12841284
}
12851285
],
12861286
'tenant_id': tenant_id,
1287-
'resource_request': {
1287+
neutron_constants.RESOURCE_REQUEST: {
12881288
"resources": {
12891289
orc.NET_BW_IGR_KILOBIT_PER_SEC: 1000,
12901290
orc.NET_BW_EGR_KILOBIT_PER_SEC: 1000},
@@ -1337,7 +1337,7 @@ class NeutronFixture(fixtures.Fixture):
13371337
}
13381338
],
13391339
'tenant_id': tenant_id,
1340-
'resource_request': {},
1340+
neutron_constants.RESOURCE_REQUEST: {},
13411341
'binding:vnic_type': 'direct',
13421342
}
13431343

@@ -1355,7 +1355,7 @@ class NeutronFixture(fixtures.Fixture):
13551355
}
13561356
],
13571357
'tenant_id': tenant_id,
1358-
'resource_request': {
1358+
neutron_constants.RESOURCE_REQUEST: {
13591359
"resources": {
13601360
orc.NET_BW_IGR_KILOBIT_PER_SEC: 10000,
13611361
orc.NET_BW_EGR_KILOBIT_PER_SEC: 10000},
@@ -1378,7 +1378,7 @@ class NeutronFixture(fixtures.Fixture):
13781378
}
13791379
],
13801380
'tenant_id': tenant_id,
1381-
'resource_request': {
1381+
neutron_constants.RESOURCE_REQUEST: {
13821382
"resources": {
13831383
orc.NET_BW_IGR_KILOBIT_PER_SEC: 10000,
13841384
orc.NET_BW_EGR_KILOBIT_PER_SEC: 10000},

nova/tests/functional/test_nova_manage.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from nova import config
2525
from nova import context
2626
from nova import exception
27+
from nova.network.neutronv2 import constants
2728
from nova import objects
2829
from nova import test
2930
from nova.tests import fixtures as nova_fixtures
@@ -770,7 +771,7 @@ def _add_resource_request_to_a_bound_port(self, port_id, resource_request):
770771
# _ports list is safe as it is re-created for each Neutron fixture
771772
# instance therefore for each individual test using that fixture.
772773
bound_port = self.neutron._ports[port_id]
773-
bound_port['resource_request'] = resource_request
774+
bound_port[constants.RESOURCE_REQUEST] = resource_request
774775

775776
def _create_server_with_missing_port_alloc(
776777
self, ports, resource_request=None):
@@ -822,7 +823,7 @@ def _assert_placement_updated(self, server, ports):
822823
# bridge RP
823824
total_request = collections.defaultdict(int)
824825
for port in ports:
825-
port_request = port['resource_request']['resources']
826+
port_request = port[constants.RESOURCE_REQUEST]['resources']
826827
for rc, amount in port_request.items():
827828
total_request[rc] += amount
828829
self.assertEqual(total_request, network_allocations)

nova/tests/functional/test_servers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from nova.compute import manager as compute_manager
3737
from nova import context
3838
from nova import exception
39+
from nova.network.neutronv2 import constants
3940
from nova import objects
4041
from nova.objects import block_device as block_device_obj
4142
from nova.scheduler import utils
@@ -5521,7 +5522,7 @@ def _create_networking_rp_tree(self, compute_rp_uuid):
55215522
self._create_sriov_networking_rp_tree(compute_rp_uuid)
55225523

55235524
def assertPortMatchesAllocation(self, port, allocations):
5524-
port_request = port['resource_request']['resources']
5525+
port_request = port[constants.RESOURCE_REQUEST]['resources']
55255526
for rc, amount in allocations.items():
55265527
self.assertEqual(port_request[rc], amount,
55275528
'port %s requested %d %s '
@@ -5540,8 +5541,9 @@ def _add_resource_request_to_a_bound_port(self, port_id):
55405541
# _ports list is safe as it is re-created for each Neutron fixture
55415542
# instance therefore for each individual test using that fixture.
55425543
bound_port = self.neutron._ports[port_id]
5543-
bound_port['resource_request'] = (
5544-
self.neutron.port_with_resource_request['resource_request'])
5544+
bound_port[constants.RESOURCE_REQUEST] = (
5545+
self.neutron.port_with_resource_request[
5546+
constants.RESOURCE_REQUEST])
55455547

55465548
def test_interface_attach_with_port_resource_request(self):
55475549
# create a server

nova/tests/unit/compute/test_compute_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from nova import exception
4444
from nova.image import api as image_api
4545
from nova.network.neutronv2 import api as neutron_api
46+
from nova.network.neutronv2 import constants
4647
from nova import objects
4748
from nova.objects import base as obj_base
4849
from nova.objects import block_device as block_device_obj
@@ -6200,7 +6201,7 @@ def test_attach_interface_qos_aware_port(self, mock_record):
62006201
with mock.patch.object(
62016202
self.compute_api.network_api, 'show_port',
62026203
return_value={'port': {
6203-
'resource_request': {
6204+
constants.RESOURCE_REQUEST: {
62046205
'resources': {'CUSTOM_RESOURCE_CLASS': 42}
62056206
}}}) as mock_show_port:
62066207
self.assertRaises(

nova/tests/unit/network/test_neutronv2.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,8 @@ def _test_get_port_vnic_info(self, mock_get_client,
21352135
if binding_vnic_type:
21362136
test_port['port']['binding:vnic_type'] = binding_vnic_type
21372137
if port_resource_request:
2138-
test_port['port']['resource_request'] = port_resource_request
2138+
test_port['port'][
2139+
constants.RESOURCE_REQUEST] = port_resource_request
21392140

21402141
mock_get_client.reset_mock()
21412142
mock_client = mock_get_client()
@@ -2147,7 +2148,7 @@ def _test_get_port_vnic_info(self, mock_get_client,
21472148

21482149
mock_client.show_port.assert_called_once_with(test_port['port']['id'],
21492150
fields=['binding:vnic_type', 'binding:profile', 'network_id',
2150-
'resource_request'])
2151+
constants.RESOURCE_REQUEST])
21512152
self.assertEqual(expected_vnic_type, vnic_type)
21522153
self.assertEqual('net-id', network_id)
21532154
self.assertIsNone(trusted)
@@ -3523,7 +3524,7 @@ def test_get_port_vnic_info_trusted(self, mock_get_client):
35233524

35243525
mock_client.show_port.assert_called_once_with(test_port['port']['id'],
35253526
fields=['binding:vnic_type', 'binding:profile', 'network_id',
3526-
'resource_request'])
3527+
constants.RESOURCE_REQUEST])
35273528
self.assertEqual(model.VNIC_TYPE_DIRECT, vnic_type)
35283529
self.assertEqual('net-id', network_id)
35293530
self.assertTrue(trusted)
@@ -3968,7 +3969,8 @@ def test_create_port_minimal_raise_qos_not_supported(self):
39683969
mock_client = mock.MagicMock()
39693970
mock_client.create_port.return_value = {'port': {
39703971
'id': uuids.port_id,
3971-
'resource_request': {'resources': {'CUSTOM_RESOURCE_CLASS': 42}}
3972+
constants.RESOURCE_REQUEST: {
3973+
'resources': {'CUSTOM_RESOURCE_CLASS': 42}}
39723974
}}
39733975

39743976
exc = self.assertRaises(exception.NetworksWithQoSPolicyNotSupported,
@@ -3989,7 +3991,8 @@ def test_create_port_minimal_raise_qos_not_supported_cleanup_fails(
39893991
mock_client = mock.MagicMock()
39903992
mock_client.create_port.return_value = {'port': {
39913993
'id': uuids.port_id,
3992-
'resource_request': {'resources': {'CUSTOM_RESOURCE_CLASS': 42}}
3994+
constants.RESOURCE_REQUEST: {
3995+
'resources': {'CUSTOM_RESOURCE_CLASS': 42}}
39933996
}}
39943997
mock_client.delete_port.side_effect = \
39953998
exceptions.NeutronClientException()
@@ -5038,7 +5041,7 @@ def test_deallocate_port_for_instance_port_with_allocation(
50385041
mock_client = mock.Mock()
50395042
mock_client.show_port.return_value = {
50405043
'port': {
5041-
'resource_request': {
5044+
constants.RESOURCE_REQUEST: {
50425045
'resources': {
50435046
'NET_BW_EGR_KILOBIT_PER_SEC': 1000
50445047
}
@@ -5178,7 +5181,7 @@ def test_port_with_resource_request_has_allocation_in_binding(
51785181
'tenant_id': uuids.project_id,
51795182
'network_id': uuids.networkid_1,
51805183
'mac_address': 'fake-mac',
5181-
'resource_request': 'fake-request'
5184+
constants.RESOURCE_REQUEST: 'fake-request'
51825185
}
51835186
mock_show_port.return_value = port
51845187
mock_get_client.return_value.list_networks.return_value = {

0 commit comments

Comments
 (0)