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
This commit is contained in:
Brad Behle 2016-02-04 17:19:37 -06:00
parent ba08683d90
commit 112d7b0e09
6 changed files with 133 additions and 2 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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))

View File

@ -0,0 +1,5 @@
---
features:
- |
Add ``subnet show`` command.
[Bug `1542359 <https://bugs.launchpad.net/bugs/1542359>`_]

View File

@ -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