Skip to content

Commit

Permalink
Add --security-group option to port-update
Browse files Browse the repository at this point in the history
port-update --security_groups list=true ... is not user-friendly.

bug 1161078

This commit also changes the followings:
- Add --no-security-groups options to port-create for consistency.
- Make --security-group and --no-security-groups mutual-exclusive
  to prevent both options specified at the same time.

Change-Id: Ibfe165a0cbdfb0eb582784c80a3371de33108fdb
  • Loading branch information
amotoki committed Jul 8, 2013
1 parent 4e3be8d commit bc99eca
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
52 changes: 33 additions & 19 deletions neutronclient/neutron/v2_0/port.py
Expand Up @@ -73,7 +73,32 @@ class ShowPort(neutronV20.ShowCommand):
log = logging.getLogger(__name__ + '.ShowPort')


class CreatePort(neutronV20.CreateCommand):
class UpdatePortSecGroupMixin(object):
def add_arguments_secgroup(self, parser):
group_sg = parser.add_mutually_exclusive_group()
group_sg.add_argument(
'--security-group', metavar='SECURITY_GROUP',
default=[], action='append', dest='security_groups',
help='security group associated with the port '
'(This option can be repeated)')
group_sg.add_argument(
'--no-security-groups',
action='store_true',
help='associate no security groups with the port')

def _resolv_sgid(self, secgroup):
return neutronV20.find_resourceid_by_name_or_id(
self.get_client(), 'security_group', secgroup)

def args2body_secgroup(self, parsed_args, port):
if parsed_args.security_groups:
port['security_groups'] = [self._resolv_sgid(sg) for sg
in parsed_args.security_groups]
elif parsed_args.no_security_groups:
port['security_groups'] = None


class CreatePort(neutronV20.CreateCommand, UpdatePortSecGroupMixin):
"""Create a port for a given tenant."""

resource = 'port'
Expand Down Expand Up @@ -113,11 +138,9 @@ def add_known_arguments(self, parser):
'--fixed_ip',
action='append',
help=argparse.SUPPRESS)
parser.add_argument(
'--security-group', metavar='SECURITY_GROUP',
default=[], action='append', dest='security_groups',
help='security group associated with the port '
'(This option can be repeated)')

self.add_arguments_secgroup(parser)

parser.add_argument(
'network_id', metavar='NETWORK',
help='Network id or name this port belongs to')
Expand Down Expand Up @@ -148,12 +171,7 @@ def args2body(self, parsed_args):
if ips:
body['port'].update({'fixed_ips': ips})

_sgids = []
for sg in parsed_args.security_groups:
_sgids.append(neutronV20.find_resourceid_by_name_or_id(
self.get_client(), 'security_group', sg))
if _sgids:
body['port']['security_groups'] = _sgids
self.args2body_secgroup(parsed_args, body['port'])

return body

Expand All @@ -165,20 +183,16 @@ class DeletePort(neutronV20.DeleteCommand):
log = logging.getLogger(__name__ + '.DeletePort')


class UpdatePort(neutronV20.UpdateCommand):
class UpdatePort(neutronV20.UpdateCommand, UpdatePortSecGroupMixin):
"""Update port's information."""

resource = 'port'
log = logging.getLogger(__name__ + '.UpdatePort')

def add_known_arguments(self, parser):
parser.add_argument(
'--no-security-groups',
action='store_true',
help='remove security groups from port')
self.add_arguments_secgroup(parser)

def args2body(self, parsed_args):
body = {'port': {}}
if parsed_args.no_security_groups:
body['port'].update({'security_groups': None})
self.args2body_secgroup(parsed_args, body['port'])
return body
30 changes: 30 additions & 0 deletions tests/unit/test_cli20_port.py
Expand Up @@ -129,6 +129,18 @@ def test_create_port_secgroups(self):
self._test_create_resource(resource, cmd, name, myid, args,
position_names, position_values)

def test_create_port_secgroup_off(self):
resource = 'port'
cmd = port.CreatePort(test_cli20.MyApp(sys.stdout), None)
name = 'myname'
myid = 'myid'
netid = 'netid'
args = ['--no-security-group', netid]
position_names = ['network_id', 'security_groups']
position_values = [netid, None]
self._test_create_resource(resource, cmd, name, myid, args,
position_names, position_values)

def test_list_ports(self):
"""List ports: -D."""
resources = "ports"
Expand Down Expand Up @@ -275,6 +287,24 @@ def test_update_port(self):
{'name': 'myname', 'tags': ['a', 'b'], }
)

def test_update_port_secgroup(self):
resource = 'port'
cmd = port.UpdatePort(test_cli20.MyApp(sys.stdout), None)
myid = 'myid'
args = ['--security-group', 'sg1_id', myid]
updatefields = {'security_groups': ['sg1_id']}
self._test_update_resource(resource, cmd, myid, args, updatefields)

def test_update_port_secgroups(self):
resource = 'port'
cmd = port.UpdatePort(test_cli20.MyApp(sys.stdout), None)
myid = 'myid'
args = ['--security-group', 'sg1_id',
'--security-group', 'sg2_id',
myid]
updatefields = {'security_groups': ['sg1_id', 'sg2_id']}
self._test_update_resource(resource, cmd, myid, args, updatefields)

def test_update_port_security_group_off(self):
"""Update port: --no-security-groups myid."""
resource = 'port'
Expand Down

0 comments on commit bc99eca

Please sign in to comment.