Skip to content

Commit 7f12b74

Browse files
author
Reedip
committed
Overwrite/Clear support for subnets
This patch adds the overwrite/clear functionality for allocation-pool and host-routes in subnets. Change-Id: Idfa41173d0c054c5bfb4eda8c5f614928012555a implements: blueprint allow-overwrite-set-options
1 parent 43f6b95 commit 7f12b74

4 files changed

Lines changed: 79 additions & 5 deletions

File tree

doc/source/command-objects/subnet.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,12 @@ Set subnet properties
232232
233233
os subnet set
234234
[--allocation-pool start=<ip-address>,end=<ip-address>]
235+
[--no-allocation-pool]
235236
[--dhcp | --no-dhcp]
236237
[--dns-nameserver <dns-nameserver>]
237238
[--gateway <gateway-ip>]
238239
[--host-route destination=<subnet>,gateway=<ip-address>]
240+
[--no-host-route]
239241
[--service-type <service-type>]
240242
[--name <new-name>]
241243
[--description <description>]
@@ -247,6 +249,12 @@ Set subnet properties
247249
``start=192.168.199.2,end=192.168.199.254``
248250
(repeat option to add multiple IP addresses)
249251
252+
.. option:: --no-allocation-pool
253+
254+
Clear associated allocation pools from this subnet.
255+
Specify both --allocation-pool and --no-allocation-pool
256+
to overwrite the current allocation pool information.
257+
250258
.. option:: --dhcp
251259
252260
Enable DHCP
@@ -272,7 +280,12 @@ Set subnet properties
272280
``destination=10.10.0.0/16,gateway=192.168.71.254``
273281
destination: destination subnet (in CIDR notation)
274282
gateway: nexthop IP address
275-
(repeat option to add multiple routes)
283+
284+
.. option:: --no-host-route
285+
286+
Clear associated host routes from this subnet.
287+
Specify both --host-route and --no-host-route
288+
to overwrite the current host route information.
276289
277290
.. option:: --service-type <service-type>
278291

openstackclient/network/v2/subnet.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _format_host_routes(data):
5757
}
5858

5959

60-
def _get_common_parse_arguments(parser):
60+
def _get_common_parse_arguments(parser, is_create=True):
6161
parser.add_argument(
6262
'--allocation-pool',
6363
metavar='start=<ip-address>,end=<ip-address>',
@@ -68,6 +68,14 @@ def _get_common_parse_arguments(parser):
6868
"e.g.: start=192.168.199.2,end=192.168.199.254 "
6969
"(repeat option to add multiple IP addresses)")
7070
)
71+
if not is_create:
72+
parser.add_argument(
73+
'--no-allocation-pool',
74+
action='store_true',
75+
help=_("Clear associated allocation-pools from the subnet. "
76+
"Specify both --allocation-pool and --no-allocation-pool "
77+
"to overwrite the current allocation pool information.")
78+
)
7179
parser.add_argument(
7280
'--dns-nameserver',
7381
metavar='<dns-nameserver>',
@@ -88,6 +96,14 @@ def _get_common_parse_arguments(parser):
8896
"gateway: nexthop IP address "
8997
"(repeat option to add multiple routes)")
9098
)
99+
if not is_create:
100+
parser.add_argument(
101+
'--no-host-route',
102+
action='store_true',
103+
help=_("Clear associated host-routes from the subnet. "
104+
"Specify both --host-route and --no-host-route "
105+
"to overwrite the current host route information.")
106+
)
91107
parser.add_argument(
92108
'--service-type',
93109
metavar='<service-type>',
@@ -508,7 +524,7 @@ def get_parser(self, prog_name):
508524
metavar='<description>',
509525
help=_("Set subnet description")
510526
)
511-
_get_common_parse_arguments(parser)
527+
_get_common_parse_arguments(parser, is_create=False)
512528
return parser
513529

514530
def take_action(self, parsed_args):
@@ -519,9 +535,15 @@ def take_action(self, parsed_args):
519535
if 'dns_nameservers' in attrs:
520536
attrs['dns_nameservers'] += obj.dns_nameservers
521537
if 'host_routes' in attrs:
522-
attrs['host_routes'] += obj.host_routes
538+
if not parsed_args.no_host_route:
539+
attrs['host_routes'] += obj.host_routes
540+
elif parsed_args.no_host_route:
541+
attrs['host_routes'] = ''
523542
if 'allocation_pools' in attrs:
524-
attrs['allocation_pools'] += obj.allocation_pools
543+
if not parsed_args.no_allocation_pool:
544+
attrs['allocation_pools'] += obj.allocation_pools
545+
elif parsed_args.no_allocation_pool:
546+
attrs['allocation_pools'] = ''
525547
if 'service_types' in attrs:
526548
attrs['service_types'] += obj.service_types
527549
client.update_subnet(obj, **attrs)

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,39 @@ def test_set_non_append_options(self):
938938
self.network.update_subnet.assert_called_with(self._subnet, **attrs)
939939
self.assertIsNone(result)
940940

941+
def test_overwrite_options(self):
942+
_testsubnet = network_fakes.FakeSubnet.create_one_subnet(
943+
{'host_routes': [{'destination': '10.20.20.0/24',
944+
'nexthop': '10.20.20.1'}],
945+
'allocation_pools': [{'start': '8.8.8.200',
946+
'end': '8.8.8.250'}], })
947+
self.network.find_subnet = mock.Mock(return_value=_testsubnet)
948+
arglist = [
949+
'--host-route', 'destination=10.30.30.30/24,gateway=10.30.30.1',
950+
'--no-host-route',
951+
'--allocation-pool', 'start=8.8.8.100,end=8.8.8.150',
952+
'--no-allocation-pool',
953+
_testsubnet.name,
954+
]
955+
verifylist = [
956+
('host_routes', [{
957+
"destination": "10.30.30.30/24", "gateway": "10.30.30.1"}]),
958+
('allocation_pools', [{
959+
'start': '8.8.8.100', 'end': '8.8.8.150'}]),
960+
('no_host_route', True),
961+
('no_allocation_pool', True),
962+
]
963+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
964+
result = self.cmd.take_action(parsed_args)
965+
attrs = {
966+
'host_routes': [{
967+
"destination": "10.30.30.30/24", "nexthop": "10.30.30.1"}],
968+
'allocation_pools': [{'start': '8.8.8.100', 'end': '8.8.8.150'}],
969+
}
970+
self.network.update_subnet.assert_called_once_with(
971+
_testsubnet, **attrs)
972+
self.assertIsNone(result)
973+
941974

942975
class TestShowSubnet(TestSubnet):
943976
# The subnets to be shown
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
``subnet set`` command now allows the user to clear and overwrite
5+
allocation-pool or host-route of a subnet.
6+
[ Blueprint `allow-overwrite-set-options <https://blueprints.launchpad.net/python-openstackclient/+spec/allow-overwrite-set-options>` _]

0 commit comments

Comments
 (0)