Support security group name for --src-group

Support security group name for the "--src-group" option on the
"os security group rule create" command.

Change-Id: Ic23d0671dad77566269c9a588644c8d774368733
Closes-Bug: #1540656
This commit is contained in:
Richard Theis 2016-03-17 12:58:44 -05:00
parent d906507962
commit 2109bce85a
4 changed files with 21 additions and 8 deletions

View File

@ -28,7 +28,7 @@ Create a new security group rule
.. option:: --src-group <group> .. option:: --src-group <group>
Source security group (ID only) Source security group (name or ID)
.. option:: --dst-port <port-range> .. option:: --dst-port <port-range>

View File

@ -69,7 +69,7 @@ class CreateSecurityGroupRule(common.NetworkAndComputeShowOne):
source_group.add_argument( source_group.add_argument(
"--src-group", "--src-group",
metavar="<group>", metavar="<group>",
help="Source security group (ID only)", help="Source security group (name or ID)",
) )
parser.add_argument( parser.add_argument(
"--dst-port", "--dst-port",
@ -103,7 +103,10 @@ class CreateSecurityGroupRule(common.NetworkAndComputeShowOne):
attrs['port_range_max'] = parsed_args.dst_port[1] attrs['port_range_max'] = parsed_args.dst_port[1]
attrs['protocol'] = parsed_args.proto attrs['protocol'] = parsed_args.proto
if parsed_args.src_group is not None: if parsed_args.src_group is not None:
attrs['remote_group_id'] = parsed_args.src_group attrs['remote_group_id'] = client.find_security_group(
parsed_args.src_group,
ignore_missing=False
).id
else: else:
attrs['remote_ip_prefix'] = parsed_args.src_ip attrs['remote_ip_prefix'] = parsed_args.src_ip
attrs['security_group_id'] = security_group_id attrs['security_group_id'] = security_group_id
@ -123,6 +126,11 @@ class CreateSecurityGroupRule(common.NetworkAndComputeShowOne):
from_port, to_port = -1, -1 from_port, to_port = -1, -1
else: else:
from_port, to_port = parsed_args.dst_port from_port, to_port = parsed_args.dst_port
if parsed_args.src_group is not None:
parsed_args.src_group = utils.find_resource(
client.security_groups,
parsed_args.src_group,
).id
obj = client.security_group_rules.create( obj = client.security_group_rules.create(
group.id, group.id,
parsed_args.proto, parsed_args.proto,

View File

@ -149,13 +149,13 @@ class TestCreateSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork):
}) })
arglist = [ arglist = [
'--dst-port', str(self._security_group_rule.port_range_min), '--dst-port', str(self._security_group_rule.port_range_min),
'--src-group', self._security_group.id, '--src-group', self._security_group.name,
self._security_group.id, self._security_group.id,
] ]
verifylist = [ verifylist = [
('dst_port', (self._security_group_rule.port_range_min, ('dst_port', (self._security_group_rule.port_range_min,
self._security_group_rule.port_range_max)), self._security_group_rule.port_range_max)),
('src_group', self._security_group.id), ('src_group', self._security_group.name),
('group', self._security_group.id), ('group', self._security_group.id),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -289,17 +289,17 @@ class TestCreateSecurityGroupRuleCompute(TestSecurityGroupRuleCompute):
expected_columns, expected_data = self._setup_security_group_rule({ expected_columns, expected_data = self._setup_security_group_rule({
'from_port': 22, 'from_port': 22,
'to_port': 22, 'to_port': 22,
'group': {'name': self._security_group.id}, 'group': {'name': self._security_group.name},
}) })
arglist = [ arglist = [
'--dst-port', str(self._security_group_rule.from_port), '--dst-port', str(self._security_group_rule.from_port),
'--src-group', self._security_group.id, '--src-group', self._security_group.name,
self._security_group.id, self._security_group.id,
] ]
verifylist = [ verifylist = [
('dst_port', (self._security_group_rule.from_port, ('dst_port', (self._security_group_rule.from_port,
self._security_group_rule.to_port)), self._security_group_rule.to_port)),
('src_group', self._security_group.id), ('src_group', self._security_group.name),
('group', self._security_group.id), ('group', self._security_group.id),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)

View File

@ -0,0 +1,5 @@
---
features:
- The ``security group rule create`` command now supports a security
group name for the ``--src-group`` option.
[Bug `1540656 <https://bugs.launchpad.net/bugs/1540656>`_]