Ensure project IDs are handled correctly for Network/Subnets
[snaps.git] / snaps / openstack / utils / nova_utils.py
index ba902f9..b37e0a4 100644 (file)
@@ -56,7 +56,7 @@ def nova_client(os_creds):
 
 
 def create_server(nova, neutron, glance, instance_config, image_config,
-                  keypair_config=None):
+                  project_id, keypair_config=None):
     """
     Creates a VM instance
     :param nova: the nova client (required)
@@ -64,6 +64,7 @@ def create_server(nova, neutron, glance, instance_config, image_config,
     :param glance: the glance client (required)
     :param instance_config: the VMInstConfig object (required)
     :param image_config: the VM's ImageConfig object (required)
+    :param project_id: the associated project ID (required)
     :param keypair_config: the VM's KeypairConfig object (optional)
     :return: a snaps.domain.VmInst object
     """
@@ -71,7 +72,8 @@ def create_server(nova, neutron, glance, instance_config, image_config,
     ports = list()
 
     for port_setting in instance_config.port_settings:
-        port = neutron_utils.get_port(neutron, port_settings=port_setting)
+        port = neutron_utils.get_port(
+            neutron, port_settings=port_setting, project_id=project_id)
         if port:
             ports.append(port)
         else:
@@ -120,14 +122,15 @@ def create_server(nova, neutron, glance, instance_config, image_config,
 
         server = nova.servers.create(**args)
 
-        return __map_os_server_obj_to_vm_inst(neutron, server)
+        return __map_os_server_obj_to_vm_inst(neutron, server, project_id)
     else:
         raise NovaException(
             'Cannot create instance, image cannot be located with name %s',
             image_config.name)
 
 
-def get_server(nova, neutron, vm_inst_settings=None, server_name=None):
+def get_server(nova, neutron, vm_inst_settings=None, server_name=None,
+               project_id=None):
     """
     Returns a VmInst object for the first server instance found.
     :param nova: the Nova client
@@ -136,6 +139,7 @@ def get_server(nova, neutron, vm_inst_settings=None, server_name=None):
                              the query if not None
     :param server_name: the server with this name to return if vm_inst_settings
                         is not None
+    :param project_id: the assocaited project ID
     :return: a snaps.domain.VmInst object or None if not found
     """
     search_opts = dict()
@@ -146,7 +150,7 @@ def get_server(nova, neutron, vm_inst_settings=None, server_name=None):
 
     servers = nova.servers.list(search_opts=search_opts)
     for server in servers:
-        return __map_os_server_obj_to_vm_inst(neutron, server)
+        return __map_os_server_obj_to_vm_inst(neutron, server, project_id)
 
 
 def get_server_connection(nova, vm_inst_settings=None, server_name=None):
@@ -170,7 +174,7 @@ def get_server_connection(nova, vm_inst_settings=None, server_name=None):
         return server.links[0]
 
 
-def __map_os_server_obj_to_vm_inst(neutron, os_server):
+def __map_os_server_obj_to_vm_inst(neutron, os_server, project_id):
     """
     Returns a VmInst object for an OpenStack Server object
     :param neutron: the Neutron client (when None, ports will be empty)
@@ -187,7 +191,8 @@ def __map_os_server_obj_to_vm_inst(neutron, os_server):
     out_ports = list()
     if len(os_server.networks) > 0:
         for net_name, ips in os_server.networks.items():
-            network = neutron_utils.get_network(neutron, network_name=net_name)
+            network = neutron_utils.get_network(
+                neutron, network_name=net_name, project_id=project_id)
             ports = neutron_utils.get_ports(neutron, network, ips)
             for port in ports:
                 out_ports.append(port)
@@ -249,28 +254,30 @@ def get_server_console_output(nova, server):
     return None
 
 
-def get_latest_server_object(nova, neutron, server):
+def get_latest_server_object(nova, neutron, server, project_id):
     """
     Returns a server with a given id
     :param nova: the Nova client
     :param neutron: the Neutron client
     :param server: the old server object
+    :param project_id: the associated project ID
     :return: the list of servers or None if not found
     """
     server = __get_latest_server_os_object(nova, server)
-    return __map_os_server_obj_to_vm_inst(neutron, server)
+    return __map_os_server_obj_to_vm_inst(neutron, server, project_id)
 
 
-def get_server_object_by_id(nova, neutron, server_id):
+def get_server_object_by_id(nova, neutron, server_id, project_id):
     """
     Returns a server with a given id
     :param nova: the Nova client
     :param neutron: the Neutron client
     :param server_id: the server's id
+    :param project_id: the associated project ID
     :return: an SNAPS-OO VmInst object or None if not found
     """
     server = __get_latest_server_os_object_by_id(nova, server_id)
-    return __map_os_server_obj_to_vm_inst(neutron, server)
+    return __map_os_server_obj_to_vm_inst(neutron, server, project_id)
 
 
 def get_server_security_group_names(nova, server):
@@ -713,7 +720,7 @@ def update_quotas(nova, project_id, compute_quotas):
     return nova.quotas.update(project_id, **update_values)
 
 
-def attach_volume(nova, neutron, server, volume, timeout=120):
+def attach_volume(nova, neutron, server, volume, project_id, timeout=120):
     """
     Attaches a volume to a server. When the timeout parameter is used, a VmInst
     object with the proper volume updates is returned unless it has not been
@@ -722,6 +729,7 @@ def attach_volume(nova, neutron, server, volume, timeout=120):
     :param neutron: the neutron client
     :param server: the VMInst domain object
     :param volume: the Volume domain object
+    :param project_id: the associated project ID
     :param timeout: denotes the amount of time to block to determine if the
                     has been properly attached.
     :return: updated VmInst object
@@ -730,7 +738,7 @@ def attach_volume(nova, neutron, server, volume, timeout=120):
 
     start_time = time.time()
     while time.time() < start_time + timeout:
-        vm = get_server_object_by_id(nova, neutron, server.id)
+        vm = get_server_object_by_id(nova, neutron, server.id, project_id)
         for vol_dict in vm.volume_ids:
             if volume.id == vol_dict['id']:
                 return vm
@@ -741,7 +749,7 @@ def attach_volume(nova, neutron, server, volume, timeout=120):
             volume.id, server.id))
 
 
-def detach_volume(nova, neutron, server, volume, timeout=120):
+def detach_volume(nova, neutron, server, volume, project_id, timeout=120):
     """
     Detaches a volume to a server. When the timeout parameter is used, a VmInst
     object with the proper volume updates is returned unless it has not been
@@ -750,6 +758,7 @@ def detach_volume(nova, neutron, server, volume, timeout=120):
     :param neutron: the neutron client
     :param server: the VMInst domain object
     :param volume: the Volume domain object
+    :param project_id: the associated project ID
     :param timeout: denotes the amount of time to block to determine if the
                     has been properly detached.
     :return: updated VmInst object
@@ -758,7 +767,7 @@ def detach_volume(nova, neutron, server, volume, timeout=120):
 
     start_time = time.time()
     while time.time() < start_time + timeout:
-        vm = get_server_object_by_id(nova, neutron, server.id)
+        vm = get_server_object_by_id(nova, neutron, server.id, project_id)
         if len(vm.volume_ids) == 0:
             return vm
         else: