Revision e496f888 snf-astakos-app/astakos/im/endpoints/qh.py

b/snf-astakos-app/astakos/im/endpoints/qh.py
76 76
    return result
77 77

  
78 78

  
79
def get_holding(payload):
80
    c = get_client()
81
    if not c:
82
        return
83
    if payload == []:
84
        return []
85
    result = c.get_holding(context={}, get_holding=payload)
86
    logger.debug('get_holding: %s reply: %s' % (payload, result))
87
    return result
88

  
89

  
90
def qh_get_holdings(users, resources):
91
    payload = []
92
    append = payload.append
93
    for user in users:
94
        for resource in resources:
95
            append((user.uuid, resource),)
96
    result = get_holding(payload)
97
    return result
98

  
99

  
100
def quota_limits_per_user_from_get(lst):
101
    quotas = {}
102
    for holder, resource, q, c, imp, exp, ret, rel, flags in lst:
103
        userquotas = quotas.get(holder, {})
104
        userquotas[resource] = QuotaValues(quantity=q, capacity=c,
105
                                           )
106
        quotas[holder] = userquotas
107
    return quotas
108

  
109

  
110 79
def quotas_per_user_from_get(lst):
111 80
    limits = {}
112
    counters = {}
113
    for holder, resource, q, c, imp, exp, ret, rel, flags in lst:
81
    usage = {}
82
    for holder, resource, c, imp_min, imp_max, st_min, st_max, flags in lst:
114 83
        userlimits = limits.get(holder, {})
115
        userlimits[resource] = QuotaValues(quantity=q, capacity=c,
116
                                           )
84
        userlimits[resource] = c
117 85
        limits[holder] = userlimits
118 86

  
119
        usercounters = counters.get(holder, {})
120
        usercounters[resource] = QuotaCounters(imported=imp, exported=exp,
121
                                               returned=ret, released=rel)
122
        counters[holder] = usercounters
123
    return limits, counters
87
        user_usage = usage.get(holder, {})
88
        user_usage[resource] = imp_max
89
        usage[holder] = user_usage
90
    return limits, usage
124 91

  
125 92

  
126 93
def qh_get_quota(users, resources):
......
131 98
    append = payload.append
132 99
    for user in users:
133 100
        for resource in resources:
134
            append((user.uuid, resource),)
101
            append((user.uuid, str(resource)),)
135 102

  
136 103
    if payload == []:
137 104
        return []
......
140 107
    return result
141 108

  
142 109

  
143
def qh_get_quota_limits(users, resources):
144
    result = qh_get_quota(users, resources)
145
    return quota_limits_per_user_from_get(result)
146

  
147

  
148 110
def qh_get_quotas(users, resources):
149 111
    result = qh_get_quota(users, resources)
150 112
    return quotas_per_user_from_get(result)
......
152 114

  
153 115
SetQuotaPayload = namedtuple('SetQuotaPayload', ('holder',
154 116
                                                 'resource',
155
                                                 'quantity',
156 117
                                                 'capacity',
157 118
                                                 'flags'))
158 119

  
......
161 122
                                         'capacity',
162 123
                                         ))
163 124

  
164
QuotaCounters = namedtuple('QuotaCounters', ('imported',
165
                                             'exported',
166
                                             'returned',
167
                                             'released'))
168

  
169

  
170
class QuotaValues(namedtuple('QuotaValues', ('quantity',
171
                                             'capacity',
172
                                             ))):
173
    __slots__ = ()
174

  
175
    def __dir__(self):
176
            return ['quantity', 'capacity']
177

  
178
    def __str__(self):
179
        return '\t'.join(['%s=%s' % (f, strbigdec(getattr(self, f)))
180
                          for f in dir(self)])
181

  
182

  
183
def add_quota_values(q1, q2):
184
    return QuotaValues(
185
        quantity = q1.quantity + q2.quantity,
186
        capacity = q1.capacity + q2.capacity,
187
        )
188

  
189 125

  
190 126
def register_quotas(quotas):
191 127
    if not quotas:
......
194 130
    payload = []
195 131
    append = payload.append
196 132
    for uuid, userquotas in quotas.iteritems():
197
        for resource, q in userquotas.iteritems():
133
        for resource, capacity in userquotas.iteritems():
198 134
            append(SetQuotaPayload(
199 135
                    holder=uuid,
200
                    resource=resource,
201
                    quantity=q.quantity,
202
                    capacity=q.capacity,
136
                    resource=str(resource),
137
                    capacity=capacity,
203 138
                    flags=0))
204 139
    return set_quota(payload)
205 140

  
......
214 149
        for resource, q in quotas.iteritems():
215 150
            append(SetQuotaPayload(
216 151
                    holder=holder,
217
                    resource=resource,
218
                    quantity=q.quantity,
152
                    resource=str(resource),
219 153
                    capacity=q.capacity,
220 154
                    flags=0))
221 155
    return set_quota(payload)
222 156

  
223 157

  
158
def initial_commission(resources):
159
    c = get_client()
160
    if not c:
161
        return
162

  
163
    for resource in resources:
164
        s = c.issue_commission(
165
            clientkey=clientkey,
166
            target=str(resource.service),
167
            name='initialization',
168
            provisions=[(None, str(resource), QH_PRACTICALLY_INFINITE)])
169

  
170
        c.accept_commission(clientkey=clientkey, serials=[s], reason='')
171

  
172

  
224 173
def register_resources(resources):
225 174

  
226 175
    payload = list(SetQuotaPayload(
227
            holder=resource.service,
228
            resource=resource,
229
            quantity=QH_PRACTICALLY_INFINITE,
176
            holder=str(resource.service),
177
            resource=str(resource),
230 178
            capacity=QH_PRACTICALLY_INFINITE,
231 179
            flags=0) for resource in resources)
232
    return set_quota(payload)
180
    set_quota(payload)
181
    initial_commission(resources)
233 182

  
234 183

  
235 184
def qh_add_quota(sub_list, add_list):
236 185
    context = {}
237 186
    c = get_client()
238 187

  
239
    sub_quota = []
240
    sub_append = sub_quota.append
241
    add_quota = []
242
    add_append = add_quota.append
243

  
244
    for ql in sub_list:
245
        args = (ql.holder, ql.resource,
246
                0, ql.capacity)
247
        sub_append(args)
248

  
249
    for ql in add_list:
250
        args = (ql.holder, ql.resource,
251
                0, ql.capacity)
252
        add_append(args)
253

  
254 188
    result = c.add_quota(context=context,
255
                         sub_quota=sub_quota,
256
                         add_quota=add_quota)
189
                         sub_quota=sub_list,
190
                         add_quota=add_list)
257 191

  
258 192
    return result
259 193

  

Also available in: Unified diff