Skip to content

Commit 8c850ca

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add "--network-segment" option to "subnet create""
2 parents f5ae23a + 6a6b192 commit 8c850ca

6 files changed

Lines changed: 157 additions & 25 deletions

File tree

doc/source/command-beta.rst

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,29 @@ To address these challenges, an OpenStackClient command may
1212
be labeled as a beta command according to the guidelines
1313
below. Such commands may introduce backwards incompatible
1414
changes and may use REST API enhancements not yet released.
15+
This also applies to command options associated with the beta
16+
command object.
1517

16-
See the examples below on how to label a command as a beta
17-
by updating the command documentation, help and implementation.
18+
See the examples below on how to label an entire command or
19+
a specific option as a beta by updating the documentation
20+
and implementation.
1821

19-
The initial release note must label the new command as a beta.
20-
No further release notes are required until the command
21-
is no longer a beta. At which time, the command beta label
22-
or the command itself must be removed and a new release note
22+
The initial release note must label the new command or option
23+
as a beta. No further release notes are required until the command
24+
or option is no longer a beta. At which time, the beta label or
25+
the command or option itself must be removed and a new release note
2326
must be provided.
2427

28+
Beta Command Example
29+
--------------------
30+
2531
Documentation
26-
-------------
32+
~~~~~~~~~~~~~
2733

2834
The command documentation must label the command as a beta.
2935

3036
example list
31-
~~~~~~~~~~~~
37+
++++++++++++
3238

3339
List examples
3440

@@ -42,7 +48,7 @@ List examples
4248
os example list
4349
4450
Help
45-
----
51+
~~~~
4652

4753
The command help must label the command as a beta.
4854

@@ -57,7 +63,7 @@ The command help must label the command as a beta.
5763
"""
5864
5965
Implementation
60-
--------------
66+
~~~~~~~~~~~~~~
6167

6268
The command must raise a ``CommandError`` exception if beta commands
6369
are not enabled via ``--os-beta-command`` global option.
@@ -66,3 +72,35 @@ are not enabled via ``--os-beta-command`` global option.
6672
6773
def take_action(self, parsed_args):
6874
self.validate_os_beta_command_enabled()
75+
76+
Beta Option Example
77+
-------------------
78+
79+
Documentation
80+
~~~~~~~~~~~~~
81+
82+
The option documentation must label the option as a beta.
83+
84+
.. option:: --example <example>
85+
86+
Example
87+
88+
.. caution:: This is a beta command option and subject
89+
to change. Use global option ``--os-beta-command``
90+
to enable this command option.
91+
92+
Implementation
93+
~~~~~~~~~~~~~~
94+
95+
The option must not be added if beta commands are not
96+
enabled via ``--os-beta-command`` global option.
97+
98+
.. code-block:: python
99+
100+
def get_parser(self, prog_name):
101+
if self.app.options.os_beta_command:
102+
parser.add_argument(
103+
'--example',
104+
metavar='<example>',
105+
help=_("Example")
106+
)

doc/source/command-objects/subnet.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Create new subnet
2828
[--ip-version {4,6}]
2929
[--ipv6-ra-mode {dhcpv6-stateful,dhcpv6-stateless,slaac}]
3030
[--ipv6-address-mode {dhcpv6-stateful,dhcpv6-stateless,slaac}]
31+
[--network-segment <network-segment>]
3132
--network <network>
3233
<name>
3334
@@ -107,6 +108,14 @@ Create new subnet
107108
108109
IPv6 address mode, valid modes: [dhcpv6-stateful, dhcpv6-stateless, slaac]
109110
111+
.. option:: --network-segment <network-segment>
112+
113+
Network segment to associate with this subnet (ID only)
114+
115+
.. caution:: This is a beta command option and subject
116+
to change. Use global option ``--os-beta-command``
117+
to enable this command option.
118+
110119
.. option:: --network <network>
111120
112121
Network this subnet belongs to (name or ID)

openstackclient/network/v2/subnet.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ def _get_attrs(client_manager, parsed_args, is_create=True):
142142
attrs['ipv6_ra_mode'] = parsed_args.ipv6_ra_mode
143143
if parsed_args.ipv6_address_mode is not None:
144144
attrs['ipv6_address_mode'] = parsed_args.ipv6_address_mode
145+
if 'network_segment' in parsed_args:
146+
attrs['segment_id'] = client.find_segment(
147+
parsed_args.network_segment, ignore_missing=False).id
145148

146149
if 'gateway' in parsed_args and parsed_args.gateway is not None:
147150
gateway = parsed_args.gateway.lower()
@@ -255,6 +258,13 @@ def get_parser(self, prog_name):
255258
help=_("IPv6 address mode, "
256259
"valid modes: [dhcpv6-stateful, dhcpv6-stateless, slaac]")
257260
)
261+
if self.app.options.os_beta_command:
262+
parser.add_argument(
263+
'--network-segment',
264+
metavar='<network-segment>',
265+
help=_("Network segment to associate with this subnet "
266+
"(ID only)")
267+
)
258268
parser.add_argument(
259269
'--network',
260270
required=True,

openstackclient/tests/network/v2/fakes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def create_one_network_segment(attrs=None):
352352

353353
# Set default attributes.
354354
network_segment_attrs = {
355-
'id': 'segment-id-' + uuid.uuid4().hex,
355+
'id': 'network-segment-id-' + uuid.uuid4().hex,
356356
'network_id': 'network-id-' + uuid.uuid4().hex,
357357
'network_type': 'vlan',
358358
'physical_network': 'physical-network-name-' + uuid.uuid4().hex,
@@ -738,9 +738,10 @@ def create_one_subnet(attrs=None):
738738
'host_routes': [],
739739
'ip_version': 4,
740740
'gateway_ip': '10.10.10.1',
741-
'ipv6_address_mode': 'None',
742-
'ipv6_ra_mode': 'None',
743-
'subnetpool_id': 'None',
741+
'ipv6_address_mode': None,
742+
'ipv6_ra_mode': None,
743+
'segment_id': None,
744+
'subnetpool_id': None,
744745
}
745746

746747
# Overwrite default attributes.

openstackclient/tests/network/v2/test_subnet.py

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ class TestCreateSubnet(TestSubnet):
9191
}
9292
)
9393

94+
# The network segment to be returned from find_segment
95+
_network_segment = \
96+
network_fakes.FakeNetworkSegment.create_one_network_segment(
97+
attrs={
98+
'network_id': _subnet.network_id,
99+
}
100+
)
101+
94102
columns = (
95103
'allocation_pools',
96104
'cidr',
@@ -105,6 +113,7 @@ class TestCreateSubnet(TestSubnet):
105113
'name',
106114
'network_id',
107115
'project_id',
116+
'segment_id',
108117
'subnetpool_id',
109118
)
110119

@@ -122,6 +131,7 @@ class TestCreateSubnet(TestSubnet):
122131
_subnet.name,
123132
_subnet.network_id,
124133
_subnet.project_id,
134+
_subnet.segment_id,
125135
_subnet.subnetpool_id,
126136
)
127137

@@ -139,6 +149,7 @@ class TestCreateSubnet(TestSubnet):
139149
_subnet_from_pool.name,
140150
_subnet_from_pool.network_id,
141151
_subnet_from_pool.project_id,
152+
_subnet_from_pool.segment_id,
142153
_subnet_from_pool.subnetpool_id,
143154
)
144155

@@ -156,6 +167,7 @@ class TestCreateSubnet(TestSubnet):
156167
_subnet_ipv6.name,
157168
_subnet_ipv6.network_id,
158169
_subnet_ipv6.project_id,
170+
_subnet_ipv6.segment_id,
159171
_subnet_ipv6.subnetpool_id,
160172
)
161173

@@ -189,6 +201,15 @@ def setUp(self):
189201
loaded=True,
190202
)
191203

204+
# Mock SDK calls for all tests.
205+
self.network.find_network = mock.Mock(return_value=self._network)
206+
self.network.find_segment = mock.Mock(
207+
return_value=self._network_segment
208+
)
209+
self.network.find_subnet_pool = mock.Mock(
210+
return_value=self._subnet_pool
211+
)
212+
192213
def test_create_no_options(self):
193214
arglist = []
194215
verifylist = []
@@ -199,11 +220,9 @@ def test_create_no_options(self):
199220
self.check_parser, self.cmd, arglist, verifylist)
200221

201222
def test_create_default_options(self):
202-
# Mock create_subnet and find_network sdk calls to return the
203-
# values we want for this test
223+
# Mock SDK calls for this test.
204224
self.network.create_subnet = mock.Mock(return_value=self._subnet)
205225
self._network.id = self._subnet.network_id
206-
self.network.find_network = mock.Mock(return_value=self._network)
207226

208227
arglist = [
209228
"--subnet-range", self._subnet.cidr,
@@ -233,14 +252,10 @@ def test_create_default_options(self):
233252
self.assertEqual(self.data, data)
234253

235254
def test_create_from_subnet_pool_options(self):
236-
# Mock create_subnet, find_subnet_pool, and find_network sdk calls
237-
# to return the values we want for this test
255+
# Mock SDK calls for this test.
238256
self.network.create_subnet = \
239257
mock.Mock(return_value=self._subnet_from_pool)
240258
self._network.id = self._subnet_from_pool.network_id
241-
self.network.find_network = mock.Mock(return_value=self._network)
242-
self.network.find_subnet_pool = \
243-
mock.Mock(return_value=self._subnet_pool)
244259

245260
arglist = [
246261
self._subnet_from_pool.name,
@@ -293,11 +308,9 @@ def test_create_from_subnet_pool_options(self):
293308
self.assertEqual(self.data_subnet_pool, data)
294309

295310
def test_create_options_subnet_range_ipv6(self):
296-
# Mock create_subnet and find_network sdk calls to return the
297-
# values we want for this test
311+
# Mock SDK calls for this test.
298312
self.network.create_subnet = mock.Mock(return_value=self._subnet_ipv6)
299313
self._network.id = self._subnet_ipv6.network_id
300-
self.network.find_network = mock.Mock(return_value=self._network)
301314

302315
arglist = [
303316
self._subnet_ipv6.name,
@@ -360,6 +373,59 @@ def test_create_options_subnet_range_ipv6(self):
360373
self.assertEqual(self.columns, columns)
361374
self.assertEqual(self.data_ipv6, data)
362375

376+
def test_create_no_beta_command_options(self):
377+
arglist = [
378+
"--subnet-range", self._subnet.cidr,
379+
"--network-segment", self._network_segment.id,
380+
"--network", self._subnet.network_id,
381+
self._subnet.name,
382+
]
383+
verifylist = [
384+
('name', self._subnet.name),
385+
('subnet_range', self._subnet.cidr),
386+
('network-segment', self._network_segment.id),
387+
('network', self._subnet.network_id),
388+
]
389+
self.app.options.os_beta_command = False
390+
self.assertRaises(tests_utils.ParserException,
391+
self.check_parser, self.cmd, arglist, verifylist)
392+
393+
def test_create_with_network_segment(self):
394+
# Mock SDK calls for this test.
395+
self.network.create_subnet = mock.Mock(return_value=self._subnet)
396+
self._network.id = self._subnet.network_id
397+
398+
arglist = [
399+
"--subnet-range", self._subnet.cidr,
400+
"--network-segment", self._network_segment.id,
401+
"--network", self._subnet.network_id,
402+
self._subnet.name,
403+
]
404+
verifylist = [
405+
('name', self._subnet.name),
406+
('subnet_range', self._subnet.cidr),
407+
('network_segment', self._network_segment.id),
408+
('network', self._subnet.network_id),
409+
('ip_version', self._subnet.ip_version),
410+
('gateway', 'auto'),
411+
412+
]
413+
414+
self.app.options.os_beta_command = True
415+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
416+
columns, data = self.cmd.take_action(parsed_args)
417+
418+
self.network.create_subnet.assert_called_once_with(**{
419+
'cidr': self._subnet.cidr,
420+
'enable_dhcp': self._subnet.enable_dhcp,
421+
'ip_version': self._subnet.ip_version,
422+
'name': self._subnet.name,
423+
'network_id': self._subnet.network_id,
424+
'segment_id': self._network_segment.id,
425+
})
426+
self.assertEqual(self.columns, columns)
427+
self.assertEqual(self.data, data)
428+
363429

364430
class TestDeleteSubnet(TestSubnet):
365431

@@ -646,6 +712,7 @@ class TestShowSubnet(TestSubnet):
646712
'name',
647713
'network_id',
648714
'project_id',
715+
'segment_id',
649716
'subnetpool_id',
650717
)
651718

@@ -663,6 +730,7 @@ class TestShowSubnet(TestSubnet):
663730
_subnet.name,
664731
_subnet.network_id,
665732
_subnet.tenant_id,
733+
_subnet.segment_id,
666734
_subnet.subnetpool_id,
667735
)
668736

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- Add ``--network-segment`` option to the ``subnet create`` command.
4+
This is a beta command option and subject to change. Use global option
5+
``--os-beta-command`` to enable this option.
6+
[Blueprint `routed-networks <https://blueprints.launchpad.net/neutron/+spec/routed-networks>`_]

0 commit comments

Comments
 (0)