Changed the way floating IPs are getting assigned to VMs as the previous 27/51327/1
authorspisarski <s.pisarski@cablelabs.com>
Mon, 29 Jan 2018 21:05:18 +0000 (14:05 -0700)
committerspisarski <s.pisarski@cablelabs.com>
Mon, 29 Jan 2018 21:05:18 +0000 (14:05 -0700)
means was not compatible with python-novaclient==10.0.0

JIRA: SNAPS-258

Change-Id: I56f7a8341c81e0ae5596f67d4d15ef6c26e0e680
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
requirements.txt
snaps/openstack/create_instance.py
snaps/openstack/tests/create_instance_tests.py
snaps/openstack/tests/openstack_tests.py
snaps/openstack/utils/neutron_utils.py
snaps/openstack/utils/nova_utils.py

index 00e9357..137159f 100644 (file)
@@ -1,7 +1,7 @@
 # The order of packages is significant, because pip processes them in the order
 # of appearance. Changing the order has an impact on the overall integration
 # process, which may cause wedges in the gate later.
-python-novaclient>=9.0.0,<10 # Apache-2.0
+python-novaclient>=9.0.0 # Apache-2.0
 python-neutronclient>=6.3.0 # Apache-2.0
 python-keystoneclient>=3.8.0 # Apache-2.0
 python-glanceclient>=2.8.0 # Apache-2.0
index 631ac6b..cdc778f 100644 (file)
@@ -15,7 +15,7 @@
 import logging
 import time
 
-from novaclient.exceptions import NotFound, BadRequest
+from novaclient.exceptions import NotFound
 
 from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig
 from snaps.openstack.openstack_creator import OpenStackComputeObject
@@ -213,18 +213,15 @@ class OpenStackVmInstance(OpenStackComputeObject):
         # gateway
         ext_gateway = self.__ext_gateway_by_router(
             floating_ip_setting.router_name)
-        if ext_gateway:
-            subnet = neutron_utils.get_subnet(
-                self.__neutron,
-                subnet_name=floating_ip_setting.subnet_name)
+        if ext_gateway and self.vm_active(block=True):
             floating_ip = neutron_utils.create_floating_ip(
-                self.__neutron, ext_gateway)
+                self.__neutron, ext_gateway, port.id)
             self.__floating_ip_dict[floating_ip_setting.name] = floating_ip
 
             logger.info(
                 'Created floating IP %s via router - %s', floating_ip.ip,
                 floating_ip_setting.router_name)
-            self.__add_floating_ip(floating_ip, port, subnet)
+
             return floating_ip
         else:
             raise VmInstanceCreationError(
@@ -346,54 +343,6 @@ class OpenStackVmInstance(OpenStackComputeObject):
 
         return ports
 
-    def __add_floating_ip(self, floating_ip, port, subnet, timeout=30,
-                          poll_interval=POLL_INTERVAL):
-        """
-        Returns True when active else False
-        TODO - Make timeout and poll_interval configurable...
-        """
-        ip = None
-
-        if subnet:
-            # Take IP of subnet if there is one configured on which to place
-            # the floating IP
-            for fixed_ip in port.ips:
-                if fixed_ip['subnet_id'] == subnet.id:
-                    ip = fixed_ip['ip_address']
-                    break
-        else:
-            # Simply take the first
-            ip = port.ips[0]['ip_address']
-
-        if ip:
-            count = timeout / poll_interval
-            while count > 0:
-                logger.debug('Attempting to add floating IP to instance')
-                try:
-                    nova_utils.add_floating_ip_to_server(
-                        self._nova, self.__vm, floating_ip, ip)
-                    logger.info(
-                        'Added floating IP %s to port IP %s on instance %s',
-                        floating_ip.ip, ip, self.instance_settings.name)
-                    return
-                except BadRequest as bre:
-                    logger.error('Cannot add floating IP [%s]', bre)
-                    raise
-                except Exception as e:
-                    logger.warn(
-                        'Retry adding floating IP to instance. Last attempt '
-                        'failed with - %s', e)
-                    time.sleep(poll_interval)
-                    count -= 1
-                    pass
-        else:
-            raise VmInstanceCreationError(
-                'Unable find IP address on which to place the floating IP')
-
-        logger.error('Timeout attempting to add the floating IP to instance.')
-        raise VmInstanceCreationError(
-            'Timeout while attempting add floating IP to instance')
-
     def get_os_creds(self):
         """
         Returns the OpenStack credentials used to create these objects
@@ -624,8 +573,15 @@ class OpenStackVmInstance(OpenStackComputeObject):
                       timeout=None, poll_interval=POLL_INTERVAL):
         """
         Returns true when the VM can be accessed via SSH
+        :param user_override: overrides the user with which to create the
+                              connection
+        :param password: overrides the use of a password instead of a private
+                         key with which to create the connection
         :param block: When true, thread will block until active or timeout
                       value in seconds has been exceeded (False)
+        :param timeout: the number of seconds to retry obtaining the connection
+                        and overrides the ssh_connect_timeout member of the
+                        self.instance_settings object
         :param poll_interval: The polling interval
         :return: T/F
         """
index 55da144..c713794 100644 (file)
@@ -20,8 +20,8 @@ import unittest
 import uuid
 
 import os
-from neutronclient.common.exceptions import InvalidIpForSubnetClient
-from novaclient.exceptions import BadRequest
+from neutronclient.common.exceptions import (
+    InvalidIpForSubnetClient, BadRequest)
 
 from snaps import file_utils
 from snaps.config.flavor import FlavorConfig
index a3dec11..3c32eb7 100644 (file)
@@ -15,7 +15,6 @@
 import logging
 import re
 
-import pkg_resources
 from snaps import file_utils
 from snaps.config.image import ImageConfig
 from snaps.config.network import NetworkConfig, SubnetConfig
@@ -139,6 +138,10 @@ def get_credentials(os_env_file=None, proxy_settings_str=None,
     if overrides and isinstance(overrides, dict):
         creds_dict.update(overrides)
 
+    for key, value in creds_dict.items():
+        if value is not None and isinstance(value, str):
+            creds_dict[key] = value.replace('"', '').replace('\'', '')
+
     os_creds = OSCreds(**creds_dict)
     logger.info('OS Credentials = %s', os_creds.__str__)
     return os_creds
@@ -336,11 +339,10 @@ class OSNetworkConfig:
                  router_name=None, external_gateway=None,
                  netconf_override=None):
         """
-        :param netconf_override: dict() containing the reconfigured network_type,
-                                 physical_network and segmentation_id
+        :param netconf_override: dict() containing the reconfigured
+                                 network_type, physical_network and
+                                 segmentation_id
         """
-
-        network_conf = None
         if subnet_name and subnet_cidr:
             network_conf = NetworkConfig(
                 name=net_name, subnet_settings=[
index e94a40e..725e251 100644 (file)
@@ -744,20 +744,24 @@ def get_floating_ips(neutron, ports=None):
     return out
 
 
-def create_floating_ip(neutron, ext_net_name):
+def create_floating_ip(neutron, ext_net_name, port_id=None):
     """
     Returns the floating IP object that was created with this call
     :param neutron: the Neutron client
     :param ext_net_name: the name of the external network on which to apply the
                          floating IP address
+    :param port_id: the ID of the port to which the floating IP will be
+                    associated
     :return: the SNAPS FloatingIp object
     """
     logger.info('Creating floating ip to external network - ' + ext_net_name)
     ext_net = get_network(neutron, network_name=ext_net_name)
     if ext_net:
-        fip = neutron.create_floatingip(
-            body={'floatingip':
-                  {'floating_network_id': ext_net.id}})
+        body = {'floatingip': {'floating_network_id': ext_net.id}}
+        if port_id:
+            body['floatingip']['port_id'] = port_id
+
+        fip = neutron.create_floatingip(body=body)
 
         return FloatingIp(id=fip['floatingip']['id'],
                           ip=fip['floatingip']['floating_ip_address'])
index 279e2ec..a8e051e 100644 (file)
@@ -676,18 +676,6 @@ def remove_security_group(nova, vm, security_group):
     nova.servers.remove_security_group(str(vm.id), security_group.name)
 
 
-def add_floating_ip_to_server(nova, vm, floating_ip, ip_addr):
-    """
-    Adds a floating IP to a server instance
-    :param nova: the nova client
-    :param vm: VmInst domain object
-    :param floating_ip: FloatingIp domain object
-    :param ip_addr: the IP to which to bind the floating IP to
-    """
-    vm = __get_latest_server_os_object(nova, vm)
-    vm.add_floating_ip(floating_ip.ip, ip_addr)
-
-
 def get_compute_quotas(nova, project_id):
     """
     Returns a list of all available keypairs