Commit 882c4f21 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Add missing 'ips' module

parent e298e10b
import logging
from snf_django.lib.api import faults
from django.db import transaction
from synnefo import quotas
from synnefo.db import pools
from synnefo.db.models import (IPPoolTable, IPAddress)
log = logging.getLogger(__name__)
def allocate_ip_from_pools(pool_rows, userid, address=None, floating_ip=False):
"""Try to allocate a value from a number of pools.
This function takes as argument a number of PoolTable objects and tries to
allocate a value from them. If all pools are empty EmptyPool is raised.
If an address is specified and does not belong to any of the pools,
InvalidValue is raised.
for pool_row in pool_rows:
pool = pool_row.pool
value = pool.get(value=address)
subnet = pool_row.subnet
ipaddress = IPAddress.objects.create(subnet=subnet,,
return ipaddress
except pools.EmptyPool:
except pools.InvalidValue:
if address is None:
raise pools.EmptyPool("No more IP addresses available on pools %s" %
raise pools.InvalidValue("Address %s does not belong to pools %s" %
(address, pool_rows))
def allocate_ip(network, userid, address=None, floating_ip=False):
"""Try to allocate an IP from networks IP pools."""
if network.action == "DESTROY":
raise faults.Conflict("Can not allocate IP. Network %s is being"
" deleted" %
ip_pools = IPPoolTable.objects.select_for_update()\
return allocate_ip_from_pools(ip_pools, userid, address=address,
except pools.EmptyPool:
raise faults.Conflict("No more IP addresses available on network %s"
except pools.ValueNotAvailable:
raise faults.Conflict("IP address %s is already used." % address)
except pools.InvalidValue:
raise faults.BadRequest("Address %s does not belong to network %s" %
def allocate_public_ip(userid, floating_ip=False, backend=None):
"""Try to allocate a public or floating IP address.
Try to allocate a a public IPv4 address from one of the available networks.
If 'floating_ip' is set, only networks which are floating IP pools will be
used and the IPAddress that will be created will be marked as a floating
IP. If 'backend' is set, only the networks that exist in this backend will
be used.
ip_pool_rows = IPPoolTable.objects.select_for_update()\
if floating_ip:
ip_pool_rows = ip_pool_rows\
if backend is not None:
ip_pool_rows = ip_pool_rows\
return allocate_ip_from_pools(ip_pool_rows, userid,
except pools.EmptyPool:
ip_type = "floating" if floating_ip else "public"
log_msg = "Failed to allocate a %s IP. Reason:" % ip_type
if ip_pool_rows:
log_msg += " No network exists."
log_msg += " All network are full."
if backend is not None:
log_msg += " Backend: %s" % backend
exception_msg = "Can not allocate a %s IP address." % ip_type
if floating_ip:
raise faults.Conflict(exception_msg)
raise faults.ServiceUnavailable(exception_msg)
def create_floating_ip(userid, network=None, address=None):
if network is None:
floating_ip = allocate_public_ip(userid, floating_ip=True)
if not network.floating_ip_pool:
msg = ("Can not allocate floating IP. Network %s is"
" not a floating IP pool.")
raise faults.Conflict(msg %
if network.action == "DESTROY":
msg = "Can not allocate floating IP. Network %s is being deleted."
raise faults.Conflict(msg %
# Allocate the floating IP
floating_ip = allocate_ip(network, userid, address=address,
# Issue commission (quotas)
transaction.commit()"Created floating IP '%s' for user IP '%s'", floating_ip, userid)
return floating_ip
def delete_floating_ip(floating_ip):
if floating_ip.nic:
# This is safe, you also need for_update to attach floating IP to
# instance.
msg = "Floating IP '%s' is attached to instance." %
raise faults.Conflict(msg)
# Return the address of the floating IP back to pool
# And mark the floating IP as deleted
floating_ip.deleted = True
# Release quota for floating IP
quotas.issue_and_accept_commission(floating_ip, delete=True)
# Delete the floating IP from DB"Deleted floating IP '%s' of user '%s", floating_ip,
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