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

Implement reconciliation for pending quotas

parent a4205872
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.core.management.base import BaseCommand
from optparse import make_option
from synnefo import quotas
class Command(BaseCommand):
help = "Reconcile quotas with Quotaholder"
output_transaction = True
option_list = BaseCommand.option_list + (
make_option("--fix", dest="fix",
action='store_true',
default=False,
help="Fix pending commissions"
),
)
def handle(self, *args, **options):
fix = options['fix']
accepted, rejected = quotas.resolve_pending_commissions()
if accepted:
self.stdout.write("Pending accepted commissions:\n %s\n" \
% list_to_string(accepted))
if rejected:
self.stdout.write("Pending rejected commissions:\n %s\n" \
% list_to_string(rejected))
if fix and (accepted or rejected):
self.stdout.write("Fixing pending commissions..\n")
quotas.accept_commissions(accepted)
quotas.reject_commissions(rejected)
def list_to_string(l):
return ",".join([str(x) for x in l])
......@@ -83,18 +83,17 @@ def uses_commission(func):
## FIXME: Wrap the following two functions inside transaction ?
def accept_commission(serials):
def accept_commission(serials, update_db=True):
"""Accept a list of pending commissions.
@param serials: List of QuotaHolderSerial objects
"""
# Update the DB if needed
log.critical("Serials are %s", serials)
for s in serials:
if s.pending:
s.accepted = True
s.save()
if update_db:
for s in serials:
if s.pending:
s.accepted = True
s.save()
with get_quota_holder() as qh:
qh.accept_commission(context={},
......@@ -102,17 +101,17 @@ def accept_commission(serials):
serials=[s.serial for s in serials])
def reject_commission(serials):
def reject_commission(serials, update_db=True):
"""Reject a list of pending commissions.
@param serials: List of QuotaHolderSerial objects
"""
# Update the DB if needed
for s in serials:
if s.pending:
s.rejected = True
s.save()
if update_db:
for s in serials:
if s.pending:
s.rejected = True
s.save()
with get_quota_holder() as qh:
qh.reject_commission(context={},
......@@ -192,3 +191,69 @@ def create_commission(user, resources, delete=False):
"ownerkey": "",
"name": "",
"provisions": provisions}
##
## Reconcile pending commissions
##
def accept_commissions(accepted):
with get_quota_holder() as qh:
qh.accept_commission(context={},
clientkey='cyclades',
serials=accepted)
def reject_commissions(rejected):
with get_quota_holder() as qh:
qh.reject_commission(context={},
clientkey='cyclades',
serials=rejected)
def fix_pending_commissions():
(accepted, rejected) = resolve_pending_commissions()
with get_quota_holder() as qh:
if accepted:
qh.accept_commission(context={},
clientkey='cyclades',
serials=accepted)
if rejected:
qh.reject_commission(context={},
clientkey='cyclades',
serials=rejected)
def resolve_pending_commissions():
"""Resolve quotaholder pending commissions.
Get pending commissions from the quotaholder and resolve them
to accepted and rejected, according to the state of the
QuotaHolderSerial DB table. A pending commission in the quotaholder
can exist in the QuotaHolderSerial table and be either accepted or
rejected, or can not exist in this table, so it is rejected.
"""
qh_pending = get_quotaholder_pending()
if not qh_pending:
return ([], [])
qh_pending.sort()
min_ = qh_pending[0]
serials = QuotaHolderSerial.objects.filter(serial__gte=min_, pending=False)
accepted = serials.filter(accepted=True).values_list('serial', flat=True)
accepted = filter(lambda x: x in qh_pending, accepted)
rejected = list(set(qh_pending) - set(accepted))
return (accepted, rejected)
def get_quotaholder_pending():
with get_quota_holder() as qh:
pending_serials = qh.get_pending_commissions(context={},
clientkey='cyclades')
return pending_serials
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