From f12f72b14593e5c72c58df0fa2549a943b95118a Mon Sep 17 00:00:00 2001
From: Giorgos Korfiatis <gkorf@grnet.gr>
Date: Thu, 16 May 2013 15:44:24 +0300
Subject: [PATCH] Catch QuotaLimit exception when reconciling resources

This can happen, if the actual usage registered by a service exceeds
the quota limit.
Advise the user to try the --force option.
---
 .../commands/reconcile-resources-astakos.py        | 11 +++++++++--
 .../commands/reconcile-resources-cyclades.py       |  7 ++++++-
 .../commands/reconcile-resources-pithos.py         | 14 +++++++++++---
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/snf-astakos-app/astakos/im/management/commands/reconcile-resources-astakos.py b/snf-astakos-app/astakos/im/management/commands/reconcile-resources-astakos.py
index 9ed1b85ca..c6e0567df 100644
--- a/snf-astakos-app/astakos/im/management/commands/reconcile-resources-astakos.py
+++ b/snf-astakos-app/astakos/im/management/commands/reconcile-resources-astakos.py
@@ -41,6 +41,7 @@ from astakos.im.models import Service, AstakosUser
 from astakos.im.quotas import service_get_quotas, SYSTEM
 from astakos.im.functions import count_pending_app
 import astakos.quotaholder_app.callpoint as qh
+import astakos.quotaholder_app.exception as qh_exception
 
 
 class Command(BaseCommand):
@@ -129,8 +130,14 @@ class Command(BaseCommand):
             pprint_table(self.stderr, unsynced, headers)
             if options["fix"]:
                 provisions = map(create_provision, unsynced)
-                s = qh.issue_commission('astakos', provisions,
-                                        name='RECONCILE', force=force)
+                try:
+                    s = qh.issue_commission('astakos', provisions,
+                                            name='RECONCILE', force=force)
+                except qh_exception.NoCapacityError:
+                    write("Reconciling failed because a limit has been "
+                          "reached. Use --force to ignore the check.\n")
+                    return
+
                 qh.resolve_pending_commission('astakos', s)
                 write("Fixed unsynced resources\n")
 
diff --git a/snf-cyclades-app/synnefo/quotas/management/commands/reconcile-resources-cyclades.py b/snf-cyclades-app/synnefo/quotas/management/commands/reconcile-resources-cyclades.py
index f8e1c4217..652d7dc2b 100644
--- a/snf-cyclades-app/synnefo/quotas/management/commands/reconcile-resources-cyclades.py
+++ b/snf-cyclades-app/synnefo/quotas/management/commands/reconcile-resources-cyclades.py
@@ -125,7 +125,12 @@ class Command(BaseCommand):
                 request["auto_accept"] = True
                 request["name"] = "RECONCILE"
                 request["provisions"] = map(create_provision, unsynced)
-                qh.issue_commission(ASTAKOS_TOKEN, request)
+                try:
+                    qh.issue_commission(ASTAKOS_TOKEN, request)
+                except quotas.QuotaLimit:
+                    write("Reconciling failed because a limit has been "
+                          "reached. Use --force to ignore the check.\n")
+                    return
                 write("Fixed unsynced resources\n")
 
         if pending_exists:
diff --git a/snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py b/snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py
index d9054bcaa..909a452fa 100644
--- a/snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py
+++ b/snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py
@@ -38,6 +38,7 @@ from optparse import make_option
 from pithos.api.util import get_backend
 from pithos.backends.modular import CLUSTER_NORMAL, DEFAULT_SOURCE
 from synnefo.webproject.management import utils
+from astakosclient.errors import QuotaLimit
 
 backend = get_backend()
 
@@ -128,9 +129,16 @@ class Command(NoArgsCommand):
                     request['auto_accept'] = True
                     request['name'] = "RECONCILE"
                     request['provisions'] = map(create_provision, unsynced)
-                    backend.astakosclient.issue_commission(
-                        backend.service_token, request
-                    )
+                    try:
+                        backend.astakosclient.issue_commission(
+                            backend.service_token, request
+                            )
+                    except QuotaLimit:
+                        self.stdout.write(
+                            "Reconciling failed because a limit has been "
+                            "reached. Use --force to ignore the check.\n")
+                        return
+
 
             if pending_exists:
                 self.stdout.write(
-- 
GitLab