Skip to content

Commit 8e2f49f

Browse files
author
Huanxuan Ao
committed
Support bulk deletion for commands that exist in both network and compute.
Some delete commands in networkv2 are exist in both network and compute, They can use NetworkAndComputeDeleteclass to supprot bulk deletion and error handling and the codes are similar, so I change them all in this patch. The changed commands including: 1.floating ip delete 2.security group delete 3.security group rule delete Also, I update unit tests and docs for these commands in this patch. Change-Id: I6c94c3d10ba579ddd9b14d17673c821e3481fd8a Partially-Implements: blueprint multi-argument-network
1 parent 0ec711c commit 8e2f49f

12 files changed

Lines changed: 459 additions & 72 deletions

File tree

doc/source/command-objects/ip-floating.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ Create new floating IP address
6666
ip floating delete
6767
------------------
6868

69-
Delete floating IP
69+
Delete floating IP(s)
7070

7171
.. program:: ip floating delete
7272
.. code:: bash
7373
74-
os ip floating delete <floating-ip>
74+
os ip floating delete
75+
<floating-ip> [<floating-ip> ...]
7576
7677
.. describe:: <floating-ip>
7778

78-
Floating IP to delete (IP address or ID)
79+
Floating IP(s) to delete (IP address or ID)
7980

8081
ip floating list
8182
----------------

doc/source/command-objects/security-group-rule.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ Create a new security group rule
104104
security group rule delete
105105
--------------------------
106106
107-
Delete a security group rule
107+
Delete security group rule(s)
108108
109109
.. program:: security group rule delete
110110
.. code:: bash
111111
112112
os security group rule delete
113-
<rule>
113+
<rule> [<rule> ...]
114114
115115
.. describe:: <rule>
116116
117-
Security group rule to delete (ID only)
117+
Security group rule(s) to delete (ID only)
118118
119119
security group rule list
120120
------------------------

doc/source/command-objects/security-group.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ Create a new security group
4545
security group delete
4646
---------------------
4747
48-
Delete a security group
48+
Delete security group(s)
4949
5050
.. program:: security group delete
5151
.. code:: bash
5252
5353
os security group delete
54-
<group>
54+
<group> [<group> ...]
5555
5656
.. describe:: <group>
5757
58-
Security group to delete (name or ID)
58+
Security group(s) to delete (name or ID)
5959
6060
security group list
6161
-------------------

openstackclient/network/v2/floating_ip.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,28 @@ def take_action_compute(self, client, parsed_args):
110110
return (columns, data)
111111

112112

113-
class DeleteFloatingIP(common.NetworkAndComputeCommand):
114-
"""Delete floating IP"""
113+
class DeleteFloatingIP(common.NetworkAndComputeDelete):
114+
"""Delete floating IP(s)"""
115+
116+
# Used by base class to find resources in parsed_args.
117+
resource = 'floating_ip'
118+
r = None
115119

116120
def update_parser_common(self, parser):
117121
parser.add_argument(
118122
'floating_ip',
119123
metavar="<floating-ip>",
120-
help=_("Floating IP to delete (IP address or ID)")
124+
nargs="+",
125+
help=_("Floating IP(s) to delete (IP address or ID)")
121126
)
122127
return parser
123128

124129
def take_action_network(self, client, parsed_args):
125-
obj = client.find_ip(parsed_args.floating_ip)
130+
obj = client.find_ip(self.r, ignore_missing=False)
126131
client.delete_ip(obj)
127132

128133
def take_action_compute(self, client, parsed_args):
129-
obj = utils.find_resource(
130-
client.floating_ips,
131-
parsed_args.floating_ip,
132-
)
134+
obj = utils.find_resource(client.floating_ips, self.r)
133135
client.floating_ips.delete(obj.id)
134136

135137

openstackclient/network/v2/security_group.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,28 @@ def take_action_compute(self, client, parsed_args):
164164
return (display_columns, data)
165165

166166

167-
class DeleteSecurityGroup(common.NetworkAndComputeCommand):
168-
"""Delete a security group"""
167+
class DeleteSecurityGroup(common.NetworkAndComputeDelete):
168+
"""Delete security group(s)"""
169+
170+
# Used by base class to find resources in parsed_args.
171+
resource = 'group'
172+
r = None
169173

170174
def update_parser_common(self, parser):
171175
parser.add_argument(
172176
'group',
173177
metavar='<group>',
174-
help=_("Security group to delete (name or ID)")
178+
nargs="+",
179+
help=_("Security group(s) to delete (name or ID)"),
175180
)
176181
return parser
177182

178183
def take_action_network(self, client, parsed_args):
179-
obj = client.find_security_group(parsed_args.group)
184+
obj = client.find_security_group(self.r, ignore_missing=False)
180185
client.delete_security_group(obj)
181186

182187
def take_action_compute(self, client, parsed_args):
183-
data = utils.find_resource(
184-
client.security_groups,
185-
parsed_args.group,
186-
)
188+
data = utils.find_resource(client.security_groups, self.r)
187189
client.security_groups.delete(data.id)
188190

189191

openstackclient/network/v2/security_group_rule.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,29 @@ def take_action_compute(self, client, parsed_args):
333333
return _format_security_group_rule_show(obj._info)
334334

335335

336-
class DeleteSecurityGroupRule(common.NetworkAndComputeCommand):
337-
"""Delete a security group rule"""
336+
class DeleteSecurityGroupRule(common.NetworkAndComputeDelete):
337+
"""Delete security group rule(s)"""
338+
339+
# Used by base class to find resources in parsed_args.
340+
resource = 'rule'
341+
r = None
338342

339343
def update_parser_common(self, parser):
340344
parser.add_argument(
341345
'rule',
342346
metavar='<rule>',
343-
help=_("Security group rule to delete (ID only)")
347+
nargs="+",
348+
help=_("Security group rule(s) to delete (ID only)")
344349
)
345350
return parser
346351

347352
def take_action_network(self, client, parsed_args):
348-
obj = client.find_security_group_rule(parsed_args.rule)
353+
obj = client.find_security_group_rule(
354+
self.r, ignore_missing=False)
349355
client.delete_security_group_rule(obj)
350356

351357
def take_action_compute(self, client, parsed_args):
352-
client.security_group_rules.delete(parsed_args.rule)
358+
client.security_group_rules.delete(self.r)
353359

354360

355361
class ListSecurityGroupRule(common.NetworkAndComputeLister):

openstackclient/tests/compute/v2/fakes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,25 @@ def create_security_groups(attrs=None, count=2):
452452

453453
return security_groups
454454

455+
@staticmethod
456+
def get_security_groups(security_groups=None, count=2):
457+
"""Get an iterable MagicMock object with a list of faked security groups.
458+
459+
If security groups list is provided, then initialize the Mock object
460+
with the list. Otherwise create one.
461+
462+
:param List security groups:
463+
A list of FakeResource objects faking security groups
464+
:param int count:
465+
The number of security groups to fake
466+
:return:
467+
An iterable Mock object with side_effect set to a list of faked
468+
security groups
469+
"""
470+
if security_groups is None:
471+
security_groups = FakeSecurityGroup.create_security_groups(count)
472+
return mock.MagicMock(side_effect=security_groups)
473+
455474

456475
class FakeSecurityGroupRule(object):
457476
"""Fake one or more security group rules."""

openstackclient/tests/network/v2/fakes.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,25 @@ def create_security_groups(attrs=None, count=2):
611611

612612
return security_groups
613613

614+
@staticmethod
615+
def get_security_groups(security_groups=None, count=2):
616+
"""Get an iterable MagicMock object with a list of faked security groups.
617+
618+
If security groups list is provided, then initialize the Mock object
619+
with the list. Otherwise create one.
620+
621+
:param List security groups:
622+
A list of FakeResource objects faking security groups
623+
:param int count:
624+
The number of security groups to fake
625+
:return:
626+
An iterable Mock object with side_effect set to a list of faked
627+
security groups
628+
"""
629+
if security_groups is None:
630+
security_groups = FakeSecurityGroup.create_security_groups(count)
631+
return mock.MagicMock(side_effect=security_groups)
632+
614633

615634
class FakeSecurityGroupRule(object):
616635
"""Fake one or more security group rules."""
@@ -670,6 +689,26 @@ def create_security_group_rules(attrs=None, count=2):
670689

671690
return security_group_rules
672691

692+
@staticmethod
693+
def get_security_group_rules(security_group_rules=None, count=2):
694+
"""Get an iterable MagicMock object with a list of faked security group rules.
695+
696+
If security group rules list is provided, then initialize the Mock
697+
object with the list. Otherwise create one.
698+
699+
:param List security group rules:
700+
A list of FakeResource objects faking security group rules
701+
:param int count:
702+
The number of security group rules to fake
703+
:return:
704+
An iterable Mock object with side_effect set to a list of faked
705+
security group rules
706+
"""
707+
if security_group_rules is None:
708+
security_group_rules = (
709+
FakeSecurityGroupRule.create_security_group_rules(count))
710+
return mock.MagicMock(side_effect=security_group_rules)
711+
673712

674713
class FakeSubnet(object):
675714
"""Fake one or more subnets."""

0 commit comments

Comments
 (0)