Skip to content

Commit 7c9bb37

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add "os subnet show" command using SDK"
2 parents c57fc41 + 112d7b0 commit 7c9bb37

6 files changed

Lines changed: 133 additions & 2 deletions

File tree

doc/source/command-objects/subnet.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ List subnets
1818
.. option:: --long
1919

2020
List additional fields in output
21+
22+
subnet show
23+
-----------
24+
25+
Show subnet details
26+
27+
.. program:: subnet show
28+
.. code:: bash
29+
30+
os subnet show
31+
<subnet>
32+
33+
.. _subnet_show-subnet:
34+
.. describe:: <subnet>
35+
36+
Subnet to show (name or ID)

openstackclient/network/v2/subnet.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ def _format_allocation_pools(data):
3030
}
3131

3232

33+
def _get_columns(item):
34+
columns = item.keys()
35+
if 'tenant_id' in columns:
36+
columns.remove('tenant_id')
37+
columns.append('project_id')
38+
return tuple(sorted(columns))
39+
40+
3341
class ListSubnet(command.Lister):
3442
"""List subnets"""
3543

@@ -61,3 +69,23 @@ def take_action(self, parsed_args):
6169
s, columns,
6270
formatters=_formatters,
6371
) for s in data))
72+
73+
74+
class ShowSubnet(command.ShowOne):
75+
"""Show subnet details"""
76+
77+
def get_parser(self, prog_name):
78+
parser = super(ShowSubnet, self).get_parser(prog_name)
79+
parser.add_argument(
80+
'subnet',
81+
metavar="<subnet>",
82+
help="Subnet to show (name or ID)"
83+
)
84+
return parser
85+
86+
def take_action(self, parsed_args):
87+
obj = self.app.client_manager.network.find_subnet(parsed_args.subnet,
88+
ignore_missing=False)
89+
columns = _get_columns(obj)
90+
data = utils.get_item_properties(obj, columns, formatters=_formatters)
91+
return (columns, data)

openstackclient/tests/network/v2/fakes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,18 +550,22 @@ def create_one_subnet(attrs={}, methods={}):
550550
A FakeResource object faking the subnet
551551
"""
552552
# Set default attributes.
553+
project_id = 'project-id-' + uuid.uuid4().hex
553554
subnet_attrs = {
554555
'id': 'subnet-id-' + uuid.uuid4().hex,
555556
'name': 'subnet-name-' + uuid.uuid4().hex,
556557
'network_id': 'network-id-' + uuid.uuid4().hex,
557558
'cidr': '10.10.10.0/24',
558-
'tenant_id': 'project-id-' + uuid.uuid4().hex,
559+
'tenant_id': project_id,
559560
'enable_dhcp': True,
560561
'dns_nameservers': [],
561562
'allocation_pools': [],
562563
'host_routes': [],
563564
'ip_version': '4',
564565
'gateway_ip': '10.10.10.1',
566+
'ipv6_address_mode': 'None',
567+
'ipv6_ra_mode': 'None',
568+
'subnetpool_id': 'None',
565569
}
566570

567571
# Overwrite default attributes.
@@ -571,7 +575,8 @@ def create_one_subnet(attrs={}, methods={}):
571575
subnet_methods = {
572576
'keys': ['id', 'name', 'network_id', 'cidr', 'enable_dhcp',
573577
'allocation_pools', 'dns_nameservers', 'gateway_ip',
574-
'host_routes', 'ip_version', 'tenant_id']
578+
'host_routes', 'ip_version', 'tenant_id',
579+
'ipv6_address_mode', 'ipv6_ra_mode', 'subnetpool_id']
575580
}
576581

577582
# Overwrite default methods.
@@ -580,6 +585,8 @@ def create_one_subnet(attrs={}, methods={}):
580585
subnet = fakes.FakeResource(info=copy.deepcopy(subnet_attrs),
581586
methods=copy.deepcopy(subnet_methods),
582587
loaded=True)
588+
# Set attributes with special mappings in OpenStack SDK.
589+
subnet.project_id = subnet_attrs['tenant_id']
583590

584591
return subnet
585592

openstackclient/tests/network/v2/test_subnet.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from openstackclient.common import utils
1717
from openstackclient.network.v2 import subnet as subnet_v2
1818
from openstackclient.tests.network.v2 import fakes as network_fakes
19+
from openstackclient.tests import utils as tests_utils
1920

2021

2122
class TestSubnet(network_fakes.TestNetworkV2):
@@ -106,3 +107,76 @@ def test_subnet_list_long(self):
106107
self.network.subnets.assert_called_with()
107108
self.assertEqual(self.columns_long, columns)
108109
self.assertEqual(self.data_long, list(data))
110+
111+
112+
class TestShowSubnet(TestSubnet):
113+
# The subnets to be shown
114+
_subnet = network_fakes.FakeSubnet.create_one_subnet()
115+
116+
columns = (
117+
'allocation_pools',
118+
'cidr',
119+
'dns_nameservers',
120+
'enable_dhcp',
121+
'gateway_ip',
122+
'host_routes',
123+
'id',
124+
'ip_version',
125+
'ipv6_address_mode',
126+
'ipv6_ra_mode',
127+
'name',
128+
'network_id',
129+
'project_id',
130+
'subnetpool_id',
131+
)
132+
133+
data = (
134+
subnet_v2._format_allocation_pools(_subnet.allocation_pools),
135+
_subnet.cidr,
136+
utils.format_list(_subnet.dns_nameservers),
137+
_subnet.enable_dhcp,
138+
_subnet.gateway_ip,
139+
utils.format_list(_subnet.host_routes),
140+
_subnet.id,
141+
_subnet.ip_version,
142+
_subnet.ipv6_address_mode,
143+
_subnet.ipv6_ra_mode,
144+
_subnet.name,
145+
_subnet.network_id,
146+
_subnet.tenant_id,
147+
_subnet.subnetpool_id,
148+
)
149+
150+
def setUp(self):
151+
super(TestShowSubnet, self).setUp()
152+
153+
# Get the command object to test
154+
self.cmd = subnet_v2.ShowSubnet(self.app, self.namespace)
155+
156+
self.network.find_subnet = mock.Mock(return_value=self._subnet)
157+
158+
def test_show_no_options(self):
159+
arglist = []
160+
verifylist = []
161+
162+
# Testing that a call without the required argument will fail and
163+
# throw a "ParserExecption"
164+
self.assertRaises(tests_utils.ParserException,
165+
self.check_parser, self.cmd, arglist, verifylist)
166+
167+
def test_show_all_options(self):
168+
arglist = [
169+
self._subnet.name,
170+
]
171+
verifylist = [
172+
('subnet', self._subnet.name),
173+
]
174+
175+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
176+
columns, data = self.cmd.take_action(parsed_args)
177+
178+
self.network.find_subnet.assert_called_with(self._subnet.name,
179+
ignore_missing=False)
180+
181+
self.assertEqual(self.columns, columns)
182+
self.assertEqual(list(self.data), list(data))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``subnet show`` command.
5+
[Bug `1542359 <https://bugs.launchpad.net/bugs/1542359>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ openstack.network.v2 =
341341
security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup
342342
security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule
343343
subnet_list = openstackclient.network.v2.subnet:ListSubnet
344+
subnet_show = openstackclient.network.v2.subnet:ShowSubnet
344345
subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool
345346
subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool
346347
subnet_pool_show = openstackclient.network.v2.subnet_pool:ShowSubnetPool

0 commit comments

Comments
 (0)