Added exception handling around adding security groups. 11/48111/1
authorspisarski <s.pisarski@cablelabs.com>
Thu, 30 Nov 2017 18:57:40 +0000 (11:57 -0700)
committerspisarski <s.pisarski@cablelabs.com>
Thu, 30 Nov 2017 18:57:40 +0000 (11:57 -0700)
When a VM instance has a security group associated with it and
the client attempts to add the same security group, a Pike pod
now raises a ClientException where previous releases simply return
None. This patch adds the appropriate exception handling that
allows several existing tests to pass not only with Pike but also
with all OpenStack versions.

JIRA: SNAPS-239

Change-Id: I282df5a2b516dab415ba5e2e7208176f2ba3f270
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
snaps/openstack/utils/nova_utils.py

index ffc9240..b735ee2 100644 (file)
@@ -21,7 +21,7 @@ from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives import serialization
 from cryptography.hazmat.primitives.asymmetric import rsa
 from novaclient.client import Client
-from novaclient.exceptions import NotFound
+from novaclient.exceptions import NotFound, ClientException
 
 from snaps import file_utils
 from snaps.domain.flavor import Flavor
@@ -605,7 +605,18 @@ def add_security_group(nova, vm, security_group_name):
     :param vm: the OpenStack server object (VM) to alter
     :param security_group_name: the name of the security group to add
     """
-    nova.servers.add_security_group(str(vm.id), security_group_name)
+    try:
+        nova.servers.add_security_group(str(vm.id), security_group_name)
+    except ClientException as e:
+        sec_grp_names = get_server_security_group_names(nova, vm)
+        if security_group_name in sec_grp_names:
+            logger.warn('Security group [%s] already added to VM [%s]',
+                        security_group_name, vm.name)
+            return
+
+        logger.error('Unexpected error while adding security group [%s] - %s',
+                     security_group_name, e)
+        raise
 
 
 def remove_security_group(nova, vm, security_group):