compute: Use security group name or ID

Despite what Nova's api-ref currently says [1], the 'addSecurityGroup'
action only requires a security group name if using nova-network: it
will happily accept either a name or ID if using neutron.

We are using 'Proxy._get_resource' to allow users to pass either an ID
or an 'openstack.network.v2.security_group.SecurityGroup' object
(there's no implementation of Nova's deprecated SecurityGroup resource).
If given an ID, this help method will only populate the 'id' field of
the created resource. Thus, we need to use this if provided.

[1] https://docs.openstack.org/api-ref/compute/#add-security-group-to-a-server-addsecuritygroup-action

Change-Id: I3dd56414b32207a16c6c83971f0494ccb86e07d4
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Closes-bug: #2089821
(cherry picked from commit ad6beea71d)
This commit is contained in:
Stephen Finucane
2024-12-03 15:10:11 +00:00
committed by Ilia Petrov
parent c530a6566d
commit dcaba75909
2 changed files with 12 additions and 6 deletions

View File

@@ -1267,7 +1267,7 @@ class Proxy(proxy.Proxy):
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param security_group: Either the ID, Name of a security group or a
:param security_group: Either the ID or name of a security group or a
:class:`~openstack.network.v2.security_group.SecurityGroup`
instance.
@@ -1275,14 +1275,17 @@ class Proxy(proxy.Proxy):
"""
server = self._get_resource(_server.Server, server)
security_group = self._get_resource(_sg.SecurityGroup, security_group)
server.add_security_group(self, security_group.name)
server.add_security_group(
self,
security_group.name or security_group.id,
)
def remove_security_group_from_server(self, server, security_group):
"""Remove a security group from a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param security_group: Either the ID of a security group or a
:param security_group: Either the ID or name of a security group or a
:class:`~openstack.network.v2.security_group.SecurityGroup`
instance.
@@ -1290,7 +1293,10 @@ class Proxy(proxy.Proxy):
"""
server = self._get_resource(_server.Server, server)
security_group = self._get_resource(_sg.SecurityGroup, security_group)
server.remove_security_group(self, security_group.name)
server.remove_security_group(
self,
security_group.name or security_group.id,
)
# ========== Server IPs ==========

View File

@@ -1522,7 +1522,7 @@ class TestCompute(TestComputeProxy):
self._verify(
'openstack.compute.v2.server.Server.add_security_group',
self.proxy.add_security_group_to_server,
method_args=["value", {'id': 'id', 'name': 'sg'}],
method_args=["value", 'sg'],
expected_args=[self.proxy, 'sg'],
)
@@ -1530,7 +1530,7 @@ class TestCompute(TestComputeProxy):
self._verify(
'openstack.compute.v2.server.Server.remove_security_group',
self.proxy.remove_security_group_from_server,
method_args=["value", {'id': 'id', 'name': 'sg'}],
method_args=["value", 'sg'],
expected_args=[self.proxy, 'sg'],
)