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

cyclades: Make API server tests pass

Refactor Cyclades code so that it follows the new models for Networks,
Subnets and IPAddresses. This refactor is not complete. Instead is the
necessary refactor for making tests for server API calls succeed.
parent c223cc55
......@@ -234,7 +234,7 @@ def release_floating_ip(request, floating_ip_id):
def network_to_pool(network):
pool = network.get_pool(with_lock=False)
pool = network.get_pool(locked=False)
return {"name": str(network.id),
"size": pool.pool_size,
"free": pool.count_available()}
......
......@@ -43,6 +43,7 @@ from snf_django.lib import api
from snf_django.lib.api import faults, utils
from synnefo.api import util
from synnefo.db import query as db_query
from synnefo.db.models import (VirtualMachine, VirtualMachineMetadata)
from synnefo.logic import servers, utils as logic_utils
......@@ -108,9 +109,16 @@ def nic_to_dict(nic):
d = {'id': nic.id,
'network_id': str(nic.network.id),
'mac_address': nic.mac,
'ipv4': nic.ipv4,
'ipv6': nic.ipv6,
'OS-EXT-IPS:type': nic.ip_type.lower()}
'ipv4': '',
'ipv6': ''}
for ip in nic.ips.filter(deleted=False).select_related("subnet"):
ip_type = "floating" if ip.floating_ip else "fixed"
if ip.subnet.ipversion == 4:
d["ipv4"] = ip.address
d["OS-EXT-IPS:type"] = ip_type
else:
d["ipv6"] = ip.address
d["OS-EXT-IPS:type"] = ip_type
if nic.firewall_profile:
d['firewallProfile'] = nic.firewall_profile
......@@ -121,9 +129,10 @@ def attachments_to_addresses(attachments):
addresses = {}
for nic in attachments:
net_nics = []
net_nics.append({"version": 4,
"addr": nic["ipv4"],
"OS-EXT-IPS:type": nic["OS-EXT-IPS:type"]})
if nic["ipv4"]:
net_nics.append({"version": 4,
"addr": nic["ipv4"],
"OS-EXT-IPS:type": nic["OS-EXT-IPS:type"]})
if nic["ipv6"]:
net_nics.append({"version": 6,
"addr": nic["ipv6"],
......@@ -180,16 +189,15 @@ def vm_to_dict(vm, detail=False):
def get_server_fqdn(vm):
fqdn_setting = settings.CYCLADES_SERVERS_FQDN
if fqdn_setting is None:
public_nics = vm.nics.filter(network__public=True, state="ACTIVE")
# Return the first public IPv4 address if exists
ipv4_nics = public_nics.exclude(ipv4=None)
if ipv4_nics:
return ipv4_nics[0].ipv4
# Else return the first public IPv6 address if exists
ipv6_nics = public_nics.exclude(ipv6=None)
if ipv6_nics:
return ipv6_nics[0].ipv6
return ""
address = db_query.get_server_public_ip(server=vm, version=4)
if address is None:
# Else return the first public IPv6 address if exists
address = db_query.get_server_public_ip(server=vm, version=6)
if address is None:
return ""
else:
return address
elif isinstance(fqdn_setting, basestring):
return fqdn_setting % {"id": vm.id}
else:
......@@ -214,13 +222,8 @@ def get_server_port_forwarding(vm, fqdn):
port_forwarding = {}
for dport, to_dest in settings.CYCLADES_PORT_FORWARDING.items():
if hasattr(to_dest, "__call__"):
public_nics = vm.nics.filter(network__public=True, state="ACTIVE")\
.exclude(ipv4=None).order_by('index')
if public_nics:
vm_ipv4 = public_nics[0].ipv4
else:
vm_ipv4 = None
to_dest = to_dest(vm_ipv4, vm.id, fqdn, vm.userid)
address = db_query.get_server_public_ip(server=vm, version=4)
to_dest = to_dest(address, vm.id, fqdn, vm.userid)
msg = ("Invalid setting: CYCLADES_PORT_FOWARDING."
" Value must be a tuple of two elements (host, port).")
if to_dest is None:
......
......@@ -35,7 +35,7 @@ import json
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder
from synnefo.db.models import IPAddress
from synnefo.db.models_factory import (IPAddressFactory, NetworkFactory,
from synnefo.db.models_factory import (FloatingIPFactory, NetworkFactory,
VirtualMachineFactory,
NetworkInterfaceFactory,
BackendNetworkFactory)
......@@ -52,19 +52,21 @@ URL = join_urls(compute_path, "os-floating-ips")
NETWORKS_URL = join_urls(compute_path, "networks")
SERVERS_URL = join_urls(compute_path, "servers")
IPAddressPoolFactory = partial(NetworkFactory, public=True, deleted=False,
floating_ips = IPAddress.objects.filter(floating_ip=True)
FloatingIPPoolFactory = partial(NetworkFactory, public=True, deleted=False,
floating_ip_pool=True)
class IPAddressAPITest(BaseAPITest):
class FloatingIPAPITest(BaseAPITest):
def test_no_floating_ip(self):
response = self.get(URL)
self.assertSuccess(response)
self.assertEqual(json.loads(response.content)["floating_ips"], [])
def test_list_ips(self):
ip = IPAddressFactory(userid="user1")
IPAddressFactory(userid="user1", deleted=True)
ip = FloatingIPFactory(userid="user1")
FloatingIPFactory(userid="user1", deleted=True)
with mocked_quotaholder():
response = self.get(URL, "user1")
self.assertSuccess(response)
......@@ -75,7 +77,7 @@ class IPAddressAPITest(BaseAPITest):
str(ip.network.id)})
def test_get_ip(self):
ip = IPAddressFactory(userid="user1")
ip = FloatingIPFactory(userid="user1")
with mocked_quotaholder():
response = self.get(URL + "/%s" % ip.id, "user1")
self.assertSuccess(response)
......@@ -86,26 +88,26 @@ class IPAddressAPITest(BaseAPITest):
str(ip.network.id)})
def test_wrong_user(self):
ip = IPAddressFactory(userid="user1")
ip = FloatingIPFactory(userid="user1")
with mocked_quotaholder():
response = self.delete(URL + "/%s" % ip.id, "user2")
self.assertItemNotFound(response)
def test_deleted_ip(self):
ip = IPAddressFactory(userid="user1", deleted=True)
ip = FloatingIPFactory(userid="user1", deleted=True)
with mocked_quotaholder():
response = self.delete(URL + "/%s" % ip.id, "user1")
self.assertItemNotFound(response)
def test_reserve(self):
net = IPAddressPoolFactory(userid="test_user",
net = FloatingIPPoolFactory(userid="test_user",
subnet="192.168.2.0/24",
gateway=None)
request = {'pool': net.id}
with mocked_quotaholder():
response = self.post(URL, "test_user", json.dumps(request), "json")
self.assertSuccess(response)
ip = IPAddress.objects.get()
ip = floating_ips.get()
self.assertEqual(ip.ipv4, "192.168.2.1")
self.assertEqual(ip.machine, None)
self.assertEqual(ip.network, net)
......@@ -120,27 +122,27 @@ class IPAddressAPITest(BaseAPITest):
response = self.post(URL, "test_user", json.dumps({}), "json")
self.assertFault(response, 413, "overLimit")
# Full network
IPAddressPoolFactory(userid="test_user",
FloatingIPPoolFactory(userid="test_user",
subnet="192.168.2.0/32",
gateway=None)
with mocked_quotaholder():
response = self.post(URL, "test_user", json.dumps({}), "json")
self.assertFault(response, 413, "overLimit")
# Success
net2 = IPAddressPoolFactory(userid="test_user",
net2 = FloatingIPPoolFactory(userid="test_user",
subnet="192.168.2.0/24",
gateway=None)
with mocked_quotaholder():
response = self.post(URL, "test_user", json.dumps({}), "json")
self.assertSuccess(response)
ip = IPAddress.objects.get()
ip = floating_ips.get()
self.assertEqual(json.loads(response.content)["floating_ip"],
{"instance_id": None, "ip": "192.168.2.1",
"fixed_ip": None, "id": str(ip.id),
"pool": str(net2.id)})
def test_reserve_full(self):
net = IPAddressPoolFactory(userid="test_user",
net = FloatingIPPoolFactory(userid="test_user",
subnet="192.168.2.0/32")
request = {'pool': net.id}
with mocked_quotaholder():
......@@ -148,20 +150,20 @@ class IPAddressAPITest(BaseAPITest):
self.assertEqual(response.status_code, 413)
def test_reserve_with_address(self):
net = IPAddressPoolFactory(userid="test_user",
net = FloatingIPPoolFactory(userid="test_user",
subnet="192.168.2.0/24")
request = {'pool': net.id, "address": "192.168.2.10"}
with mocked_quotaholder():
response = self.post(URL, "test_user", json.dumps(request), "json")
self.assertSuccess(response)
ip = IPAddress.objects.get()
ip = floating_ips.get()
self.assertEqual(json.loads(response.content)["floating_ip"],
{"instance_id": None, "ip": "192.168.2.10",
"fixed_ip": None, "id": str(ip.id), "pool":
str(net.id)})
# Already reserved
IPAddressFactory(network=net, ipv4="192.168.2.3")
FloatingIPFactory(network=net, ipv4="192.168.2.3")
request = {'pool': net.id, "address": "192.168.2.3"}
with mocked_quotaholder():
response = self.post(URL, "test_user", json.dumps(request), "json")
......@@ -194,7 +196,7 @@ class IPAddressAPITest(BaseAPITest):
self.assertBadRequest(response)
def test_release_in_use(self):
ip = IPAddressFactory()
ip = FloatingIPFactory()
vm = ip.machine
vm.operstate = "ACTIVE"
vm.userid = ip.userid
......@@ -218,16 +220,16 @@ class IPAddressAPITest(BaseAPITest):
self.assertFault(response, 409, "conflict")
def test_release(self):
ip = IPAddressFactory(machine=None)
ip = FloatingIPFactory(machine=None)
with mocked_quotaholder():
response = self.delete(URL + "/%s" % ip.id, ip.userid)
self.assertSuccess(response)
ips_after = IPAddress.objects.filter(id=ip.id)
ips_after = floating_ips.filter(id=ip.id)
self.assertEqual(len(ips_after), 0)
@patch("synnefo.logic.backend", Mock())
def test_delete_network_with_floating_ips(self):
ip = IPAddressFactory(machine=None, network__flavor="IP_LESS_ROUTED")
ip = FloatingIPFactory(machine=None, network__flavor="IP_LESS_ROUTED")
net = ip.network
# Can not remove network with floating IPs
with mocked_quotaholder():
......@@ -246,14 +248,14 @@ class IPAddressAPITest(BaseAPITest):
POOLS_URL = join_urls(compute_path, "os-floating-ip-pools")
class IPAddressPoolsAPITest(BaseAPITest):
class FloatingIPPoolsAPITest(BaseAPITest):
def test_no_pool(self):
response = self.get(POOLS_URL)
self.assertSuccess(response)
self.assertEqual(json.loads(response.content)["floating_ip_pools"], [])
def test_list_pools(self):
net = IPAddressPoolFactory(subnet="192.168.0.0/30",
net = FloatingIPPoolFactory(subnet="192.168.0.0/30",
gateway="192.168.0.1")
NetworkFactory(public=True, deleted=True)
NetworkFactory(public=False, deleted=False)
......@@ -264,7 +266,7 @@ class IPAddressPoolsAPITest(BaseAPITest):
[{"name": str(net.id), "size": 4, "free": 1}])
class IPAddressActionsTest(BaseAPITest):
class FloatingIPActionsTest(BaseAPITest):
def setUp(self):
vm = VirtualMachineFactory()
vm.operstate = "ACTIVE"
......@@ -289,21 +291,21 @@ class IPAddressActionsTest(BaseAPITest):
self.assertItemNotFound(response)
# In use
vm1 = VirtualMachineFactory()
ip1 = IPAddressFactory(userid=self.vm.userid, machine=vm1)
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=vm1)
BackendNetworkFactory(network=ip1.network, backend=vm1.backend,
operstate='ACTIVE')
request = {"addFloatingIp": {"address": ip1.ipv4}}
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertFault(response, 409, "conflict")
# Success
ip1 = IPAddressFactory(userid=self.vm.userid, machine=None)
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None)
BackendNetworkFactory(network=ip1.network, backend=self.vm.backend,
operstate='ACTIVE')
request = {"addFloatingIp": {"address": ip1.ipv4}}
mock().ModifyInstance.return_value = 1
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertEqual(response.status_code, 202)
ip1_after = IPAddress.objects.get(id=ip1.id)
ip1_after = floating_ips.get(id=ip1.id)
self.assertEqual(ip1_after.machine, self.vm)
self.assertTrue(ip1_after.in_use())
nic = self.vm.nics.get(ipv4=ip1_after.ipv4)
......@@ -323,18 +325,18 @@ class IPAddressActionsTest(BaseAPITest):
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertItemNotFound(response)
# Not In Use
ip1 = IPAddressFactory(userid=self.vm.userid, machine=None)
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None)
request = {"removeFloatingIp": {"address": ip1.ipv4}}
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertItemNotFound(response)
# Success
ip1 = IPAddressFactory(userid=self.vm.userid, machine=self.vm)
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.post(url, self.vm.userid, json.dumps(request), "json")
self.assertEqual(response.status_code, 202)
# Yet used. Wait for the callbacks
ip1_after = IPAddress.objects.get(id=ip1.id)
ip1_after = floating_ips.get(id=ip1.id)
self.assertEqual(ip1_after.machine, self.vm)
self.assertTrue(ip1_after.in_use())
......@@ -122,9 +122,12 @@ class ServerAPITest(ComputeAPITest):
"""Test if a server details are returned."""
db_vm = self.vm2
user = self.vm2.userid
net = mfactory.NetworkFactory()
nic = mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net,
ipv6="::babe")
ip4 = mfactory.IPv4AddressFactory(nic__machine=self.vm2)
nic = ip4.nic
net = ip4.network
ip6 = mfactory.IPv6AddressFactory(nic=nic, network=net)
nic.mac = "aa:00:11:22:33:44"
nic.save()
db_vm_meta = mfactory.VirtualMachineMetadataFactory(vm=db_vm)
......@@ -141,14 +144,14 @@ class ServerAPITest(ComputeAPITest):
self.assertEqual(api_nic['network_id'], str(net.id))
self.assertEqual(api_nic['mac_address'], nic.mac)
self.assertEqual(api_nic['firewallProfile'], nic.firewall_profile)
self.assertEqual(api_nic['ipv4'], nic.ipv4)
self.assertEqual(api_nic['ipv6'], nic.ipv6)
self.assertEqual(api_nic['ipv4'], ip4.address)
self.assertEqual(api_nic['ipv6'], ip6.address)
self.assertEqual(api_nic['OS-EXT-IPS:type'], "fixed")
self.assertEqual(api_nic['id'], nic.id)
api_address = server["addresses"]
self.assertEqual(api_address[str(net.id)], [
{"version": 4, "addr": nic.ipv4, "OS-EXT-IPS:type": "fixed"},
{"version": 6, "addr": nic.ipv6, "OS-EXT-IPS:type": "fixed"}
{"version": 4, "addr": ip4.address, "OS-EXT-IPS:type": "fixed"},
{"version": 6, "addr": ip6.address, "OS-EXT-IPS:type": "fixed"}
])
metadata = server['metadata']
......@@ -184,24 +187,22 @@ class ServerAPITest(ComputeAPITest):
self.assertEqual(server["SNF:fqdn"], "")
# IPv6 NIC
nic = mfactory.NetworkInterfaceFactory(machine=vm, ipv4=None,
ipv6="babe::", state="ACTIVE",
network__public=True)
ipv6_address = mfactory.IPv6AddressFactory(nic__machine=vm,
network__public=True)
with override_settings(settings,
CYCLADES_SERVERS_FQDN=None):
response = self.myget("servers/%d" % vm.id, vm.userid)
server = json.loads(response.content)['server']
self.assertEqual(server["SNF:fqdn"], nic.ipv6)
self.assertEqual(server["SNF:fqdn"], ipv6_address.address)
# IPv4 NIC
nic = mfactory.NetworkInterfaceFactory(machine=vm,
network__public=True,
state="ACTIVE")
ipv4_address = mfactory.IPv4AddressFactory(nic__machine=vm,
network__public=True)
with override_settings(settings,
CYCLADES_SERVERS_FQDN=None):
response = self.myget("servers/%d" % vm.id, vm.userid)
server = json.loads(response.content)['server']
self.assertEqual(server["SNF:fqdn"], nic.ipv4)
self.assertEqual(server["SNF:fqdn"], ipv4_address.address)
def test_server_port_forwarding(self):
vm = mfactory.VirtualMachineFactory()
......@@ -229,8 +230,9 @@ class ServerAPITest(ComputeAPITest):
server = json.loads(response.content)['server']
self.assertEqual(server["SNF:port_forwarding"], {})
mfactory.NetworkInterfaceFactory(machine=vm, ipv4="192.168.2.2",
network__public=True)
mfactory.IPv4AddressFactory(nic__machine=vm,
network__public=True,
address="192.168.2.2")
with override_settings(settings,
CYCLADES_PORT_FORWARDING=ports):
response = self.myget("servers/%d" % vm.id, vm.userid)
......@@ -335,7 +337,8 @@ class ServerCreateAPITest(ComputeAPITest):
def setUp(self):
self.flavor = mfactory.FlavorFactory()
# Create public network and backend
self.network = mfactory.NetworkFactory(public=True)
subnet = mfactory.IPv4SubnetFactory(network__public=True)
self.network = subnet.network
self.backend = mfactory.BackendFactory()
mfactory.BackendNetworkFactory(network=self.network,
backend=self.backend,
......@@ -386,16 +389,23 @@ class ServerCreateAPITest(ComputeAPITest):
def test_create_network_settings(self, mrapi):
mrapi().CreateInstance.return_value = 12
bnet1 = mfactory.BackendNetworkFactory(operstate="ACTIVE",
backend=self.backend)
bnet2 = mfactory.BackendNetworkFactory(operstate="ACTIVE",
backend=self.backend)
bnet3 = mfactory.BackendNetworkFactory(network__userid="test_user",
operstate="ACTIVE",
backend=self.backend)
bnet4 = mfactory.BackendNetworkFactory(network__userid="test_user",
operstate="ACTIVE",
backend=self.backend)
# Create public network and backend
subnet1 = mfactory.IPv4SubnetFactory()
bnet1 = mfactory.BackendNetworkFactory(network=subnet1.network,
backend=self.backend,
operstate="ACTIVE")
subnet2 = mfactory.IPv4SubnetFactory()
bnet2 = mfactory.BackendNetworkFactory(network=subnet2.network,
backend=self.backend,
operstate="ACTIVE")
subnet3 = mfactory.IPv4SubnetFactory(network__userid="test_user")
bnet3 = mfactory.BackendNetworkFactory(network=subnet3.network,
backend=self.backend,
operstate="ACTIVE")
subnet4 = mfactory.IPv4SubnetFactory(network__userid="test_user")
bnet4 = mfactory.BackendNetworkFactory(network=subnet4.network,
backend=self.backend,
operstate="ACTIVE")
# User requested private networks
request = deepcopy(self.request)
request["server"]["networks"] = [bnet3.network.id, bnet4.network.id]
......@@ -464,17 +474,13 @@ class ServerCreateAPITest(ComputeAPITest):
# Test floating IPs
request = deepcopy(self.request)
request["server"]["networks"] = [bnet4.network.id]
network = mfactory.NetworkFactory(subnet="10.0.0.0/24")
mfactory.BackendNetworkFactory(network=network,
backend=self.backend,
operstate="ACTIVE")
fp1 = mfactory.IPAddressFactory(ipv4="10.0.0.2",
fp1 = mfactory.FloatingIPFactory(address="10.0.0.2",
userid="test_user",
network=network, machine=None)
fp2 = mfactory.IPAddressFactory(ipv4="10.0.0.3", network=network,
nic=None)
fp2 = mfactory.FloatingIPFactory(address="10.0.0.3",
userid="test_user",
machine=None)
request["server"]["floating_ips"] = [fp1.ipv4, fp2.ipv4]
nic=None)
request["server"]["floating_ips"] = [fp1.address, fp2.address]
with override_settings(settings,
DEFAULT_INSTANCE_NETWORKS=[bnet3.network.id]):
with mocked_quotaholder():
......@@ -483,18 +489,18 @@ class ServerCreateAPITest(ComputeAPITest):
self.assertEqual(response.status_code, 202)
api_server = json.loads(response.content)['server']
vm = VirtualMachine.objects.get(id=api_server["id"])
fp1 = IPAddress.objects.get(id=fp1.id)
fp2 = IPAddress.objects.get(id=fp2.id)
self.assertEqual(fp1.machine, vm)
self.assertEqual(fp2.machine, vm)
fp1 = IPAddress.objects.get(floating_ip=True, id=fp1.id)
fp2 = IPAddress.objects.get(floating_ip=True, id=fp2.id)
self.assertEqual(fp1.nic.machine, vm)
self.assertEqual(fp2.nic.machine, vm)
name, args, kwargs = mrapi().CreateInstance.mock_calls[2]
self.assertEqual(len(kwargs["nics"]), 4)
self.assertEqual(kwargs["nics"][0]["network"],
bnet3.network.backend_id)
self.assertEqual(kwargs["nics"][1]["network"], network.backend_id)
self.assertEqual(kwargs["nics"][1]["ip"], fp1.ipv4)
self.assertEqual(kwargs["nics"][2]["network"], network.backend_id)
self.assertEqual(kwargs["nics"][2]["ip"], fp2.ipv4)
self.assertEqual(kwargs["nics"][1]["network"], fp1.network.backend_id)
self.assertEqual(kwargs["nics"][1]["ip"], fp1.address)
self.assertEqual(kwargs["nics"][2]["network"], fp2.network.backend_id)
self.assertEqual(kwargs["nics"][2]["ip"], fp2.address)
self.assertEqual(kwargs["nics"][3]["network"],
bnet4.network.backend_id)
......@@ -510,10 +516,6 @@ class ServerCreateAPITest(ComputeAPITest):
"""Test if the create server call returns the expected response
if a valid request has been speficied."""
mrapi().CreateInstance.side_effect = GanetiApiError("..ganeti is down")
# Create public network and backend
network = mfactory.NetworkFactory(public=True)
backend = mfactory.BackendFactory()
mfactory.BackendNetworkFactory(network=network, backend=backend)
request = self.request
with mocked_quotaholder():
......
......@@ -234,14 +234,14 @@ def get_floating_ip(user_id, ipv4, for_update=False):
raise faults.ItemNotFound("Floating IP does not exist.")
def allocate_public_address(backend):
def allocate_public_address(backend, userid):
"""Get a public IP for any available network of a backend."""
# Guarantee exclusive access to backend, because accessing the IP pools of
# the backend networks may result in a deadlock with backend allocator
# which also checks that backend networks have a free IP.
backend = Backend.objects.select_for_update().get(id=backend.id)
public_networks = backend_public_networks(backend)
return get_free_ip(public_networks)
return get_free_ip(public_networks, userid)
def backend_public_networks(backend):
......@@ -255,16 +255,14 @@ def backend_public_networks(backend):
network__public=True,
network__deleted=False,
network__floating_ip_pool=False,
network__subnet__isnull=False,
network__drained=False)
return [b.network for b in bnets]
def get_free_ip(networks):
def get_free_ip(networks, userid):
for network in networks:
try:
address = get_network_free_address(network)
return network, address
return network.allocate_address(userid=userid)
except faults.OverLimit:
pass
msg = "Can not allocate public IP. Public networks are full."
......@@ -272,16 +270,13 @@ def get_free_ip(networks):
raise faults.OverLimit(msg)
def get_network_free_address(network):
def get_network_free_address(network, userid):
"""Reserve an IP address from the IP Pool of the network."""
pool = network.get_pool()
try:
address = pool.get()
return network.allocate_address(userid=userid)
except EmptyPool:
raise faults.OverLimit("Network %s is full." % network.backend_id)
pool.save()
return address
def get_vm_nic(vm, nic_id):
......
......@@ -531,22 +531,19 @@ class Network(models.Model):
if not backend_exists:
BackendNetwork.objects.create(backend=backend, network=self)
def get_pool(self, with_lock=True):
def get_pool(self, locked=True):
try:
subnet = self.subnets.get(ipversion=4, deleted=False)
except Subnet.DoesNotExist:
raise pools.EmptyPool
return subnet.get_pool(locked=locked)
def allocate_address(self, userid):
try:
pool = subnet.ip_pools.all()[0]
except IndexError:
pool = IPPoolTable.objects.create(available_map='',
reserved_map='',
size=0,
subnet=subnet)