Skip to content

Commit 5ef5cc9

Browse files
committed
compute: Add server create --no-security-group option
To allow users to create servers with no security groups associated with the ports. Change-Id: I91b1d9dd5c3fbba838640841d98341cd8ccb1b16 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent ecc744a commit 5ef5cc9

4 files changed

Lines changed: 103 additions & 22 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,14 +1356,26 @@ def get_parser(self, prog_name):
13561356
'This option requires cloud support.'
13571357
),
13581358
)
1359-
parser.add_argument(
1359+
secgroups = parser.add_mutually_exclusive_group()
1360+
secgroups.add_argument(
1361+
'--no-security-group',
1362+
dest='security_groups',
1363+
action='store_const',
1364+
const=[],
1365+
help=_(
1366+
'Do not associate a security group with ports attached to '
1367+
'this server. This does not affect the security groups '
1368+
'associated with pre-existing ports.'
1369+
),
1370+
)
1371+
secgroups.add_argument(
13601372
'--security-group',
13611373
metavar='<security-group>',
13621374
action='append',
1363-
default=[],
13641375
dest='security_groups',
13651376
help=_(
1366-
'Security group to assign to this server (name or ID) '
1377+
'Security group to associate with ports attached to this '
1378+
'server (name or ID) '
13671379
'(repeat option to set multiple groups)'
13681380
),
13691381
)
@@ -1980,22 +1992,24 @@ def _match_image(image_api, wanted_properties):
19801992
networks = 'auto'
19811993

19821994
# Check security group(s) exist and convert ID to name
1983-
security_groups = []
1984-
if self.app.client_manager.is_network_endpoint_enabled():
1985-
network_client = self.app.client_manager.network
1986-
for security_group in parsed_args.security_groups:
1987-
sg = network_client.find_security_group(
1988-
security_group, ignore_missing=False
1989-
)
1990-
# Use security group ID to avoid multiple security group have
1991-
# same name in neutron networking backend
1992-
security_groups.append({'name': sg.id})
1993-
else: # nova-network
1994-
for security_group in parsed_args.security_groups:
1995-
sg = compute_v2.find_security_group(
1996-
compute_client, security_group
1997-
)
1998-
security_groups.append({'name': sg['name']})
1995+
security_groups = None
1996+
if parsed_args.security_groups is not None:
1997+
security_groups = []
1998+
if self.app.client_manager.is_network_endpoint_enabled():
1999+
network_client = self.app.client_manager.network
2000+
for security_group in parsed_args.security_groups:
2001+
sg = network_client.find_security_group(
2002+
security_group, ignore_missing=False
2003+
)
2004+
# Use security group ID to avoid multiple security group
2005+
# have same name in neutron networking backend
2006+
security_groups.append({'name': sg.id})
2007+
else: # nova-network
2008+
for security_group in parsed_args.security_groups:
2009+
sg = compute_v2.find_security_group(
2010+
compute_client, security_group
2011+
)
2012+
security_groups.append({'name': sg['name']})
19992013

20002014
hints = {}
20012015
for key, values in parsed_args.hints.items():
@@ -2058,7 +2072,7 @@ def _match_image(image_api, wanted_properties):
20582072
if files:
20592073
kwargs['personality'] = files
20602074

2061-
if security_groups:
2075+
if security_groups is not None:
20622076
kwargs['security_groups'] = security_groups
20632077

20642078
if block_device_mapping_v2:

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,60 @@ def test_server_create_with_security_group_in_nova_network(self):
15441544
self.assertEqual(self.columns, columns)
15451545
self.assertEqual(self.datalist(), data)
15461546

1547+
def test_server_create_with_no_security_group(self):
1548+
arglist = [
1549+
'--image',
1550+
self.image.id,
1551+
'--flavor',
1552+
self.flavor.id,
1553+
'--no-security-group',
1554+
self.server.name,
1555+
]
1556+
verifylist = [
1557+
('image', self.image.id),
1558+
('flavor', self.flavor.id),
1559+
('key_name', None),
1560+
('properties', None),
1561+
('security_groups', []),
1562+
('hints', {}),
1563+
('server_group', None),
1564+
('config_drive', False),
1565+
('password', None),
1566+
('server_name', self.server.name),
1567+
]
1568+
1569+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1570+
columns, data = self.cmd.take_action(parsed_args)
1571+
1572+
self.compute_sdk_client.find_flavor.assert_has_calls(
1573+
[mock.call(self.flavor.id, ignore_missing=False)] * 2
1574+
)
1575+
self.network_client.find_security_group.assert_not_called()
1576+
self.image_client.find_image.assert_called_once_with(
1577+
self.image.id, ignore_missing=False
1578+
)
1579+
self.compute_sdk_client.create_server.assert_called_once_with(
1580+
name=self.server.name,
1581+
image_id=self.image.id,
1582+
flavor_id=self.flavor.id,
1583+
min_count=1,
1584+
max_count=1,
1585+
security_groups=[],
1586+
networks=[],
1587+
block_device_mapping=[
1588+
{
1589+
'uuid': self.image.id,
1590+
'boot_index': 0,
1591+
'source_type': 'image',
1592+
'destination_type': 'local',
1593+
'delete_on_termination': True,
1594+
},
1595+
],
1596+
)
1597+
1598+
self.assertEqual(self.columns, columns)
1599+
self.assertEqual(self.datalist(), data)
1600+
15471601
def test_server_create_with_network(self):
15481602
network_net1 = network_fakes.create_one_network()
15491603
network_net2 = network_fakes.create_one_network()

openstackclient/tests/unit/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ def check_parser(self, cmd, args, verify_args):
9393
f"Argument parse failed: {stderr.getvalue()}"
9494
)
9595
for av in verify_args:
96-
attr, value = av
96+
attr, expected_value = av
9797
if attr:
98+
actual_value = getattr(parsed_args, attr)
9899
self.assertIn(attr, parsed_args)
99-
self.assertEqual(value, getattr(parsed_args, attr))
100+
self.assertEqual(
101+
expected_value,
102+
actual_value,
103+
f'args.{attr}: expected: {expected_value}, got: '
104+
f'{actual_value}',
105+
)
100106
return parsed_args
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
features:
3+
- |
4+
The ``server create`` command now supports a ``--no-security-group``
5+
option. When provided, no security groups will be associated with ports
6+
created and attached to the server during server creation. This does not
7+
affect pre-created ports.

0 commit comments

Comments
 (0)