Revision 2ff02341 snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py

b/snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from django.core.management.base import NoArgsCommand, CommandError
34
from django.core.management.base import NoArgsCommand
35 35

  
36 36
from optparse import make_option
37 37

  
38 38
from pithos.api.util import get_backend
39 39
from pithos.api.resources import resources
40
from pithos.backends.modular import CLUSTER_NORMAL, DEFAULT_SOURCE
40
from pithos.backends.modular import DEFAULT_SOURCE
41 41

  
42 42
from synnefo.webproject.management import utils
43 43

  
44
from astakosclient.errors import QuotaLimit
44
from astakosclient.errors import QuotaLimit, NotFound
45 45

  
46 46
backend = get_backend()
47 47

  
......
70 70

  
71 71
    def handle_noargs(self, **options):
72 72
        try:
73
            qh_result = backend.astakosclient.service_get_quotas(
74
                backend.service_token)
73
            userid = options['userid']
75 74

  
76
            users = (options['userid'],) if options['userid'] else None
77
            account_nodes = backend.node.node_accounts(users)
78
            if not account_nodes:
79
                raise CommandError('No users found.')
75
            # Get holding from Pithos DB
76
            db_usage = backend.node.node_account_usage(userid)
80 77

  
81
            db_usage = {}
82
            for path, node in account_nodes:
83
                size = backend.node.node_account_usage(node, CLUSTER_NORMAL)
84
                db_usage[path] = size or 0
78
            users = set(db_usage.keys())
79
            if userid and userid not in users:
80
                self.stdout.write("User '%s' does not exist in DB!\n" % userid)
81
                return
85 82

  
86
            users = set(qh_result.keys())
87
            users.update(db_usage.keys())
83
            # Get holding from Quotaholder
84
            try:
85
                qh_result = backend.astakosclient.service_get_quotas(
86
                    backend.service_token, userid)
87
            except NotFound:
88
                self.stdout.write(
89
                    "User '%s' does not exist in Quotaholder!\n" % userid)
90
                return
91

  
92
            users.update(qh_result.keys())
88 93

  
89 94
            pending_exists = False
90 95
            unknown_user_exists = False
......
95 100
                    qh_all = qh_result[uuid]
96 101
                except KeyError:
97 102
                    self.stdout.write(
98
                        "User '%s' does not exist in Quotaholder!\n" % uuid
99
                    )
103
                        "User '%s' does not exist in Quotaholder!\n" % uuid)
100 104
                    unknown_user_exists = True
101 105
                    continue
102 106
                else:
......
114 118
                            self.stdout.write(
115 119
                                "Pending commission. "
116 120
                                "User '%s', resource '%s'.\n" %
117
                                (uuid, resource)
118
                            )
121
                                (uuid, resource))
119 122
                            pending_exists = True
120 123
                            continue
121 124

  
......
146 149
            if pending_exists:
147 150
                self.stdout.write(
148 151
                    "Found pending commissions. Run 'snf-manage"
149
                    " reconcile-commissions-pithos'\n"
150
                )
152
                    " reconcile-commissions-pithos'\n")
151 153
            elif not (unsynced or unknown_user_exists):
152 154
                self.stdout.write("Everything in sync.\n")
153 155
        finally:

Also available in: Unified diff