From dcaba759092b2557fbf3f01aee1a61ae789aa802 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 3 Dec 2024 15:10:11 +0000 Subject: [PATCH] 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 Closes-bug: #2089821 (cherry picked from commit ad6beea71dbeeefbbb84a9ff7fe2433120b459b0) --- openstack/compute/v2/_proxy.py | 14 ++++++++++---- openstack/tests/unit/compute/v2/test_proxy.py | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/openstack/compute/v2/_proxy.py b/openstack/compute/v2/_proxy.py index 675b47003..7d28bbdee 100644 --- a/openstack/compute/v2/_proxy.py +++ b/openstack/compute/v2/_proxy.py @@ -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 ========== diff --git a/openstack/tests/unit/compute/v2/test_proxy.py b/openstack/tests/unit/compute/v2/test_proxy.py index 996ccb88b..7de6c5e69 100644 --- a/openstack/tests/unit/compute/v2/test_proxy.py +++ b/openstack/tests/unit/compute/v2/test_proxy.py @@ -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'], )