Commit 59ce6ec5 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Various fixes to quotas

parent 1f56b2c5
......@@ -28,7 +28,7 @@
# policies, either expressed or implied, of GRNET S.A.
from functools import wraps
from contextlib import contextmanager
from django.utils import simplejson as json
from snf_django.lib.api import faults
from synnefo.db.models import QuotaHolderSerial
......@@ -43,6 +43,13 @@ import logging
log = logging.getLogger(__name__)
DEFAULT_SOURCE = 'system'
RESOURCES = [
"cyclades.vm",
"cyclades.cpu",
"cyclades.disk",
"cyclades.ram",
"cyclades.network.private"
]
class Quotaholder(object):
......@@ -163,32 +170,34 @@ def issue_commission(user, source, provisions,
def issue_vm_commission(user, flavor, delete=False):
resources = prepare(get_server_resources(flavor), delete)
resources = get_server_resources(flavor)
if delete:
resources = reverse_quantities(resources)
return issue_commission(user, DEFAULT_SOURCE, resources)
def get_server_resources(flavor):
return {'vm': 1,
'cpu': flavor.cpu,
'disk': 1073741824 * flavor.disk, # flavor.disk is in GB
return {'cyclades.vm': 1,
'cyclades.cpu': flavor.cpu,
'cyclades.disk': 1073741824 * flavor.disk, # flavor.disk is in GB
# 'public_ip': 1,
#'disk_template': flavor.disk_template,
'ram': 1048576 * flavor.ram} # flavor.ram is in MB
'cyclades.ram': 1048576 * flavor.ram} # flavor.ram is in MB
def issue_network_commission(user, delete=False):
resources = prepare(get_network_resources(), delete)
resources = get_network_resources()
if delete:
resources = reverse_quantities(resources)
return issue_commission(user, DEFAULT_SOURCE, resources)
def get_network_resources():
return {"network.private": 1}
return {"cyclades.network.private": 1}
def prepare(resources_dict, delete):
if delete:
return dict((r, -s) for r, s in resources_dict.items())
return resources_dict
def reverse_quantities(resources):
return dict((r, -s) for r, s in resources.items())
##
......@@ -257,9 +266,11 @@ def render_overlimit_exception(e):
"cpu": "CPU",
"ram": "RAM",
"network.private": "Private Network"}
details = e.details
details = json.loads(e.details)
data = details['overLimit']['data']
available = data['available']
usage = data["usage"]
limit = data["limit"]
available = limit - usage
provision = data['provision']
requested = provision['quantity']
resource = provision['resource']
......
......@@ -46,8 +46,7 @@ from synnefo.quotas import util
class GetDBHoldingsTestCase(TestCase):
def test_no_holdings(self):
users = ["dummy_user1", "dummy_user2"]
holdings = util.get_db_holdings(users=users)
holdings = util.get_db_holdings(user=None)
self.assertEqual(holdings, {})
def test_vm_holdings(self):
......@@ -55,11 +54,11 @@ class GetDBHoldingsTestCase(TestCase):
disk_template='drbd')
mfactory.VirtualMachineFactory()
mfactory.VirtualMachineFactory(flavor=flavor, userid="user1")
user_holdings = {"user1": {"vm": 1,
"cpu": 24,
"disk": 21474836480,
"ram": 8589934592}}
holdings = util.get_db_holdings(users=["user1"])
user_holdings = {"user1": {"cyclades.vm": 1,
"cyclades.cpu": 24,
"cyclades.disk": 21474836480,
"cyclades.ram": 8589934592}}
holdings = util.get_db_holdings(user="user1")
self.assertEqual(holdings, user_holdings)
holdings = util.get_db_holdings()
self.assertEqual(holdings["user1"], user_holdings["user1"])
......@@ -67,8 +66,8 @@ class GetDBHoldingsTestCase(TestCase):
def test_network_holdings(self):
mfactory.NetworkFactory(userid="user1")
mfactory.NetworkFactory(userid="user2")
user_holdings = {"user2": {"network.private": 1}}
holdings = util.get_db_holdings(users=["user2"])
user_holdings = {"user2": {"cyclades.network.private": 1}}
holdings = util.get_db_holdings(user="user2")
self.assertEqual(holdings, user_holdings)
holdings = util.get_db_holdings()
self.assertEqual(holdings["user2"], user_holdings["user2"])
......
......@@ -37,17 +37,16 @@ from synnefo.db.models import VirtualMachine, Network
from synnefo.quotas import Quotaholder, ASTAKOS_TOKEN
def get_db_holdings(users=None):
def get_db_holdings(user=None):
"""Get holdings from Cyclades DB."""
holdings = {}
vms = VirtualMachine.objects.filter(deleted=False)
networks = Network.objects.filter(deleted=False)
if users:
assert(type(users) is list)
vms = vms.filter(userid__in=users)
networks = networks.filter(userid__in=users)
if user is not None:
vms = vms.filter(userid=user)
networks = networks.filter(userid=user)
# Get resources related with VMs
vm_resources = vms.values("userid").annotate(num=Count("id"),
......@@ -56,10 +55,10 @@ def get_db_holdings(users=None):
disk=Sum("flavor__disk"))
for vm_res in vm_resources:
user = vm_res['userid']
res = {"vm": vm_res["num"],
"cpu": vm_res["cpu"],
"disk": 1073741824 * vm_res["disk"],
"ram": 1048576 * vm_res["ram"]}
res = {"cyclades.vm": vm_res["num"],
"cyclades.cpu": vm_res["cpu"],
"cyclades.disk": 1073741824 * vm_res["disk"],
"cyclades.ram": 1048576 * vm_res["ram"]}
holdings[user] = res
# Get resources related with networks
......@@ -69,7 +68,7 @@ def get_db_holdings(users=None):
user = net_res['userid']
if user not in holdings:
holdings[user] = {}
holdings[user]["network.private"] = net_res["num"]
holdings[user]["cyclades.network.private"] = net_res["num"]
return holdings
......@@ -80,16 +79,14 @@ def get_quotaholder_holdings(user=None):
Returns quotas for all users, unless a single user is specified.
"""
qh = Quotaholder.get()
return qh.get_service_quotas(ASTAKOS_TOKEN, user)
return qh.service_get_quotas(ASTAKOS_TOKEN, user)
def transform_quotas(quotas):
d = {}
for resource, counters in quotas.iteritems():
res = resource.replace("cyclades.", "")
available = counters['available']
used = counters['usage']
limit = counters['limit']
used = counters['used']
used_max = limit - available
d[res] = (used, used_max)
pending = counters['pending']
d[resource] = (used, limit, pending)
return d
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