Removed TODO from comment about the physical network value.
[snaps.git] / snaps / openstack / create_instance.py
index 189ea57..d5917a8 100644 (file)
@@ -126,7 +126,7 @@ class OpenStackVmInstance:
 
         if block:
             if not self.vm_active(block=True):
 
         if block:
             if not self.vm_active(block=True):
-                raise Exception(
+                raise VmInstanceCreationError(
                     'Fatal error, VM did not become ACTIVE within the alloted '
                     'time')
 
                     'Fatal error, VM did not become ACTIVE within the alloted '
                     'time')
 
@@ -136,7 +136,7 @@ class OpenStackVmInstance:
                 nova_utils.add_security_group(self.__nova, self.__vm,
                                               sec_grp_name)
             else:
                 nova_utils.add_security_group(self.__nova, self.__vm,
                                               sec_grp_name)
             else:
-                raise Exception(
+                raise VmInstanceCreationError(
                     'Cannot applying security group with name ' +
                     sec_grp_name +
                     ' to VM that did not activate with name - ' +
                     'Cannot applying security group with name ' +
                     sec_grp_name +
                     ' to VM that did not activate with name - ' +
@@ -157,7 +157,7 @@ class OpenStackVmInstance:
             port = port_dict.get(floating_ip_setting.port_name)
 
             if not port:
             port = port_dict.get(floating_ip_setting.port_name)
 
             if not port:
-                raise Exception(
+                raise VmInstanceCreationError(
                     'Cannot find port object with name - ' +
                     floating_ip_setting.port_name)
 
                     'Cannot find port object with name - ' +
                     floating_ip_setting.port_name)
 
@@ -178,9 +178,9 @@ class OpenStackVmInstance:
                     floating_ip_setting.router_name)
                 self.__add_floating_ip(floating_ip, port, subnet)
             else:
                     floating_ip_setting.router_name)
                 self.__add_floating_ip(floating_ip, port, subnet)
             else:
-                raise Exception('Unable to add floating IP to port,'
-                                ' cannot locate router with an external '
-                                'gateway ')
+                raise VmInstanceCreationError(
+                    'Unable to add floating IP to port, cannot locate router '
+                    'with an external gateway ')
 
     def __ext_gateway_by_router(self, router_name):
         """
 
     def __ext_gateway_by_router(self, router_name):
         """
@@ -314,11 +314,12 @@ class OpenStackVmInstance:
                     count -= 1
                     pass
         else:
                     count -= 1
                     pass
         else:
-            raise Exception(
+            raise VmInstanceCreationError(
                 'Unable find IP address on which to place the floating IP')
 
         logger.error('Timeout attempting to add the floating IP to instance.')
                 'Unable find IP address on which to place the floating IP')
 
         logger.error('Timeout attempting to add the floating IP to instance.')
-        raise Exception('Timeout while attempting add floating IP to instance')
+        raise VmInstanceCreationError(
+            'Timeout while attempting add floating IP to instance')
 
     def get_os_creds(self):
         """
 
     def get_os_creds(self):
         """
@@ -405,7 +406,7 @@ class OpenStackVmInstance:
         """
         Responsible for configuring NICs on RPM systems where the instance has
         more than one configured port
         """
         Responsible for configuring NICs on RPM systems where the instance has
         more than one configured port
-        :return: None
+        :return: the value returned by ansible_utils.apply_ansible_playbook()
         """
         if len(self.__ports) > 1 and len(self.__floating_ips) > 0:
             if self.vm_active(block=True) and self.vm_ssh_active(block=True):
         """
         if len(self.__ports) > 1 and len(self.__floating_ips) > 0:
             if self.vm_active(block=True) and self.vm_ssh_active(block=True):
@@ -413,11 +414,12 @@ class OpenStackVmInstance:
                     port_index = self.__ports.index((key, port))
                     if port_index > 0:
                         nic_name = 'eth' + repr(port_index)
                     port_index = self.__ports.index((key, port))
                     if port_index > 0:
                         nic_name = 'eth' + repr(port_index)
-                        self.__config_nic(
+                        retval = self.__config_nic(
                             nic_name, port,
                             self.__get_first_provisioning_floating_ip().ip)
                         logger.info('Configured NIC - %s on VM - %s',
                                     nic_name, self.instance_settings.name)
                             nic_name, port,
                             self.__get_first_provisioning_floating_ip().ip)
                         logger.info('Configured NIC - %s on VM - %s',
                                     nic_name, self.instance_settings.name)
+                        return retval
 
     def __get_first_provisioning_floating_ip(self):
         """
 
     def __get_first_provisioning_floating_ip(self):
         """
@@ -567,7 +569,8 @@ class OpenStackVmInstance:
             return False
 
         if status == 'ERROR':
             return False
 
         if status == 'ERROR':
-            raise Exception('Instance had an error during deployment')
+            raise VmInstanceCreationError(
+                'Instance had an error during deployment')
         logger.debug(
             'Instance status [%s] is - %s', self.instance_settings.name,
             status)
         logger.debug(
             'Instance status [%s] is - %s', self.instance_settings.name,
             status)
@@ -742,7 +745,7 @@ class VmInstanceSettings:
             elif isinstance(kwargs['security_group_names'], str):
                 self.security_group_names = [kwargs['security_group_names']]
             else:
             elif isinstance(kwargs['security_group_names'], str):
                 self.security_group_names = [kwargs['security_group_names']]
             else:
-                raise Exception(
+                raise VmInstanceSettingsError(
                     'Invalid data type for security_group_names attribute')
         else:
             self.security_group_names = set()
                     'Invalid data type for security_group_names attribute')
         else:
             self.security_group_names = set()
@@ -780,11 +783,11 @@ class VmInstanceSettings:
             self.availability_zone = None
 
         if not self.name or not self.flavor:
             self.availability_zone = None
 
         if not self.name or not self.flavor:
-            raise Exception(
+            raise VmInstanceSettingsError(
                 'Instance configuration requires the attributes: name, flavor')
 
         if len(self.port_settings) == 0:
                 'Instance configuration requires the attributes: name, flavor')
 
         if len(self.port_settings) == 0:
-            raise Exception(
+            raise VmInstanceSettingsError(
                 'Instance configuration requires port settings (aka. NICS)')
 
 
                 'Instance configuration requires port settings (aka. NICS)')
 
 
@@ -821,6 +824,24 @@ class FloatingIpSettings:
             self.provisioning = True
 
         if not self.name or not self.port_name or not self.router_name:
             self.provisioning = True
 
         if not self.name or not self.port_name or not self.router_name:
-            raise Exception(
+            raise FloatingIpSettingsError(
                 'The attributes name, port_name and router_name are required '
                 'for FloatingIPSettings')
                 'The attributes name, port_name and router_name are required '
                 'for FloatingIPSettings')
+
+
+class VmInstanceSettingsError(Exception):
+    """
+    Exception to be thrown when an VM instance settings are incorrect
+    """
+
+
+class FloatingIpSettingsError(Exception):
+    """
+    Exception to be thrown when an VM instance settings are incorrect
+    """
+
+
+class VmInstanceCreationError(Exception):
+    """
+    Exception to be thrown when an VM instance cannot be created
+    """