Revision bab17726

b/snf-pithos-app/pithos/api/management/commands/reconcile-resources-pithos.py
36 36
from optparse import make_option
37 37

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

  
40 42
from synnefo.webproject.management import utils
43

  
41 44
from astakosclient.errors import QuotaLimit
42 45

  
43 46
backend = get_backend()
......
61 64
        make_option("--force",
62 65
                    default=False,
63 66
                    action="store_true",
64
                    help="Override Astakos quotas. Force Astakos to impose"
65
                         " the Pithos quota, independently of their value.")
67
                    help="Override Astakos quotas. Force Astakos to impose "
68
                         "the Pithos quota, independently of their value.")
66 69
    )
67 70

  
68 71
    def handle_noargs(self, **options):
69 72
        try:
73
            qh_result = backend.astakosclient.service_get_quotas(
74
                backend.service_token)
75

  
70 76
            users = (options['userid'],) if options['userid'] else None
71 77
            account_nodes = backend.node.node_accounts(users)
72 78
            if not account_nodes:
......
77 83
                size = backend.node.node_account_usage(node, CLUSTER_NORMAL)
78 84
                db_usage[path] = size or 0
79 85

  
80
            qh_result = backend.astakosclient.service_get_quotas(
81
                backend.service_token,
82
                users
83
            )
86
            users = set(qh_result.keys())
87
            users.update(db_usage.keys())
84 88

  
85
            resource = 'pithos.diskspace'
86 89
            pending_exists = False
87 90
            unknown_user_exists = False
88 91
            unsynced = []
89
            for uuid in db_usage.keys():
90
                db_value = db_usage[uuid]
92
            for uuid in users:
93
                db_value = db_usage.get(uuid, 0)
91 94
                try:
92 95
                    qh_all = qh_result[uuid]
93 96
                except KeyError:
......
98 101
                    continue
99 102
                else:
100 103
                    qh = qh_all.get(DEFAULT_SOURCE, {})
101
                    try:
102
                        qh_resource = qh[resource]
103
                    except KeyError:
104
                        self.stdout.write(
105
                            "Resource '%s' does not exist in Quotaholder"
106
                            " for user '%s'!\n" % (resource, uuid))
107
                        continue
108

  
109
                    if qh_resource['pending']:
110
                        self.stdout.write(
111
                            "Pending commission. User '%s', resource '%s'.\n" %
112
                            (uuid, resource)
113
                        )
114
                        pending_exists = True
115
                        continue
104
                    for resource in [r['name'] for r in resources]:
105
                        try:
106
                            qh_resource = qh[resource]
107
                        except KeyError:
108
                            self.stdout.write(
109
                                "Resource '%s' does not exist in Quotaholder "
110
                                "for user '%s'!\n" % (resource, uuid))
111
                            continue
112

  
113
                        if qh_resource['pending']:
114
                            self.stdout.write(
115
                                "Pending commission. "
116
                                "User '%s', resource '%s'.\n" %
117
                                (uuid, resource)
118
                            )
119
                            pending_exists = True
120
                            continue
116 121

  
117
                    qh_value = qh_resource['usage']
122
                        qh_value = qh_resource['usage']
118 123

  
119
                    if  db_value != qh_value:
120
                        data = (uuid, resource, db_value, qh_value)
121
                        unsynced.append(data)
124
                        if  db_value != qh_value:
125
                            data = (uuid, resource, db_value, qh_value)
126
                            unsynced.append(data)
122 127

  
123 128
            if unsynced:
124 129
                headers = ("User", "Resource", "Database", "Quotaholder")
......
131 136
                    request['provisions'] = map(create_provision, unsynced)
132 137
                    try:
133 138
                        backend.astakosclient.issue_commission(
134
                            backend.service_token, request
135
                            )
139
                            backend.service_token, request)
136 140
                    except QuotaLimit:
137 141
                        self.stdout.write(
138 142
                            "Reconciling failed because a limit has been "
139 143
                            "reached. Use --force to ignore the check.\n")
140 144
                        return
141 145

  
142

  
143 146
            if pending_exists:
144 147
                self.stdout.write(
145 148
                    "Found pending commissions. Run 'snf-manage"

Also available in: Unified diff