Commit fdecc17f authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Clear FloatingIP when deleting NIC

Override 'delete' method of NetworkInterface model, to clear the
'machine' flag of the corresponding NIC, if any.
parent c40fb807
......@@ -712,6 +712,19 @@ class NetworkInterface(models.Model):
# assigned to more than one NICs
unique_together = ("network", "ipv4")
def delete(self):
"""Custom method for deleting NetworkInterfaces.
In case the NIC is of 'FLOATING' type, this method clears the 'machine'
flag of the FloatingIP object, before deleting the NIC.
"""
if self.ip_type == "FLOATING":
FloatingIP.objects.filter(machine=self.machine_id,
network=self.network_id,
ipv4=self.ipv4).update(machine=None)
super(NetworkInterface, self).delete()
class FloatingIP(models.Model):
userid = models.CharField("UUID of the owner", max_length=128,
......
......@@ -37,7 +37,7 @@ from datetime import datetime, timedelta
from synnefo.db.models import (Backend, VirtualMachine, Network,
BackendNetwork, BACKEND_STATUSES,
pooled_rapi_client, VirtualMachineDiagnostic,
Flavor)
Flavor, FloatingIP)
from synnefo.logic import utils
from synnefo import quotas
from synnefo.api.util import release_resource
......@@ -325,12 +325,18 @@ def release_nic_address(nic):
"""Release the IPv4 address of a NIC.
Check if an instance's NIC has an IPv4 address and release it if it is not
a Floating IP.
a Floating IP. If it is as Floating IP, then disassociate the FloatingIP
from the machine.
"""
if nic.ipv4 and not nic.ip_type == "FLOATING":
nic.network.release_address(nic.ipv4)
if nic.ipv4:
if nic.ip_type == "FLOATING":
FloatingIP.objects.filter(machine=nic.machine_id,
network=nic.network_id,
ipv4=nic.ipv4).update(machine=None)
else:
nic.network.release_address(nic.ipv4)
@transaction.commit_on_success
......
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