Skip to content

Commit e8c7315

Browse files
committed
Allow setting network-segment on subnet update
To enable the possibility to migrate a non-routed network to a routed network allow updating the segment_id of a subnet. Change-Id: I3ebae2ff28d5d4e5373ebd1f52194f8c52071b88 Partial-Bug: bug/1692490 Depends-On: I1aee29dfb59e9769ec0f1cb1f5d2933bc5dc0dc5
1 parent c18f93b commit e8c7315

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

doc/source/cli/command-objects/subnet.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Set subnet properties
265265
[--dns-nameserver <dns-nameserver>]
266266
[--no-dns-nameserver]
267267
[--gateway <gateway-ip>]
268+
[--network-segment <network-segment>]
268269
[--host-route destination=<subnet>,gateway=<ip-address>]
269270
[--no-host-route]
270271
[--service-type <service-type>]
@@ -310,6 +311,13 @@ Set subnet properties
310311
'none': This subnet will not use a gateway,
311312
e.g.: ``--gateway 192.168.9.1``, ``--gateway none``.
312313
314+
.. option:: --network-segment <network-segment>
315+
316+
Network segment to associate with this subnet (name or ID). It is only
317+
allowed to set the segment if the current value is `None`, the network
318+
must also have only one segment and only one subnet can exist on the
319+
network.
320+
313321
.. option:: --host-route destination=<subnet>,gateway=<ip-address>
314322
315323
Additional route for this subnet e.g.:

openstackclient/network/v2/subnet.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def convert_entries_to_gateway(entries):
167167

168168
def _get_attrs(client_manager, parsed_args, is_create=True):
169169
attrs = {}
170+
client = client_manager.network
170171
if 'name' in parsed_args and parsed_args.name is not None:
171172
attrs['name'] = str(parsed_args.name)
172173

@@ -179,7 +180,6 @@ def _get_attrs(client_manager, parsed_args, is_create=True):
179180
parsed_args.project_domain,
180181
).id
181182
attrs['tenant_id'] = project_id
182-
client = client_manager.network
183183
attrs['network_id'] = client.find_network(parsed_args.network,
184184
ignore_missing=False).id
185185
if parsed_args.subnet_pool is not None:
@@ -200,10 +200,10 @@ def _get_attrs(client_manager, parsed_args, is_create=True):
200200
attrs['ipv6_ra_mode'] = parsed_args.ipv6_ra_mode
201201
if parsed_args.ipv6_address_mode is not None:
202202
attrs['ipv6_address_mode'] = parsed_args.ipv6_address_mode
203-
if parsed_args.network_segment is not None:
204-
attrs['segment_id'] = client.find_segment(
205-
parsed_args.network_segment, ignore_missing=False).id
206203

204+
if parsed_args.network_segment is not None:
205+
attrs['segment_id'] = client.find_segment(
206+
parsed_args.network_segment, ignore_missing=False).id
207207
if 'gateway' in parsed_args and parsed_args.gateway is not None:
208208
gateway = parsed_args.gateway.lower()
209209

@@ -558,6 +558,14 @@ def get_parser(self, prog_name):
558558
"'none': This subnet will not use a gateway, "
559559
"e.g.: --gateway 192.168.9.1, --gateway none.")
560560
)
561+
parser.add_argument(
562+
'--network-segment',
563+
metavar='<network-segment>',
564+
help=_("Network segment to associate with this subnet (name or "
565+
"ID). It is only allowed to set the segment if the current "
566+
"value is `None`, the network must also have only one "
567+
"segment and only one subnet can exist on the network.")
568+
)
561569
parser.add_argument(
562570
'--description',
563571
metavar='<description>',

openstackclient/tests/unit/network/v2/test_subnet.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,29 @@ def test_set_with_tags(self):
10741074
def test_set_with_no_tag(self):
10751075
self._test_set_tags(with_tags=False)
10761076

1077+
def test_set_segment(self):
1078+
_net = network_fakes.FakeNetwork.create_one_network()
1079+
_segment = network_fakes.FakeNetworkSegment.create_one_network_segment(
1080+
attrs={'network_id': _net.id})
1081+
_subnet = network_fakes.FakeSubnet.create_one_subnet(
1082+
{'host_routes': [{'destination': '10.20.20.0/24',
1083+
'nexthop': '10.20.20.1'}],
1084+
'allocation_pools': [{'start': '8.8.8.200',
1085+
'end': '8.8.8.250'}],
1086+
'dns_nameservers': ["10.0.0.1"],
1087+
'network_id': _net.id,
1088+
'segment_id': None})
1089+
self.network.find_subnet = mock.Mock(return_value=_subnet)
1090+
self.network.find_segment = mock.Mock(return_value=_segment)
1091+
arglist = ['--network-segment', _segment.id, _subnet.name]
1092+
verifylist = [('network_segment', _segment.id)]
1093+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1094+
result = self.cmd.take_action(parsed_args)
1095+
attrs = {'segment_id': _segment.id}
1096+
self.network.update_subnet.assert_called_once_with(_subnet, **attrs)
1097+
self.network.update_subnet.assert_called_with(_subnet, **attrs)
1098+
self.assertIsNone(result)
1099+
10771100

10781101
class TestShowSubnet(TestSubnet):
10791102
# The subnets to be shown
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- Add ``--network-segment`` option to ``subnet set`` command. This
4+
enables the possiblity to set the ``segment_id`` of a subnet on
5+
update.

0 commit comments

Comments
 (0)