Commit 362e8c15 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Various fixes regarding floating IPs

Fix some issues regarding floating ips introduced by rebasing branch.
parent ab65a62f
......@@ -143,7 +143,7 @@ def allocate_floating_ip(request):
except IndexError:
except Network.DoesNotExist:
raise faults.ItemNotFound("Pool '%s' does not exist." % pool)
if address is None:
# User did not specified an IP address. Choose a random one
......@@ -827,7 +827,7 @@ def remove(request, net, args):
return HttpResponse(status=202)
def add_floating_ip(request, vm, args):
address = args.get("address")
if address is None:
......@@ -837,7 +837,7 @@ def add_floating_ip(request, vm, args):
return HttpResponse(status=202)
def remove_floating_ip(request, vm, args):
address = args.get("address")
if address is None:
......@@ -287,6 +287,7 @@ class FloatingIPActionsTest(BaseAPITest):
BackendNetworkFactory(, backend=self.vm.backend,
request = {"addFloatingIp": {"address": ip1.ipv4}}
mock().ModifyInstance.return_value = 1
response =, self.vm.userid, json.dumps(request), "json")
self.assertEqual(response.status_code, 202)
ip1_after = FloatingIP.objects.get(
......@@ -309,6 +310,7 @@ class FloatingIPActionsTest(BaseAPITest):
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=self.vm)
NetworkInterfaceFactory(machine=self.vm, ipv4=ip1.ipv4)
request = {"removeFloatingIp": {"address": ip1.ipv4}}
mock().ModifyInstance.return_value = 2
response =, self.vm.userid, json.dumps(request), "json")
self.assertEqual(response.status_code, 202)
# Yet used. Wait for the callbacks
......@@ -296,7 +296,7 @@ class ServerCreateAPITest(ComputeAPITest):
with mocked_quotaholder():
response = self.mypost('servers', 'test_user',
json.dumps(self.request), 'json')
json.dumps(self.request), 'json')
self.assertEqual(response.status_code, 503, "serviceUnavailable")
def test_create_network_settings(self, mrapi):
......@@ -59,7 +59,8 @@ from django.db.models import Q
from snf_django.lib.api import faults
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata,
Network, BackendNetwork, NetworkInterface,
BridgePoolTable, MacPrefixPoolTable, Backend)
BridgePoolTable, MacPrefixPoolTable, Backend,
from synnefo.db.pools import EmptyPool
from snf_django.lib.astakos import get_user
......@@ -224,12 +225,22 @@ def get_network(network_id, user_id, for_update=False, non_deleted=False):
network = objects.get(Q(userid=user_id) | Q(public=True),
if non_deleted and network.deleted:
raise faults.BadRequest("Networkhas been deleted.")
raise faults.BadRequest("Network has been deleted.")
return network
except (ValueError, Network.DoesNotExist):
raise faults.ItemNotFound('Network not found.')
def get_floating_ip(user_id, ipv4, for_update=False):
objects = FloatingIP.objects
if for_update:
objects = objects.select_for_update()
return objects.get(userid=user_id, ipv4=ipv4, deleted=False)
except FloatingIP.DoesNotExist:
raise faults.ItemNotFound("Floating IP does not exist.")
def validate_network_params(subnet, gateway=None, subnet6=None, gateway6=None):
# Use strict option to not all subnets with host bits set
......@@ -295,7 +295,7 @@ def nics_changed(old_nics, new_nics):
def release_instance_ips(vm, ganeti_nics):
old_addresses = set(vm.nics.values_list("network", "ipv4"))
new_addresses = set(map(lambda nic: (nic["network"], nic["ipv4"]),
new_addresses = set(map(lambda nic: (nic["network"].id, nic["ipv4"]),
to_release = old_addresses - new_addresses
for (network_id, ipv4) in to_release:
......@@ -202,7 +202,7 @@ def create(userid, name, password, flavor, image, metadata={},
vm.task_job_id = jobID
transaction.commit()"User %s created VM %s, NIC %s, Backend %s, JobID %s","User %s created VM %s, NICs %s, Backend %s, JobID %s",
userid, vm, nics, backend, str(jobID))
except GanetiApiError as e:
log.exception("Can not communicate to backend %s: %s.",
......@@ -249,9 +249,7 @@ def create_instance_nics(vm, userid, private_networks=[], floating_ips=[]):
address = util.get_network_free_address(network)
attachments.append((network, address))
for address in floating_ips:
floating_ip = add_floating_ip_to_vm(address=address,
floating_ip = add_floating_ip_to_vm(vm=vm, address=address)
network =
attachments.append((network, address))
for network_id in private_networks:
......@@ -423,7 +421,7 @@ def console(vm, console_type):
return console
def add_floating_ip(vm, address):
floating_ip = add_floating_ip_to_vm(vm, address)"Connecting VM %s to floating IP %s", vm, floating_ip)
......@@ -464,7 +462,7 @@ def add_floating_ip_to_vm(vm, address):
return floating_ip
def remove_floating_ip(vm, address):
user_id = vm.userid
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment