Ensure project IDs are handled correctly for Network/Subnets
[snaps.git] / snaps / openstack / create_instance.py
index cdc778f..dcb73cd 100644 (file)
@@ -30,6 +30,7 @@ __author__ = 'spisarski'
 logger = logging.getLogger('create_instance')
 
 POLL_INTERVAL = 3
+VOL_DETACH_TIMEOUT = 120
 STATUS_ACTIVE = 'ACTIVE'
 STATUS_DELETED = 'DELETED'
 
@@ -112,8 +113,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
                     'Found existing machine with name - %s',
                     self.instance_settings.name)
 
-                fips = neutron_utils.get_floating_ips(self.__neutron,
-                                                      self.__ports)
+                fips = neutron_utils.get_port_floating_ips(
+                    self.__neutron, self.__ports)
                 for port_id, fip in fips:
                     settings = self.instance_settings.floating_ip_settings
                     for fip_setting in settings:
@@ -135,7 +136,7 @@ class OpenStackVmInstance(OpenStackComputeObject):
         glance = glance_utils.glance_client(self._os_creds)
         self.__vm = nova_utils.create_server(
             self._nova, self.__neutron, glance, self.instance_settings,
-            self.image_settings, self.keypair_settings)
+            self.image_settings, self.project_id, self.keypair_settings)
         logger.info('Created instance with name - %s',
                     self.instance_settings.name)
 
@@ -164,15 +165,15 @@ class OpenStackVmInstance(OpenStackComputeObject):
                     cinder, volume_name=volume_name)
 
                 if volume and self.vm_active(block=True):
-                    timeout = 30
                     vm = nova_utils.attach_volume(
-                        self._nova, self.__neutron, self.__vm, volume, timeout)
+                        self._nova, self.__neutron, self.__vm, volume,
+                        self.project_id, timeout=VOL_DETACH_TIMEOUT)
 
                     if vm:
                         self.__vm = vm
                     else:
                         logger.warn('Volume [%s] not attached within timeout '
-                                    'of [%s]', volume.name, timeout)
+                                    'of [%s]', volume.name, VOL_DETACH_TIMEOUT)
                 else:
                     logger.warn('Unable to attach volume named [%s]',
                                 volume_name)
@@ -271,7 +272,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
                     cinder, volume_rec['id'])
                 if volume:
                     vm = nova_utils.detach_volume(
-                        self._nova, self.__neutron, self.__vm, volume, 30)
+                        self._nova, self.__neutron, self.__vm, volume,
+                        self.project_id, timeout=VOL_DETACH_TIMEOUT)
                     if vm:
                         self.__vm = vm
                     else:
@@ -316,7 +318,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
 
         for port_setting in port_settings:
             port = neutron_utils.get_port(
-                self.__neutron, port_settings=port_setting)
+                self.__neutron, port_settings=port_setting,
+                project_id=self.project_id)
             if port:
                 ports.append((port_setting.name, port))
 
@@ -356,7 +359,7 @@ class OpenStackVmInstance(OpenStackComputeObject):
         :return: Server object
         """
         return nova_utils.get_server_object_by_id(
-            self._nova, self.__neutron, self.__vm.id)
+            self._nova, self.__neutron, self.__vm.id, self.project_id)
 
     def get_console_output(self):
         """
@@ -504,7 +507,7 @@ class OpenStackVmInstance(OpenStackComputeObject):
                 STATUS_ACTIVE, block, self.instance_settings.vm_boot_timeout,
                 poll_interval):
             self.__vm = nova_utils.get_server_object_by_id(
-                self._nova, self.__neutron, self.__vm.id)
+                self._nova, self.__neutron, self.__vm.id, self.project_id)
             return True
         return False
 
@@ -771,19 +774,21 @@ class OpenStackVmInstance(OpenStackComputeObject):
             self._nova, self.__vm, reboot_type=reboot_type)
 
 
-def generate_creator(os_creds, vm_inst, image_config, keypair_config=None):
+def generate_creator(os_creds, vm_inst, image_config, project_id,
+                     keypair_config=None):
     """
     Initializes an OpenStackVmInstance object
     :param os_creds: the OpenStack credentials
     :param vm_inst: the SNAPS-OO VmInst domain object
     :param image_config: the associated ImageConfig object
+    :param project_id: the associated project ID
     :param keypair_config: the associated KeypairConfig object (optional)
     :return: an initialized OpenStackVmInstance object
     """
     nova = nova_utils.nova_client(os_creds)
     neutron = neutron_utils.neutron_client(os_creds)
     derived_inst_config = settings_utils.create_vm_inst_config(
-        nova, neutron, vm_inst)
+        nova, neutron, vm_inst, project_id)
 
     derived_inst_creator = OpenStackVmInstance(
         os_creds, derived_inst_config, image_config, keypair_config)