Commit 13a5bff7 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Replace FloatingIP with IPAddress

This commit just replaces all references to FloatingIP objects with
references to IPAddress object, in order to be able to just load the
code. But the code is left unchanged, so all tests will fail.
parent 38997cfc
......@@ -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, NetworkInterface
from synnefo.db.models import Network, IPAddress, NetworkInterface
from logging import getLogger
......@@ -92,7 +92,7 @@ def list_floating_ips(request):
log.debug("list_floating_ips")
userid = request.user_uniq
floating_ips = FloatingIP.objects.filter(userid=userid).order_by("id")
floating_ips = IPAddress.objects.filter(userid=userid).order_by("id")
floating_ips = utils.filter_modified_since(request, objects=floating_ips)
floating_ips = map(ip_to_dict, floating_ips)
......@@ -109,10 +109,10 @@ def get_floating_ip(request, floating_ip_id):
"""Return information for a floating IP."""
userid = request.user_uniq
try:
floating_ip = FloatingIP.objects.get(id=floating_ip_id,
floating_ip = IPAddress.objects.get(id=floating_ip_id,
deleted=False,
userid=userid)
except FloatingIP.DoesNotExist:
except IPAddress.DoesNotExist:
raise faults.ItemNotFound("Floating IP '%s' does not exist" %
floating_ip_id)
request.serialization = "json"
......@@ -155,7 +155,7 @@ def allocate_floating_ip(request):
else:
# User specified an IP address. Check that it is not a used
# floating IP
if FloatingIP.objects.filter(network=network,
if IPAddress.objects.filter(network=network,
deleted=False,
ipv4=address).exists():
msg = "Floating IP '%s' is reserved" % address
......@@ -178,7 +178,7 @@ def allocate_floating_ip(request):
except NetworkInterface.DoesNotExist:
msg = "Address '%s' is already in use" % address
raise faults.Conflict(msg)
floating_ip = FloatingIP.objects.create(ipv4=address, network=network,
floating_ip = IPAddress.objects.create(ipv4=address, network=network,
userid=userid, machine=machine)
quotas.issue_and_accept_commission(floating_ip)
except:
......@@ -202,11 +202,11 @@ def release_floating_ip(request, floating_ip_id):
userid = request.user_uniq
log.info("release_floating_ip '%s'. User '%s'.", floating_ip_id, userid)
try:
floating_ip = FloatingIP.objects.select_for_update()\
floating_ip = IPAddress.objects.select_for_update()\
.get(id=floating_ip_id,
deleted=False,
userid=userid)
except FloatingIP.DoesNotExist:
except IPAddress.DoesNotExist:
raise faults.ItemNotFound("Floating IP '%s' does not exist" %
floating_ip_id)
......
......@@ -34,8 +34,8 @@
import json
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder
from synnefo.db.models import FloatingIP
from synnefo.db.models_factory import (FloatingIPFactory, NetworkFactory,
from synnefo.db.models import IPAddress
from synnefo.db.models_factory import (IPAddressFactory, NetworkFactory,
VirtualMachineFactory,
NetworkInterfaceFactory,
BackendNetworkFactory)
......@@ -52,19 +52,19 @@ URL = join_urls(compute_path, "os-floating-ips")
NETWORKS_URL = join_urls(compute_path, "networks")
SERVERS_URL = join_urls(compute_path, "servers")
FloatingIPPoolFactory = partial(NetworkFactory, public=True, deleted=False,
IPAddressPoolFactory = partial(NetworkFactory, public=True, deleted=False,
floating_ip_pool=True)
class FloatingIPAPITest(BaseAPITest):
class IPAddressAPITest(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 = FloatingIPFactory(userid="user1")
FloatingIPFactory(userid="user1", deleted=True)
ip = IPAddressFactory(userid="user1")
IPAddressFactory(userid="user1", deleted=True)
with mocked_quotaholder():
response = self.get(URL, "user1")
self.assertSuccess(response)
......@@ -75,7 +75,7 @@ class FloatingIPAPITest(BaseAPITest):
str(ip.network.id)})
def test_get_ip(self):
ip = FloatingIPFactory(userid="user1")
ip = IPAddressFactory(userid="user1")
with mocked_quotaholder():
response = self.get(URL + "/%s" % ip.id, "user1")
self.assertSuccess(response)
......@@ -86,26 +86,26 @@ class FloatingIPAPITest(BaseAPITest):
str(ip.network.id)})
def test_wrong_user(self):
ip = FloatingIPFactory(userid="user1")
ip = IPAddressFactory(userid="user1")
with mocked_quotaholder():
response = self.delete(URL + "/%s" % ip.id, "user2")
self.assertItemNotFound(response)
def test_deleted_ip(self):
ip = FloatingIPFactory(userid="user1", deleted=True)
ip = IPAddressFactory(userid="user1", deleted=True)
with mocked_quotaholder():
response = self.delete(URL + "/%s" % ip.id, "user1")
self.assertItemNotFound(response)
def test_reserve(self):
net = FloatingIPPoolFactory(userid="test_user",
net = IPAddressPoolFactory(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 = FloatingIP.objects.get()
ip = IPAddress.objects.get()
self.assertEqual(ip.ipv4, "192.168.2.1")
self.assertEqual(ip.machine, None)
self.assertEqual(ip.network, net)
......@@ -120,27 +120,27 @@ class FloatingIPAPITest(BaseAPITest):
response = self.post(URL, "test_user", json.dumps({}), "json")
self.assertFault(response, 413, "overLimit")
# Full network
FloatingIPPoolFactory(userid="test_user",
IPAddressPoolFactory(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 = FloatingIPPoolFactory(userid="test_user",
net2 = IPAddressPoolFactory(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 = FloatingIP.objects.get()
ip = IPAddress.objects.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 = FloatingIPPoolFactory(userid="test_user",
net = IPAddressPoolFactory(userid="test_user",
subnet="192.168.2.0/32")
request = {'pool': net.id}
with mocked_quotaholder():
......@@ -148,20 +148,20 @@ class FloatingIPAPITest(BaseAPITest):
self.assertEqual(response.status_code, 413)
def test_reserve_with_address(self):
net = FloatingIPPoolFactory(userid="test_user",
net = IPAddressPoolFactory(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 = FloatingIP.objects.get()
ip = IPAddress.objects.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
FloatingIPFactory(network=net, ipv4="192.168.2.3")
IPAddressFactory(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 +194,7 @@ class FloatingIPAPITest(BaseAPITest):
self.assertBadRequest(response)
def test_release_in_use(self):
ip = FloatingIPFactory()
ip = IPAddressFactory()
vm = ip.machine
vm.operstate = "ACTIVE"
vm.userid = ip.userid
......@@ -218,16 +218,16 @@ class FloatingIPAPITest(BaseAPITest):
self.assertFault(response, 409, "conflict")
def test_release(self):
ip = FloatingIPFactory(machine=None)
ip = IPAddressFactory(machine=None)
with mocked_quotaholder():
response = self.delete(URL + "/%s" % ip.id, ip.userid)
self.assertSuccess(response)
ips_after = FloatingIP.objects.filter(id=ip.id)
ips_after = IPAddress.objects.filter(id=ip.id)
self.assertEqual(len(ips_after), 0)
@patch("synnefo.logic.backend", Mock())
def test_delete_network_with_floating_ips(self):
ip = FloatingIPFactory(machine=None, network__flavor="IP_LESS_ROUTED")
ip = IPAddressFactory(machine=None, network__flavor="IP_LESS_ROUTED")
net = ip.network
# Can not remove network with floating IPs
with mocked_quotaholder():
......@@ -246,14 +246,14 @@ class FloatingIPAPITest(BaseAPITest):
POOLS_URL = join_urls(compute_path, "os-floating-ip-pools")
class FloatingIPPoolsAPITest(BaseAPITest):
class IPAddressPoolsAPITest(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 = FloatingIPPoolFactory(subnet="192.168.0.0/30",
net = IPAddressPoolFactory(subnet="192.168.0.0/30",
gateway="192.168.0.1")
NetworkFactory(public=True, deleted=True)
NetworkFactory(public=False, deleted=False)
......@@ -264,7 +264,7 @@ class FloatingIPPoolsAPITest(BaseAPITest):
[{"name": str(net.id), "size": 4, "free": 1}])
class FloatingIPActionsTest(BaseAPITest):
class IPAddressActionsTest(BaseAPITest):
def setUp(self):
vm = VirtualMachineFactory()
vm.operstate = "ACTIVE"
......@@ -289,21 +289,21 @@ class FloatingIPActionsTest(BaseAPITest):
self.assertItemNotFound(response)
# In use
vm1 = VirtualMachineFactory()
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=vm1)
ip1 = IPAddressFactory(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 = FloatingIPFactory(userid=self.vm.userid, machine=None)
ip1 = IPAddressFactory(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 = FloatingIP.objects.get(id=ip1.id)
ip1_after = IPAddress.objects.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 +323,18 @@ class FloatingIPActionsTest(BaseAPITest):
response = self.post(url, self.vm.userid, json.dumps(request), "json")
self.assertItemNotFound(response)
# Not In Use
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None)
ip1 = IPAddressFactory(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 = FloatingIPFactory(userid=self.vm.userid, machine=self.vm)
ip1 = IPAddressFactory(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 = FloatingIP.objects.get(id=ip1.id)
ip1_after = IPAddress.objects.get(id=ip1.id)
self.assertEqual(ip1_after.machine, self.vm)
self.assertTrue(ip1_after.in_use())
......@@ -37,7 +37,7 @@ from copy import deepcopy
from snf_django.utils.testing import (BaseAPITest, mocked_quotaholder,
override_settings)
from synnefo.db.models import (VirtualMachine, VirtualMachineMetadata,
FloatingIP)
IPAddress)
from synnefo.db import models_factory as mfactory
from synnefo.logic.utils import get_rsapi_state
from synnefo.cyclades_settings import cyclades_services
......@@ -468,10 +468,10 @@ class ServerCreateAPITest(ComputeAPITest):
mfactory.BackendNetworkFactory(network=network,
backend=self.backend,
operstate="ACTIVE")
fp1 = mfactory.FloatingIPFactory(ipv4="10.0.0.2",
fp1 = mfactory.IPAddressFactory(ipv4="10.0.0.2",
userid="test_user",
network=network, machine=None)
fp2 = mfactory.FloatingIPFactory(ipv4="10.0.0.3", network=network,
fp2 = mfactory.IPAddressFactory(ipv4="10.0.0.3", network=network,
userid="test_user",
machine=None)
request["server"]["floating_ips"] = [fp1.ipv4, fp2.ipv4]
......@@ -483,8 +483,8 @@ 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 = FloatingIP.objects.get(id=fp1.id)
fp2 = FloatingIP.objects.get(id=fp2.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)
name, args, kwargs = mrapi().CreateInstance.mock_calls[2]
......
......@@ -49,7 +49,7 @@ from snf_django.lib.api import faults
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata,
Network, BackendNetwork, NetworkInterface,
BridgePoolTable, MacPrefixPoolTable, Backend,
FloatingIP)
IPAddress)
from synnefo.db.pools import EmptyPool
from synnefo.plankton.utils import image_backend
......@@ -226,11 +226,11 @@ def get_network(network_id, user_id, for_update=False, non_deleted=False):
def get_floating_ip(user_id, ipv4, for_update=False):
try:
objects = FloatingIP.objects
objects = IPAddress.objects
if for_update:
objects = objects.select_for_update()
return objects.get(userid=user_id, ipv4=ipv4, deleted=False)
except FloatingIP.DoesNotExist:
except IPAddress.DoesNotExist:
raise faults.ItemNotFound("Floating IP does not exist.")
......
......@@ -35,9 +35,10 @@ from django.db import transaction
from datetime import datetime, timedelta
from synnefo.db.models import (Backend, VirtualMachine, Network,
IPAddress,
BackendNetwork, BACKEND_STATUSES,
pooled_rapi_client, VirtualMachineDiagnostic,
Flavor, FloatingIP)
Flavor)
from synnefo.logic import utils
from synnefo import quotas
from synnefo.api.util import release_resource
......@@ -339,9 +340,9 @@ def release_nic_address(nic):
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)
IPAddress.objects.filter(machine=nic.machine_id,
network=nic.network_id,
ipv4=nic.ipv4).update(machine=None)
else:
nic.network.release_address(nic.ipv4)
......
......@@ -31,7 +31,7 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from synnefo.db.models import FloatingIP
from synnefo.db.models import IPAddress
from snf_django.management.commands import ListCommand
from synnefo.settings import CYCLADES_SERVICE_TOKEN, ASTAKOS_BASE_URL
from logging import getLogger
......@@ -40,7 +40,7 @@ log = getLogger(__name__)
class Command(ListCommand):
help = "List Floating IPs"
object_class = FloatingIP
object_class = IPAddress
deleted_field = "deleted"
user_uuid_field = "userid"
astakos_url = ASTAKOS_BASE_URL
......
......@@ -13,7 +13,7 @@ from synnefo.api import util
from synnefo.logic import backend
from synnefo.logic.backend_allocator import BackendAllocator
from synnefo.db.models import (NetworkInterface, VirtualMachine, Network,
VirtualMachineMetadata, FloatingIP)
VirtualMachineMetadata, IPAddress)
from vncauthproxy.client import request_forwarding as request_vnc_forwarding
......@@ -441,7 +441,7 @@ def add_floating_ip(vm, address):
def add_floating_ip_to_vm(vm, address):
"""Get a floating IP by it's address and add it to VirtualMachine.
Helper function for looking up a FloatingIP by it's address and associating
Helper function for looking up a IPAddress by it's address and associating
it with a VirtualMachine object (without adding the NIC in the Ganeti
backend!). This function also checks if the floating IP is currently used
by any instance and if it is available in the Backend that hosts the VM.
......@@ -451,10 +451,10 @@ def add_floating_ip_to_vm(vm, address):
try:
# Get lock in VM, to guarantee that floating IP will only by assigned
# once
floating_ip = FloatingIP.objects.select_for_update()\
floating_ip = IPAddress.objects.select_for_update()\
.get(userid=user_id, ipv4=address,
deleted=False)
except FloatingIP.DoesNotExist:
except IPAddress.DoesNotExist:
raise faults.ItemNotFound("Floating IP '%s' does not exist" % address)
if floating_ip.in_use():
......@@ -476,10 +476,10 @@ def add_floating_ip_to_vm(vm, address):
def remove_floating_ip(vm, address):
user_id = vm.userid
try:
floating_ip = FloatingIP.objects.select_for_update()\
floating_ip = IPAddress.objects.select_for_update()\
.get(userid=user_id, ipv4=address,
deleted=False, machine=vm)
except FloatingIP.DoesNotExist:
except IPAddress.DoesNotExist:
raise faults.ItemNotFound("Floating IP '%s' does not exist" % address)
try:
......
......@@ -34,7 +34,7 @@ from random import randint
from django.test import TestCase
from synnefo.db.models import (VirtualMachine, FloatingIP, BackendNetwork,
from synnefo.db.models import (VirtualMachine, IPAddress, BackendNetwork,
Network, BridgePoolTable, MacPrefixPoolTable)
from synnefo.db import models_factory as mfactory
from synnefo.lib.utils import split_time
......@@ -151,8 +151,8 @@ class UpdateDBTest(TestCase):
self.assertTrue(nic.network.get_pool().is_available(nic.ipv4))
vm2 = mfactory.VirtualMachineFactory()
network = mfactory.NetworkFactory(floating_ip_pool=True)
fp1 = mfactory.FloatingIPFactory(machine=vm2, network=network)
fp2 = mfactory.FloatingIPFactory(machine=vm2, network=network)
fp1 = mfactory.IPAddressFactory(machine=vm2, network=network)
fp2 = mfactory.IPAddressFactory(machine=vm2, network=network)
mfactory.NetworkInterfaceFactory(machine=vm2, network=network,
ipv4=fp1.ipv4)
mfactory.NetworkInterfaceFactory(machine=vm2, network=network,
......@@ -169,8 +169,8 @@ class UpdateDBTest(TestCase):
db_vm = VirtualMachine.objects.get(id=vm.id)
self.assertEqual(db_vm.operstate, 'DESTROYED')
self.assertTrue(db_vm.deleted)
self.assertEqual(FloatingIP.objects.get(id=fp1.id).machine, None)
self.assertEqual(FloatingIP.objects.get(id=fp2.id).machine, None)
self.assertEqual(IPAddress.objects.get(id=fp1.id).machine, None)
self.assertEqual(IPAddress.objects.get(id=fp2.id).machine, None)
pool = network.get_pool()
# Test that floating ips are not released
self.assertFalse(pool.is_available(fp1.ipv4))
......
......@@ -32,7 +32,7 @@ from django.db import transaction
from snf_django.lib.api import faults
from synnefo.db.models import (QuotaHolderSerial, VirtualMachine, Network,
FloatingIP)
IPAddress)
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_BASE_URL)
......@@ -297,7 +297,7 @@ def prepare_qh_resources(resource):
'cyclades.active_ram': 1048576 * flavor.ram}
elif isinstance(resource, Network):
return {"cyclades.network.private": 1}
elif isinstance(resource, FloatingIP):
elif isinstance(resource, IPAddress):
return {"cyclades.floating_ip": 1}
else:
raise ValueError("Unknown Resource '%s'" % resource)
......
......@@ -97,10 +97,10 @@ class GetDBHoldingsTestCase(TestCase):
self.assertEqual(holdings["user2"], user_holdings["user2"])
def test_floating_ip_holdings(self):
mfactory.FloatingIPFactory(userid="user1")
mfactory.FloatingIPFactory(userid="user1")
mfactory.FloatingIPFactory(userid="user2")
mfactory.FloatingIPFactory(userid="user3")
mfactory.IPAddressFactory(userid="user1")
mfactory.IPAddressFactory(userid="user1")
mfactory.IPAddressFactory(userid="user2")
mfactory.IPAddressFactory(userid="user3")
holdings = util.get_db_holdings()
self.assertEqual(holdings["user1"]["cyclades.floating_ip"], 2)
self.assertEqual(holdings["user2"]["cyclades.floating_ip"], 1)
......
......@@ -33,7 +33,7 @@
from django.db.models import Sum, Count, Q
from synnefo.db.models import VirtualMachine, Network, FloatingIP
from synnefo.db.models import VirtualMachine, Network, IPAddress
from synnefo.quotas import Quotaholder, ASTAKOS_TOKEN
......@@ -43,7 +43,7 @@ def get_db_holdings(user=None):
vms = VirtualMachine.objects.filter(deleted=False)
networks = Network.objects.filter(deleted=False)
floating_ips = FloatingIP.objects.filter(deleted=False)
floating_ips = IPAddress.objects.filter(deleted=False)
if user is not None:
vms = vms.filter(userid=user)
......
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