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

Add USE_QUOTAHOLDER setting

Add setting indicating whether to use the Quotaholder service or
use the static limits.
parent a0732089
......@@ -104,6 +104,9 @@ BACKEND_PER_USER = {}
# Quota
#
# Set to True to use the Quotaholder service. Otherwise, static
# limits are used
USE_QUOTAHOLDER = False
# Maximum number of VMs a user is allowed to have.
MAX_VMS_PER_USER = 3
......
......@@ -30,8 +30,11 @@
from functools import wraps
from contextlib import contextmanager
from synnefo.settings import QUOTAHOLDER_URL
from synnefo.db.models import QuotaHolderSerial
from synnefo.settings import (QUOTAHOLDER_URL, USE_QUOTAHOLDER,
VMS_USER_QUOTA, MAX_VMS_PER_USER,
NETWORKS_USER_QUOTA, MAX_NETWORKS_PER_USER)
from synnefo.db.models import QuotaHolderSerial, VirtualMachine, Network
from synnefo.api.faults import OverLimit
from kamaki.clients.quotaholder import QuotaholderClient
......@@ -42,10 +45,57 @@ import logging
log = logging.getLogger(__name__)
class DummySerial(QuotaHolderSerial):
accepted = True
rejected = True
pending = True
id = None
def save(*args, **kwargs):
pass
class DummyQuotaholderClient(object):
def issue_commission(self, **commission_info):
provisions = commission_info["provisions"]
userid = commission_info["target"]
for provision in provisions:
entity, resource, size = provision
if resource == "cyclades.vm":
user_vms = VirtualMachine.objects.filter(userid=userid,
deleted=False).count()
user_vm_limit = VMS_USER_QUOTA.get(userid, MAX_VMS_PER_USER)
log.warning("Users VMs %s User Limits %s", user_vms,
user_vm_limit)
if user_vms + size >= user_vm_limit:
raise NoQuantityError()
if resource == "cyclades.network.private":
user_networks = Network.objects.filter(userid=userid,
deleted=False).count()
user_network_limit = NETWORKS_USER_QUOTA.get(userid,
MAX_NETWORKS_PER_USER)
if user_networks + size >= user_network_limit:
raise NoQuantityError()
return None
def accept_commission(self, *args, **kwargs):
pass
def reject_commission(self, *args, **kwargs):
pass
def get_pending_commissions(self, *args, **kwargs):
return []
@contextmanager
def get_quota_holder():
"""Context manager for using a QuotaHolder."""
quotaholder = QuotaholderClient(QUOTAHOLDER_URL)
if USE_QUOTAHOLDER:
quotaholder = QuotaholderClient(QUOTAHOLDER_URL)
else:
quotaholder = DummyQuotaholderClient()
try:
yield quotaholder
......@@ -75,6 +125,7 @@ def uses_commission(func):
return ret
except CallError:
log.exception("Unexpected error")
raise
except:
if serials:
reject_commission(serials=serials)
......@@ -136,8 +187,12 @@ def issue_commission(**commission_info):
if e.call_error in ["NoCapacityError", "NoQuantityError"]:
raise OverLimit("Limit exceeded for your account")
db_serial = QuotaHolderSerial.objects.create(serial=serial)
return db_serial
if serial:
return QuotaHolderSerial.objects.create(serial=serial)
elif not USE_QUOTAHOLDER:
return DummySerial()
else:
raise Exception("No serial")
# Wrapper functions for issuing commissions for each resource type. Each
......@@ -182,7 +237,6 @@ def create_commission(user, resources, delete=False):
resources = invert_resources(resources)
provisions = [('cyclades', 'cyclades.' + r, s)
for r, s in resources.items()]
log.debug("Provisions are %s", provisions)
return {"context": {},
"target": user,
"key": "1",
......
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