From 91d2fd9f9249126b135040b7f86387f887a414b3 Mon Sep 17 00:00:00 2001 From: spisarski Date: Thu, 30 Nov 2017 11:57:40 -0700 Subject: Added exception handling around adding security groups. 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 --- snaps/openstack/utils/nova_utils.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py index ffc9240..b735ee2 100644 --- a/snaps/openstack/utils/nova_utils.py +++ b/snaps/openstack/utils/nova_utils.py @@ -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): -- cgit 1.2.3-korg