Skip to content

Commit d94468b

Browse files
authored
Merge pull request #252 from conkiztador/secgroups
Fix creating servers with multiple security groups
2 parents 073922b + 10233c1 commit d94468b

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

nova_plugin/server.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -851,17 +851,21 @@ def connect_security_group(nova_client, **kwargs):
851851
'Expected external resources server {0} and security-group {1} to '
852852
'be connected'.format(server_id, security_group_id))
853853

854+
def group_matches(security_group):
855+
return (
856+
security_group_id == security_group.id or
857+
security_group_name == security_group.name
858+
)
859+
# Since some security groups are already attached in
860+
# create this will ensure that they are not attached twice.
854861
server = nova_client.servers.get(server_id)
855-
for security_group in server.list_security_group():
856-
# Since some security groups are already attached in
857-
# create this will ensure that they are not attached twice.
858-
if security_group_id != security_group.id and \
859-
security_group_name != security_group.name:
860-
# to support nova security groups as well,
861-
# we connect the security group by name
862-
# (as connecting by id
863-
# doesn't seem to work well for nova SGs)
864-
server.add_security_group(security_group_name)
862+
present = any(map(group_matches, server.list_security_group()))
863+
# to support nova security groups as well,
864+
# we connect the security group by name
865+
# (as connecting by id
866+
# doesn't seem to work well for nova SGs)
867+
if not present:
868+
server.add_security_group(security_group_name)
865869

866870
_validate_security_group_and_server_connection_status(nova_client,
867871
server_id,

nova_plugin/tests/test_server.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# * See the License for the specific language governing permissions and
1414
# * limitations under the License.
1515

16+
import collections
1617
from os import path
1718
import tempfile
1819

@@ -1009,6 +1010,9 @@ def test_handle_boot_image_no_image(self, *_):
10091010

10101011
@mock.patch('openstack_plugin_common.OpenStackClient._validate_auth_params')
10111012
class TestServerSGAttachments(unittest.TestCase):
1013+
SecurityGroup = collections.namedtuple(
1014+
'SecurityGroup', ['id', 'name'], verbose=True)
1015+
10121016
def setUp(self):
10131017
ctx = MockCloudifyContext(
10141018
target=MockContext({
@@ -1036,14 +1040,32 @@ def setUp(self):
10361040
findctx.start()
10371041
self.addCleanup(findctx.stop)
10381042

1039-
def test_detach_already_detached(self, *kwargs):
1040-
fake_client = mock.MagicMock()
1041-
fake_client().servers.get = mock.MagicMock()
1042-
fake_client().servers.get().remove_security_group = mock.MagicMock(
1043-
side_effect=nova_exceptions.NotFound('test'))
1044-
with mock.patch('openstack_plugin_common.NovaClientWithSugar',
1045-
fake_client):
1046-
nova_plugin.server.disconnect_security_group()
1043+
@mock.patch('openstack_plugin_common.NovaClientWithSugar')
1044+
def test_detach_already_detached(self, client, *kwargs):
1045+
server = client.return_value.servers.get.return_value
1046+
server.remove_security_group.side_effect = \
1047+
nova_exceptions.NotFound('test')
1048+
nova_plugin.server.disconnect_security_group()
1049+
1050+
@mock.patch('openstack_plugin_common.NovaClientWithSugar')
1051+
def test_connect_not_connected(self, client, *kwargs):
1052+
security_groups = [self.SecurityGroup('test-sg-2', 'test-sg-2-name')]
1053+
server = client.return_value.servers.get.return_value
1054+
server.list_security_group.return_value = security_groups
1055+
server.add_security_group.side_effect = (
1056+
lambda _: security_groups.append(
1057+
self.SecurityGroup('test-sg', 'test-sg-name')))
1058+
nova_plugin.server.connect_security_group()
1059+
server.add_security_group.assert_called_once_with('test-sg-name')
1060+
1061+
@mock.patch('openstack_plugin_common.NovaClientWithSugar')
1062+
def test_connect_already_connected(self, client, *kwargs):
1063+
security_groups = [self.SecurityGroup('test-sg', 'test-sg-name'),
1064+
self.SecurityGroup('test-sg-2', 'test-sg-2-name')]
1065+
server = client.return_value.servers.get.return_value
1066+
server.list_security_group.return_value = security_groups
1067+
nova_plugin.server.connect_security_group()
1068+
server.add_security_group.assert_not_called()
10471069

10481070

10491071
class TestServerRelationships(unittest.TestCase):

0 commit comments

Comments
 (0)