Fix security group rule list for NEtwork v2

Fix the formatting of Port Range in the security group rule list command
for Network v2 to handle SDK changes.

Change-Id: Id954cbfaedbb74f60125ebda91f80db751759933
This commit is contained in:
Dean Troyer
2017-01-13 11:56:34 -06:00
parent 1880527e60
commit 5988ee61d8
2 changed files with 26 additions and 22 deletions

View File

@@ -51,17 +51,17 @@ def _format_network_port_range(rule):
# - Single port: '80:80' # - Single port: '80:80'
# - No port range: '' # - No port range: ''
port_range = '' port_range = ''
if _is_icmp_protocol(rule.protocol): if _is_icmp_protocol(rule['protocol']):
if rule.port_range_min: if rule['port_range_min']:
port_range += 'type=' + str(rule.port_range_min) port_range += 'type=' + str(rule['port_range_min'])
if rule.port_range_max: if rule['port_range_max']:
port_range += ':code=' + str(rule.port_range_max) port_range += ':code=' + str(rule['port_range_max'])
elif rule.port_range_min or rule.port_range_max: elif rule['port_range_min'] or rule['port_range_max']:
port_range_min = str(rule.port_range_min) port_range_min = str(rule['port_range_min'])
port_range_max = str(rule.port_range_max) port_range_max = str(rule['port_range_max'])
if rule.port_range_min is None: if rule['port_range_min'] is None:
port_range_min = port_range_max port_range_min = port_range_max
if rule.port_range_max is None: if rule['port_range_max'] is None:
port_range_max = port_range_min port_range_max = port_range_min
port_range = port_range_min + ':' + port_range_max port_range = port_range_min + ':' + port_range_max
return port_range return port_range
@@ -423,6 +423,16 @@ class DeleteSecurityGroupRule(common.NetworkAndComputeDelete):
class ListSecurityGroupRule(common.NetworkAndComputeLister): class ListSecurityGroupRule(common.NetworkAndComputeLister):
_description = _("List security group rules") _description = _("List security group rules")
def _format_network_security_group_rule(self, rule):
"""Transform the SDK SecurityGroupRule object to a dict
The SDK object gets in the way of reformatting columns...
Create port_range column from port_range_min and port_range_max
"""
rule = rule.to_dict()
rule['port_range'] = _format_network_port_range(rule)
return rule
def update_parser_common(self, parser): def update_parser_common(self, parser):
parser.add_argument( parser.add_argument(
'group', 'group',
@@ -508,7 +518,7 @@ class ListSecurityGroupRule(common.NetworkAndComputeLister):
'id', 'id',
'protocol', 'protocol',
'remote_ip_prefix', 'remote_ip_prefix',
'port_range_min', 'port_range',
) )
if parsed_args.long: if parsed_args.long:
columns = columns + ('direction', 'ethertype',) columns = columns + ('direction', 'ethertype',)
@@ -535,16 +545,13 @@ class ListSecurityGroupRule(common.NetworkAndComputeLister):
if parsed_args.protocol is not None: if parsed_args.protocol is not None:
query['protocol'] = parsed_args.protocol query['protocol'] = parsed_args.protocol
rules = list(client.security_group_rules(**query)) rules = [
self._format_network_security_group_rule(r)
# Reformat the rules to display a port range instead for r in client.security_group_rules(**query)
# of just the port range minimum. This maintains ]
# output compatibility with compute.
for rule in rules:
rule.port_range_min = _format_network_port_range(rule)
return (column_headers, return (column_headers,
(utils.get_item_properties( (utils.get_dict_properties(
s, columns, s, columns,
) for s in rules)) ) for s in rules))

View File

@@ -12,8 +12,6 @@
import uuid import uuid
import testtools
from openstackclient.tests.functional import base from openstackclient.tests.functional import base
@@ -54,7 +52,6 @@ class SecurityGroupRuleTests(base.TestCase):
cls.SECURITY_GROUP_NAME) cls.SECURITY_GROUP_NAME)
cls.assertOutput('', raw_output) cls.assertOutput('', raw_output)
@testtools.skip('broken SDK testing')
def test_security_group_rule_list(self): def test_security_group_rule_list(self):
opts = self.get_opts(self.ID_HEADER) opts = self.get_opts(self.ID_HEADER)
raw_output = self.openstack('security group rule list ' + raw_output = self.openstack('security group rule list ' +