Skip to content

Commit bac9fb1

Browse files
committed
Refactor security group set to use SDK
Refactored the 'os security group set' command to use the SDK when neutron is enabled, but continue to use the nova client when nova network is enabled. This patch set also fixes a compute bug which ignores name and description when set to an empty value. Change-Id: I4225179dca4aedf799e1656ec49236bdedc5e9bd Partial-Bug: #1519511 Implements: blueprint neutron-client
1 parent f9add05 commit bac9fb1

4 files changed

Lines changed: 190 additions & 42 deletions

File tree

openstackclient/compute/v2/security_group.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -217,47 +217,6 @@ def take_action(self, parsed_args):
217217
) for s in rules))
218218

219219

220-
class SetSecurityGroup(command.Command):
221-
"""Set security group properties"""
222-
223-
def get_parser(self, prog_name):
224-
parser = super(SetSecurityGroup, self).get_parser(prog_name)
225-
parser.add_argument(
226-
'group',
227-
metavar='<group>',
228-
help='Security group to modify (name or ID)',
229-
)
230-
parser.add_argument(
231-
'--name',
232-
metavar='<new-name>',
233-
help='New security group name',
234-
)
235-
parser.add_argument(
236-
"--description",
237-
metavar="<description>",
238-
help="New security group description",
239-
)
240-
return parser
241-
242-
def take_action(self, parsed_args):
243-
compute_client = self.app.client_manager.compute
244-
data = utils.find_resource(
245-
compute_client.security_groups,
246-
parsed_args.group,
247-
)
248-
249-
if parsed_args.name:
250-
data.name = parsed_args.name
251-
if parsed_args.description:
252-
data.description = parsed_args.description
253-
254-
compute_client.security_groups.update(
255-
data,
256-
data.name,
257-
data.description,
258-
)
259-
260-
261220
class ShowSecurityGroup(command.ShowOne):
262221
"""Display security group details"""
263222

openstackclient/network/v2/security_group.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,59 @@ def take_action_compute(self, client, parsed_args):
8888
data = client.security_groups.list(search_opts=search)
8989
return self._get_return_data(data,
9090
include_project=parsed_args.all_projects)
91+
92+
93+
class SetSecurityGroup(common.NetworkAndComputeCommand):
94+
"""Set security group properties"""
95+
96+
def update_parser_common(self, parser):
97+
parser.add_argument(
98+
'group',
99+
metavar='<group>',
100+
help='Security group to modify (name or ID)',
101+
)
102+
parser.add_argument(
103+
'--name',
104+
metavar='<new-name>',
105+
help='New security group name',
106+
)
107+
parser.add_argument(
108+
"--description",
109+
metavar="<description>",
110+
help="New security group description",
111+
)
112+
return parser
113+
114+
def take_action_network(self, client, parsed_args):
115+
obj = client.find_security_group(parsed_args.group,
116+
ignore_missing=False)
117+
attrs = {}
118+
if parsed_args.name is not None:
119+
attrs['name'] = parsed_args.name
120+
if parsed_args.description is not None:
121+
attrs['description'] = parsed_args.description
122+
# NOTE(rtheis): Previous behavior did not raise a CommandError
123+
# if there were no updates. Maintain this behavior and issue
124+
# the update.
125+
client.update_security_group(obj, **attrs)
126+
return
127+
128+
def take_action_compute(self, client, parsed_args):
129+
data = utils.find_resource(
130+
client.security_groups,
131+
parsed_args.group,
132+
)
133+
134+
if parsed_args.name is not None:
135+
data.name = parsed_args.name
136+
if parsed_args.description is not None:
137+
data.description = parsed_args.description
138+
139+
# NOTE(rtheis): Previous behavior did not raise a CommandError
140+
# if there were no updates. Maintain this behavior and issue
141+
# the update.
142+
client.security_groups.update(
143+
data,
144+
data.name,
145+
data.description,
146+
)

openstackclient/tests/network/v2/test_security_group.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from openstackclient.network.v2 import security_group
1717
from openstackclient.tests.compute.v2 import fakes as compute_fakes
1818
from openstackclient.tests.network.v2 import fakes as network_fakes
19+
from openstackclient.tests import utils as tests_utils
1920

2021

2122
class TestSecurityGroupNetwork(network_fakes.TestNetworkV2):
@@ -230,3 +231,135 @@ def test_security_group_list_all_projects(self):
230231
self.compute.security_groups.list.assert_called_with(**kwargs)
231232
self.assertEqual(self.expected_columns_all_projects, columns)
232233
self.assertEqual(self.expected_data_all_projects, tuple(data))
234+
235+
236+
class TestSetSecurityGroupNetwork(TestSecurityGroupNetwork):
237+
238+
# The security group to be set.
239+
_security_group = \
240+
network_fakes.FakeSecurityGroup.create_one_security_group()
241+
242+
def setUp(self):
243+
super(TestSetSecurityGroupNetwork, self).setUp()
244+
245+
self.network.update_security_group = mock.Mock(return_value=None)
246+
247+
self.network.find_security_group = mock.Mock(
248+
return_value=self._security_group)
249+
250+
# Get the command object to test
251+
self.cmd = security_group.SetSecurityGroup(self.app, self.namespace)
252+
253+
def test_set_no_options(self):
254+
self.assertRaises(tests_utils.ParserException,
255+
self.check_parser, self.cmd, [], [])
256+
257+
def test_set_no_updates(self):
258+
arglist = [
259+
self._security_group.name,
260+
]
261+
verifylist = [
262+
('group', self._security_group.name),
263+
]
264+
265+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
266+
result = self.cmd.take_action(parsed_args)
267+
268+
self.network.update_security_group.assert_called_once_with(
269+
self._security_group,
270+
**{}
271+
)
272+
self.assertIsNone(result)
273+
274+
def test_set_all_options(self):
275+
new_name = 'new-' + self._security_group.name
276+
new_description = 'new-' + self._security_group.description
277+
arglist = [
278+
'--name', new_name,
279+
'--description', new_description,
280+
self._security_group.name,
281+
]
282+
verifylist = [
283+
('description', new_description),
284+
('group', self._security_group.name),
285+
('name', new_name),
286+
]
287+
288+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
289+
result = self.cmd.take_action(parsed_args)
290+
291+
attrs = {
292+
'description': new_description,
293+
'name': new_name,
294+
}
295+
self.network.update_security_group.assert_called_once_with(
296+
self._security_group,
297+
**attrs
298+
)
299+
self.assertIsNone(result)
300+
301+
302+
class TestSetSecurityGroupCompute(TestSecurityGroupCompute):
303+
304+
# The security group to be set.
305+
_security_group = \
306+
compute_fakes.FakeSecurityGroup.create_one_security_group()
307+
308+
def setUp(self):
309+
super(TestSetSecurityGroupCompute, self).setUp()
310+
311+
self.app.client_manager.network_endpoint_enabled = False
312+
313+
self.compute.security_groups.update = mock.Mock(return_value=None)
314+
315+
self.compute.security_groups.get = mock.Mock(
316+
return_value=self._security_group)
317+
318+
# Get the command object to test
319+
self.cmd = security_group.SetSecurityGroup(self.app, None)
320+
321+
def test_set_no_options(self):
322+
self.assertRaises(tests_utils.ParserException,
323+
self.check_parser, self.cmd, [], [])
324+
325+
def test_set_no_updates(self):
326+
arglist = [
327+
self._security_group.name,
328+
]
329+
verifylist = [
330+
('group', self._security_group.name),
331+
]
332+
333+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
334+
result = self.cmd.take_action(parsed_args)
335+
336+
self.compute.security_groups.update.assert_called_once_with(
337+
self._security_group,
338+
self._security_group.name,
339+
self._security_group.description
340+
)
341+
self.assertIsNone(result)
342+
343+
def test_set_all_options(self):
344+
new_name = 'new-' + self._security_group.name
345+
new_description = 'new-' + self._security_group.description
346+
arglist = [
347+
'--name', new_name,
348+
'--description', new_description,
349+
self._security_group.name,
350+
]
351+
verifylist = [
352+
('description', new_description),
353+
('group', self._security_group.name),
354+
('name', new_name),
355+
]
356+
357+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
358+
result = self.cmd.take_action(parsed_args)
359+
360+
self.compute.security_groups.update.assert_called_once_with(
361+
self._security_group,
362+
new_name,
363+
new_description
364+
)
365+
self.assertIsNone(result)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ openstack.compute.v2 =
100100
keypair_show = openstackclient.compute.v2.keypair:ShowKeypair
101101

102102
security_group_create = openstackclient.compute.v2.security_group:CreateSecurityGroup
103-
security_group_set = openstackclient.compute.v2.security_group:SetSecurityGroup
104103
security_group_show = openstackclient.compute.v2.security_group:ShowSecurityGroup
105104
security_group_rule_create = openstackclient.compute.v2.security_group:CreateSecurityGroupRule
106105
security_group_rule_list = openstackclient.compute.v2.security_group:ListSecurityGroupRule
@@ -340,6 +339,7 @@ openstack.network.v2 =
340339
router_show = openstackclient.network.v2.router:ShowRouter
341340
security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup
342341
security_group_list = openstackclient.network.v2.security_group:ListSecurityGroup
342+
security_group_set = openstackclient.network.v2.security_group:SetSecurityGroup
343343
security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule
344344
security_group_rule_show = openstackclient.network.v2.security_group_rule:ShowSecurityGroupRule
345345
subnet_delete = openstackclient.network.v2.subnet:DeleteSubnet

0 commit comments

Comments
 (0)