summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Behle <behle@us.ibm.com>2016-02-04 17:19:37 -0600
committerBrad Behle <behle@us.ibm.com>2016-02-22 13:04:25 -0600
commit112d7b0e0966599aa940de4c0598cea759780785 (patch)
treefab9ae2e815c303d41451fb3f258925bb309e1b0
parentba08683d90277e3cc6f943a2a994ccf08589cb1a (diff)
downloadpython-openstackclient-112d7b0e0966599aa940de4c0598cea759780785.tar.gz
Add "os subnet show" command using SDK
Implement the openstack client subnet show command using SDK calls. This shows the details of a specific subnet. Co-Authored-By: Terry Howe <terrylhowe@gmail.com> Partially implements: blueprint neutron-client Closes-Bug: #1542359 Change-Id: Iaf18b9e44af35ca0cd61033b468e0c60cd3b05d6
-rw-r--r--doc/source/command-objects/subnet.rst16
-rw-r--r--openstackclient/network/v2/subnet.py28
-rw-r--r--openstackclient/tests/network/v2/fakes.py11
-rw-r--r--openstackclient/tests/network/v2/test_subnet.py74
-rw-r--r--releasenotes/notes/bug-1542359-181d28db21a2358a.yaml5
-rw-r--r--setup.cfg1
6 files changed, 133 insertions, 2 deletions
diff --git a/doc/source/command-objects/subnet.rst b/doc/source/command-objects/subnet.rst
index 70a0eedf..12b05665 100644
--- a/doc/source/command-objects/subnet.rst
+++ b/doc/source/command-objects/subnet.rst
@@ -18,3 +18,19 @@ List subnets
.. option:: --long
List additional fields in output
+
+subnet show
+-----------
+
+Show subnet details
+
+.. program:: subnet show
+.. code:: bash
+
+ os subnet show
+ <subnet>
+
+.. _subnet_show-subnet:
+.. describe:: <subnet>
+
+ Subnet to show (name or ID)
diff --git a/openstackclient/network/v2/subnet.py b/openstackclient/network/v2/subnet.py
index b948c656..7ed02a3a 100644
--- a/openstackclient/network/v2/subnet.py
+++ b/openstackclient/network/v2/subnet.py
@@ -30,6 +30,14 @@ _formatters = {
}
+def _get_columns(item):
+ columns = item.keys()
+ if 'tenant_id' in columns:
+ columns.remove('tenant_id')
+ columns.append('project_id')
+ return tuple(sorted(columns))
+
+
class ListSubnet(command.Lister):
"""List subnets"""
@@ -61,3 +69,23 @@ class ListSubnet(command.Lister):
s, columns,
formatters=_formatters,
) for s in data))
+
+
+class ShowSubnet(command.ShowOne):
+ """Show subnet details"""
+
+ def get_parser(self, prog_name):
+ parser = super(ShowSubnet, self).get_parser(prog_name)
+ parser.add_argument(
+ 'subnet',
+ metavar="<subnet>",
+ help="Subnet to show (name or ID)"
+ )
+ return parser
+
+ def take_action(self, parsed_args):
+ obj = self.app.client_manager.network.find_subnet(parsed_args.subnet,
+ ignore_missing=False)
+ columns = _get_columns(obj)
+ data = utils.get_item_properties(obj, columns, formatters=_formatters)
+ return (columns, data)
diff --git a/openstackclient/tests/network/v2/fakes.py b/openstackclient/tests/network/v2/fakes.py
index 680e9cbf..c24410e1 100644
--- a/openstackclient/tests/network/v2/fakes.py
+++ b/openstackclient/tests/network/v2/fakes.py
@@ -550,18 +550,22 @@ class FakeSubnet(object):
A FakeResource object faking the subnet
"""
# Set default attributes.
+ project_id = 'project-id-' + uuid.uuid4().hex
subnet_attrs = {
'id': 'subnet-id-' + uuid.uuid4().hex,
'name': 'subnet-name-' + uuid.uuid4().hex,
'network_id': 'network-id-' + uuid.uuid4().hex,
'cidr': '10.10.10.0/24',
- 'tenant_id': 'project-id-' + uuid.uuid4().hex,
+ 'tenant_id': project_id,
'enable_dhcp': True,
'dns_nameservers': [],
'allocation_pools': [],
'host_routes': [],
'ip_version': '4',
'gateway_ip': '10.10.10.1',
+ 'ipv6_address_mode': 'None',
+ 'ipv6_ra_mode': 'None',
+ 'subnetpool_id': 'None',
}
# Overwrite default attributes.
@@ -571,7 +575,8 @@ class FakeSubnet(object):
subnet_methods = {
'keys': ['id', 'name', 'network_id', 'cidr', 'enable_dhcp',
'allocation_pools', 'dns_nameservers', 'gateway_ip',
- 'host_routes', 'ip_version', 'tenant_id']
+ 'host_routes', 'ip_version', 'tenant_id',
+ 'ipv6_address_mode', 'ipv6_ra_mode', 'subnetpool_id']
}
# Overwrite default methods.
@@ -580,6 +585,8 @@ class FakeSubnet(object):
subnet = fakes.FakeResource(info=copy.deepcopy(subnet_attrs),
methods=copy.deepcopy(subnet_methods),
loaded=True)
+ # Set attributes with special mappings in OpenStack SDK.
+ subnet.project_id = subnet_attrs['tenant_id']
return subnet
diff --git a/openstackclient/tests/network/v2/test_subnet.py b/openstackclient/tests/network/v2/test_subnet.py
index 5fca5edd..c02dc407 100644
--- a/openstackclient/tests/network/v2/test_subnet.py
+++ b/openstackclient/tests/network/v2/test_subnet.py
@@ -16,6 +16,7 @@ import mock
from openstackclient.common import utils
from openstackclient.network.v2 import subnet as subnet_v2
from openstackclient.tests.network.v2 import fakes as network_fakes
+from openstackclient.tests import utils as tests_utils
class TestSubnet(network_fakes.TestNetworkV2):
@@ -106,3 +107,76 @@ class TestListSubnet(TestSubnet):
self.network.subnets.assert_called_with()
self.assertEqual(self.columns_long, columns)
self.assertEqual(self.data_long, list(data))
+
+
+class TestShowSubnet(TestSubnet):
+ # The subnets to be shown
+ _subnet = network_fakes.FakeSubnet.create_one_subnet()
+
+ columns = (
+ 'allocation_pools',
+ 'cidr',
+ 'dns_nameservers',
+ 'enable_dhcp',
+ 'gateway_ip',
+ 'host_routes',
+ 'id',
+ 'ip_version',
+ 'ipv6_address_mode',
+ 'ipv6_ra_mode',
+ 'name',
+ 'network_id',
+ 'project_id',
+ 'subnetpool_id',
+ )
+
+ data = (
+ subnet_v2._format_allocation_pools(_subnet.allocation_pools),
+ _subnet.cidr,
+ utils.format_list(_subnet.dns_nameservers),
+ _subnet.enable_dhcp,
+ _subnet.gateway_ip,
+ utils.format_list(_subnet.host_routes),
+ _subnet.id,
+ _subnet.ip_version,
+ _subnet.ipv6_address_mode,
+ _subnet.ipv6_ra_mode,
+ _subnet.name,
+ _subnet.network_id,
+ _subnet.tenant_id,
+ _subnet.subnetpool_id,
+ )
+
+ def setUp(self):
+ super(TestShowSubnet, self).setUp()
+
+ # Get the command object to test
+ self.cmd = subnet_v2.ShowSubnet(self.app, self.namespace)
+
+ self.network.find_subnet = mock.Mock(return_value=self._subnet)
+
+ def test_show_no_options(self):
+ arglist = []
+ verifylist = []
+
+ # Testing that a call without the required argument will fail and
+ # throw a "ParserExecption"
+ self.assertRaises(tests_utils.ParserException,
+ self.check_parser, self.cmd, arglist, verifylist)
+
+ def test_show_all_options(self):
+ arglist = [
+ self._subnet.name,
+ ]
+ verifylist = [
+ ('subnet', self._subnet.name),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+ columns, data = self.cmd.take_action(parsed_args)
+
+ self.network.find_subnet.assert_called_with(self._subnet.name,
+ ignore_missing=False)
+
+ self.assertEqual(self.columns, columns)
+ self.assertEqual(list(self.data), list(data))
diff --git a/releasenotes/notes/bug-1542359-181d28db21a2358a.yaml b/releasenotes/notes/bug-1542359-181d28db21a2358a.yaml
new file mode 100644
index 00000000..3aa7ad8e
--- /dev/null
+++ b/releasenotes/notes/bug-1542359-181d28db21a2358a.yaml
@@ -0,0 +1,5 @@
+---
+features:
+ - |
+ Add ``subnet show`` command.
+ [Bug `1542359 <https://bugs.launchpad.net/bugs/1542359>`_]
diff --git a/setup.cfg b/setup.cfg
index a6e27025..b535fd4c 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -341,6 +341,7 @@ openstack.network.v2 =
security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup
security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule
subnet_list = openstackclient.network.v2.subnet:ListSubnet
+ subnet_show = openstackclient.network.v2.subnet:ShowSubnet
subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool
subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool
subnet_pool_show = openstackclient.network.v2.subnet_pool:ShowSubnetPool