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

cyclades: Add method to get ip stats to network

Extend Network model with a method to get the number of free and total
IPv4 addresses of the network. Also, add method to IPAddress model to
release its IPv4 address if any. Finally, update backend-list management
command to use 'ip_count' method of Network object.
parent 9f964975
......@@ -568,6 +568,17 @@ class Network(models.Model):
if subnet.ipversion == version:
return subnet.cidr
def ip_count(self):
"""Return the total and free IPv4 addresses of the network."""
subnets = self.subnets.filter(ipversion=4).prefetch_related("ip_pools")
total, free = 0, 0
for subnet in subnets:
for ip_pool in subnet.ip_pools.all():
pool = ip_pool.pool
total += pool.pool_size
free += pool.count_available()
return total, free
class InvalidBackendIdError(Exception):
def __init__(self, value):
self.value = value
......@@ -745,6 +756,15 @@ class IPAddress(models.Model):
def public(self):
return self.network.public
def release_address(self):
"""Release the IPv4 address."""
if self.ipversion == 4:
for pool_row in self.subnet.ip_pools.all():
ip_pool = pool_row.pool
if ip_pool.contains(self.address):
ip_pool.put(self.address)
ip_pool.save()
class NetworkInterface(models.Model):
FIREWALL_PROFILES = (
......
......@@ -242,7 +242,7 @@ class IPv6AddressFactory(IPv4AddressFactory):
class FloatingIPFactory(IPv4AddressFactory):
network = factory.SubFactory(NetworkFactory, public=False,
network = factory.SubFactory(NetworkFactory, public=True,
floating_ip_pool=True)
floating_ip = True
......
......@@ -53,9 +53,9 @@ class Command(ListCommand):
free_ips = 0
total_ips = 0
for network in util.backend_public_networks(backend):
pool = network.get_pool(locked=False)
free_ips += pool.count_available()
total_ips += pool.pool_size
total, free = network.ip_count()
total_ips += total
free_ips += free
return "%s/%s" % (free_ips, total_ips)
FIELDS = {
......
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