Commit 14f14bc8 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Add 'ip_type' attribute to NIC



Extend NetworkInterface model with the 'ip_type' attribute to show
whether the IP is a floating or fixed.
Signed-off-by: default avatarChristos Stavrakakis <cstavr@grnet.gr>
parent 1a4633ef
......@@ -40,7 +40,7 @@ from snf_django.lib import api
from snf_django.lib.api import faults, utils
from synnefo.api import util
from synnefo import quotas
from synnefo.db.models import Network, FloatingIP
from synnefo.db.models import Network, FloatingIP, NetworkInterface
from logging import getLogger
......@@ -169,8 +169,13 @@ def allocate_floating_ip(request):
pool.save()
# If address is not available, check that it belongs to the
# same user
elif not network.nics.filter(ipv4=address,
machine__userid=userid).exists():
else:
try:
nic = network.nics.get(ipv4=address,
machine__userid=userid)
nic.ip_type = "FLOATING"
nic.save()
except NetworkInterface.DoesNotExist:
msg = "Address '%s' is already in use" % address
raise faults.Conflict(msg)
floating_ip = FloatingIP.objects.create(ipv4=address, network=network,
......
......@@ -105,13 +105,12 @@ def metadata_item_demux(request, server_id, key):
def nic_to_dict(nic):
ip_type = "floating" if nic.is_floating_ip else "fixed"
d = {'id': util.construct_nic_id(nic),
'network_id': str(nic.network.id),
'mac_address': nic.mac,
'ipv4': nic.ipv4,
'ipv6': nic.ipv6,
'OS-EXT-IPS:type': ip_type}
'OS-EXT-IPS:type': nic.ip_type.lower()}
if nic.firewall_profile:
d['firewallProfile'] = nic.firewall_profile
......
......@@ -675,6 +675,11 @@ class NetworkInterface(models.Model):
("ERROR", "Error"),
)
IP_TYPES = (
("FIXED", "Fixed IP Address"),
("FLOATING", "Floating IP Address"),
)
machine = models.ForeignKey(VirtualMachine, related_name='nics',
on_delete=models.CASCADE)
network = models.ForeignKey(Network, related_name='nics',
......@@ -689,6 +694,8 @@ class NetworkInterface(models.Model):
max_length=30, null=True)
state = models.CharField(max_length=32, null=False, default="ACTIVE",
choices=STATES)
ip_type = models.CharField(max_length=32, null=False, default="FIXED",
choices=IP_TYPES)
@property
def backend_uuid(self):
......@@ -700,15 +707,6 @@ class NetworkInterface(models.Model):
(self.id, self.machine_id, self.network_id, self.ipv4,
self.ipv6)
@property
def is_floating_ip(self):
network = self.network
if self.ipv4 and network.floating_ip_pool:
return network.floating_ips.filter(machine=self.machine,
ipv4=self.ipv4,
deleted=False).exists()
return False
class Meta:
# Assert than an IPv4 address from the same network will not be
# assigned to more than one NICs
......
......@@ -356,7 +356,8 @@ def connect(vm, network):
# Get a free IP from the address pool.
address = util.get_network_free_address(network)
nic = NetworkInterface.objects.create(machine=vm, network=network,
ipv4=address, state="BUILDING")
ip_type="STATIC", ipv4=address,
state="BUILDING")
log.info("Connecting VM %s to Network %s. NIC: %s", vm, network, nic)
return backend.connect_to_network(vm, nic)
......@@ -430,6 +431,7 @@ def add_floating_ip(vm, address):
nic = NetworkInterface.objects.create(machine=vm,
network=floating_ip.network,
ipv4=floating_ip.ipv4,
ip_type="FLOATING",
state="BUILDING")
log.info("Connecting VM %s to floating IP %s. NIC: %s", vm, floating_ip,
nic)
......
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