Skip to content

Commit bb153b7

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Refactor security group rule delete to use SDK"
2 parents 272ac55 + a29c973 commit bb153b7

7 files changed

Lines changed: 266 additions & 33 deletions

File tree

openstackclient/compute/v2/security_group.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,6 @@ def take_action(self, parsed_args):
169169
return zip(*sorted(six.iteritems(info)))
170170

171171

172-
class DeleteSecurityGroupRule(command.Command):
173-
"""Delete a security group rule"""
174-
175-
def get_parser(self, prog_name):
176-
parser = super(DeleteSecurityGroupRule, self).get_parser(prog_name)
177-
parser.add_argument(
178-
'rule',
179-
metavar='<rule>',
180-
help='Security group rule to delete (ID only)',
181-
)
182-
return parser
183-
184-
def take_action(self, parsed_args):
185-
186-
compute_client = self.app.client_manager.compute
187-
compute_client.security_group_rules.delete(parsed_args.rule)
188-
189-
190172
class ListSecurityGroup(command.Lister):
191173
"""List security groups"""
192174

openstackclient/network/common.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919

2020
@six.add_metaclass(abc.ABCMeta)
2121
class NetworkAndComputeCommand(command.Command):
22-
"""Network and Compute Command"""
22+
"""Network and Compute Command
23+
24+
Command class for commands that support implementation via
25+
the network or compute endpoint. Such commands have different
26+
implementations for take_action() and may even have different
27+
arguments.
28+
"""
2329

2430
def take_action(self, parsed_args):
2531
if self.app.client_manager.is_network_endpoint_enabled():
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
"""Security Group Rule action implementations"""
15+
16+
from openstackclient.network import common
17+
18+
19+
class DeleteSecurityGroupRule(common.NetworkAndComputeCommand):
20+
"""Delete a security group rule"""
21+
22+
def update_parser_common(self, parser):
23+
parser.add_argument(
24+
'rule',
25+
metavar='<rule>',
26+
help='Security group rule to delete (ID only)',
27+
)
28+
return parser
29+
30+
def take_action_network(self, client, parsed_args):
31+
obj = client.find_security_group_rule(parsed_args.rule)
32+
client.delete_security_group_rule(obj)
33+
34+
def take_action_compute(self, client, parsed_args):
35+
client.security_group_rules.delete(parsed_args.rule)

openstackclient/tests/compute/v2/fakes.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,68 @@ def create_hypervisors(attrs={}, count=2):
235235
return hypervisors
236236

237237

238+
class FakeSecurityGroupRule(object):
239+
"""Fake one or more security group rules."""
240+
241+
@staticmethod
242+
def create_one_security_group_rule(attrs={}, methods={}):
243+
"""Create a fake security group rule.
244+
245+
:param Dictionary attrs:
246+
A dictionary with all attributes
247+
:param Dictionary methods:
248+
A dictionary with all methods
249+
:return:
250+
A FakeResource object, with id, etc.
251+
"""
252+
# Set default attributes.
253+
security_group_rule_attrs = {
254+
'from_port': -1,
255+
'group': {},
256+
'id': 'security-group-rule-id-' + uuid.uuid4().hex,
257+
'ip_protocol': 'icmp',
258+
'ip_range': {'cidr': '0.0.0.0/0'},
259+
'parent_group_id': 'security-group-id-' + uuid.uuid4().hex,
260+
'to_port': -1,
261+
}
262+
263+
# Overwrite default attributes.
264+
security_group_rule_attrs.update(attrs)
265+
266+
# Set default methods.
267+
security_group_rule_methods = {}
268+
269+
# Overwrite default methods.
270+
security_group_rule_methods.update(methods)
271+
272+
security_group_rule = fakes.FakeResource(
273+
info=copy.deepcopy(security_group_rule_attrs),
274+
methods=copy.deepcopy(security_group_rule_methods),
275+
loaded=True)
276+
return security_group_rule
277+
278+
@staticmethod
279+
def create_security_group_rules(attrs={}, methods={}, count=2):
280+
"""Create multiple fake security group rules.
281+
282+
:param Dictionary attrs:
283+
A dictionary with all attributes
284+
:param Dictionary methods:
285+
A dictionary with all methods
286+
:param int count:
287+
The number of security group rules to fake
288+
:return:
289+
A list of FakeResource objects faking the security group rules
290+
"""
291+
security_group_rules = []
292+
for i in range(0, count):
293+
security_group_rules.append(
294+
FakeSecurityGroupRule.create_one_security_group_rule(
295+
attrs, methods))
296+
297+
return security_group_rules
298+
299+
238300
class FakeServer(object):
239301
"""Fake one or more compute servers."""
240302

openstackclient/tests/network/v2/fakes.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -462,28 +462,76 @@ def create_security_groups(attrs={}, methods={}, count=2):
462462
security_groups = []
463463
for i in range(0, count):
464464
security_groups.append(
465-
FakeRouter.create_one_security_group(attrs, methods))
465+
FakeSecurityGroup.create_one_security_group(attrs, methods))
466466

467467
return security_groups
468468

469+
470+
class FakeSecurityGroupRule(object):
471+
"""Fake one or more security group rules."""
472+
469473
@staticmethod
470-
def get_security_groups(security_groups=None, count=2):
471-
"""Get an iterable MagicMock object with a list of faked security groups.
474+
def create_one_security_group_rule(attrs={}, methods={}):
475+
"""Create a fake security group rule.
472476
473-
If security group list is provided, then initialize the Mock object
474-
with the list. Otherwise create one.
477+
:param Dictionary attrs:
478+
A dictionary with all attributes
479+
:param Dictionary methods:
480+
A dictionary with all methods
481+
:return:
482+
A FakeResource object, with id, name, etc.
483+
"""
484+
# Set default attributes.
485+
security_group_rule_attrs = {
486+
'description': 'security-group-rule-desc-' + uuid.uuid4().hex,
487+
'direction': 'ingress',
488+
'ethertype': 'IPv4',
489+
'id': 'security-group-rule-id-' + uuid.uuid4().hex,
490+
'name': 'security-group-rule-name-' + uuid.uuid4().hex,
491+
'port_range_max': None,
492+
'port_range_min': None,
493+
'protocol': None,
494+
'remote_group_id': 'remote-security-group-id-' + uuid.uuid4().hex,
495+
'remote_ip_prefix': None,
496+
'security_group_id': 'security-group-id-' + uuid.uuid4().hex,
497+
'tenant_id': 'project-id-' + uuid.uuid4().hex,
498+
}
499+
500+
# Overwrite default attributes.
501+
security_group_rule_attrs.update(attrs)
475502

476-
:param List security groups:
477-
A list of FakeResource objects faking security groups
503+
# Set default methods.
504+
security_group_rule_methods = {}
505+
506+
# Overwrite default methods.
507+
security_group_rule_methods.update(methods)
508+
509+
security_group_rule = fakes.FakeResource(
510+
info=copy.deepcopy(security_group_rule_attrs),
511+
methods=copy.deepcopy(security_group_rule_methods),
512+
loaded=True)
513+
return security_group_rule
514+
515+
@staticmethod
516+
def create_security_group_rules(attrs={}, methods={}, count=2):
517+
"""Create multiple fake security group rules.
518+
519+
:param Dictionary attrs:
520+
A dictionary with all attributes
521+
:param Dictionary methods:
522+
A dictionary with all methods
478523
:param int count:
479-
The number of security groups to fake
524+
The number of security group rules to fake
480525
:return:
481-
An iterable Mock object with side_effect set to a list of faked
482-
security groups
526+
A list of FakeResource objects faking the security group rules
483527
"""
484-
if security_groups is None:
485-
security_groups = FakeRouter.create_security_groups(count)
486-
return mock.MagicMock(side_effect=security_groups)
528+
security_group_rules = []
529+
for i in range(0, count):
530+
security_group_rules.append(
531+
FakeSecurityGroupRule.create_one_security_group_rule(
532+
attrs, methods))
533+
534+
return security_group_rules
487535

488536

489537
class FakeSubnet(object):
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
import mock
15+
16+
from openstackclient.network.v2 import security_group_rule
17+
from openstackclient.tests.compute.v2 import fakes as compute_fakes
18+
from openstackclient.tests.network.v2 import fakes as network_fakes
19+
20+
21+
class TestSecurityGroupRuleNetwork(network_fakes.TestNetworkV2):
22+
23+
def setUp(self):
24+
super(TestSecurityGroupRuleNetwork, self).setUp()
25+
26+
# Get a shortcut to the network client
27+
self.network = self.app.client_manager.network
28+
29+
30+
class TestSecurityGroupRuleCompute(compute_fakes.TestComputev2):
31+
32+
def setUp(self):
33+
super(TestSecurityGroupRuleCompute, self).setUp()
34+
35+
# Get a shortcut to the network client
36+
self.compute = self.app.client_manager.compute
37+
38+
39+
class TestDeleteSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork):
40+
41+
# The security group rule to be deleted.
42+
_security_group_rule = \
43+
network_fakes.FakeSecurityGroupRule.create_one_security_group_rule()
44+
45+
def setUp(self):
46+
super(TestDeleteSecurityGroupRuleNetwork, self).setUp()
47+
48+
self.network.delete_security_group_rule = mock.Mock(return_value=None)
49+
50+
self.network.find_security_group_rule = mock.Mock(
51+
return_value=self._security_group_rule)
52+
53+
# Get the command object to test
54+
self.cmd = security_group_rule.DeleteSecurityGroupRule(
55+
self.app, self.namespace)
56+
57+
def test_security_group_rule_delete(self):
58+
arglist = [
59+
self._security_group_rule.id,
60+
]
61+
verifylist = [
62+
('rule', self._security_group_rule.id),
63+
]
64+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
65+
66+
result = self.cmd.take_action(parsed_args)
67+
68+
self.network.delete_security_group_rule.assert_called_with(
69+
self._security_group_rule)
70+
self.assertEqual(None, result)
71+
72+
73+
class TestDeleteSecurityGroupRuleCompute(TestSecurityGroupRuleCompute):
74+
75+
# The security group rule to be deleted.
76+
_security_group_rule = \
77+
compute_fakes.FakeSecurityGroupRule.create_one_security_group_rule()
78+
79+
def setUp(self):
80+
super(TestDeleteSecurityGroupRuleCompute, self).setUp()
81+
82+
self.app.client_manager.network_endpoint_enabled = False
83+
84+
# Get the command object to test
85+
self.cmd = security_group_rule.DeleteSecurityGroupRule(self.app, None)
86+
87+
def test_security_group_rule_delete(self):
88+
arglist = [
89+
self._security_group_rule.id,
90+
]
91+
verifylist = [
92+
('rule', self._security_group_rule.id),
93+
]
94+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
95+
96+
result = self.cmd.take_action(parsed_args)
97+
98+
self.compute.security_group_rules.delete.assert_called_with(
99+
self._security_group_rule.id)
100+
self.assertEqual(None, result)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ openstack.compute.v2 =
104104
security_group_set = openstackclient.compute.v2.security_group:SetSecurityGroup
105105
security_group_show = openstackclient.compute.v2.security_group:ShowSecurityGroup
106106
security_group_rule_create = openstackclient.compute.v2.security_group:CreateSecurityGroupRule
107-
security_group_rule_delete = openstackclient.compute.v2.security_group:DeleteSecurityGroupRule
108107
security_group_rule_list = openstackclient.compute.v2.security_group:ListSecurityGroupRule
109108

110109
server_add_security_group = openstackclient.compute.v2.server:AddServerSecurityGroup
@@ -340,6 +339,7 @@ openstack.network.v2 =
340339
router_set = openstackclient.network.v2.router:SetRouter
341340
router_show = openstackclient.network.v2.router:ShowRouter
342341
security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup
342+
security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule
343343
subnet_list = openstackclient.network.v2.subnet:ListSubnet
344344

345345
openstack.object_store.v1 =

0 commit comments

Comments
 (0)