Commit 6d560c8c authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Make Floating IPs a quotable resource

Add 'cyclades.floating_ip' to quotable resources. Update helper
functions and tests to include this resource.
parent 02da2000
......@@ -91,6 +91,12 @@ cyclades_services = {
"service_type": "compute",
"service_origin": "cyclades_compute",
},
'floating_ip': {
"name": "cyclades.floating_ip",
"desc": "Number of Floating IP addresses",
"service_type": "compute",
"service_origin": "cyclades_compute",
},
},
},
......
......@@ -31,7 +31,8 @@ from django.utils import simplejson as json
from django.db import transaction
from snf_django.lib.api import faults
from synnefo.db.models import QuotaHolderSerial, VirtualMachine, Network
from synnefo.db.models import (QuotaHolderSerial, VirtualMachine, Network,
FloatingIP)
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_BASE_URL)
......@@ -50,7 +51,8 @@ RESOURCES = [
"cyclades.disk",
"cyclades.ram",
"cyclades.active_ram",
"cyclades.network.private"
"cyclades.network.private",
"cyclades.floating_ip",
]
......@@ -194,7 +196,8 @@ def render_overlimit_exception(e):
resource_name = {"vm": "Virtual Machine",
"cpu": "CPU",
"ram": "RAM",
"network.private": "Private Network"}
"network.private": "Private Network",
"floating_ip": "Floating IP address"}
details = json.loads(e.details)
data = details['overLimit']['data']
usage = data["usage"]
......@@ -291,6 +294,8 @@ def prepare_qh_resources(resource):
'cyclades.active_ram': 1048576 * flavor.ram}
elif isinstance(resource, Network):
return {"cyclades.network.private": 1}
elif isinstance(resource, FloatingIP):
return {"cyclades.floating_ip": 1}
else:
raise ValueError("Unknown Resource '%s'" % resource)
......
......@@ -96,6 +96,16 @@ class GetDBHoldingsTestCase(TestCase):
holdings = util.get_db_holdings()
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")
holdings = util.get_db_holdings()
self.assertEqual(holdings["user1"]["cyclades.floating_ip"], 2)
self.assertEqual(holdings["user2"]["cyclades.floating_ip"], 1)
self.assertEqual(holdings["user3"]["cyclades.floating_ip"], 1)
@patch("synnefo.quotas.get_quotaholder_pending")
class ResolvePendingTestCase(TestCase):
......
......@@ -33,7 +33,7 @@
from django.db.models import Sum, Count, Q
from synnefo.db.models import VirtualMachine, Network
from synnefo.db.models import VirtualMachine, Network, FloatingIP
from synnefo.quotas import Quotaholder, ASTAKOS_TOKEN
......@@ -43,10 +43,12 @@ 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)
if user is not None:
vms = vms.filter(userid=user)
networks = networks.filter(userid=user)
floating_ips = floating_ips.filter(userid=user)
# Get resources related with VMs
vm_resources = vms.values("userid").annotate(num=Count("id"),
......@@ -60,7 +62,7 @@ def get_db_holdings(user=None):
.annotate(active_ram=Sum("flavor__ram"),
active_cpu=Sum("flavor__cpu"))
for vm_res in vm_resources:
for vm_res in vm_resources.iterator():
user = vm_res['userid']
res = {"cyclades.vm": vm_res["num"],
"cyclades.cpu": vm_res["cpu"],
......@@ -68,7 +70,7 @@ def get_db_holdings(user=None):
"cyclades.ram": 1048576 * vm_res["ram"]}
holdings[user] = res
for vm_res in vm_active_resources:
for vm_res in vm_active_resources.iterator():
user = vm_res['userid']
holdings[user]["cyclades.active_cpu"] = vm_res["active_cpu"]
holdings[user]["cyclades.active_ram"] = 1048576 * vm_res["active_ram"]
......@@ -76,12 +78,18 @@ def get_db_holdings(user=None):
# Get resources related with networks
net_resources = networks.values("userid")\
.annotate(num=Count("id"))
for net_res in net_resources:
for net_res in net_resources.iterator():
user = net_res['userid']
if user not in holdings:
holdings[user] = {}
holdings.setdefault(user, {})
holdings[user]["cyclades.network.private"] = net_res["num"]
floating_ips_resources = floating_ips.values("userid")\
.annotate(num=Count("id"))
for floating_ip_res in floating_ips_resources.iterator():
user = floating_ip_res["userid"]
holdings.setdefault(user, {})
holdings[user]["cyclades.floating_ip"] = floating_ip_res["num"]
return holdings
......
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