Revision e496f888

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

  
b/snf-astakos-app/astakos/im/management/commands/astakos-quota.py
139 139
            h_initial = astakos_initial[holder]
140 140
            email = info[holder]
141 141
            for resource, limits in resources.iteritems():
142
                initials = h_initial[resource]
143
                initial = str(initials.capacity)
144
                capacity = str(limits.capacity)
145
                c = h_counters[resource]
146
                used = str(c.imported - c.exported + c.returned - c.released)
142
                initial = str(h_initial[resource])
143
                capacity = str(limits)
144
                used = str(h_counters[resource])
147 145

  
148 146
                fields = holder, email, resource, initial, capacity, used
149 147
                output = []
b/snf-astakos-app/astakos/im/management/commands/user-set-initial-quota.py
46 46

  
47 47
AddResourceArgs = namedtuple('AddQuotaArgs', ('resource',
48 48
                                              'capacity',
49
                                              'quantity',
50 49
                                              ))
51 50

  
52 51
class Command(BaseCommand):
......
56 55
    The file must contain non-empty lines, and each line must
57 56
    contain a single-space-separated list of values:
58 57

  
59
    <user> <resource name> <capacity> <quantity>
58
    <user> <resource name> <capacity>
60 59

  
61 60
    For example to grant the following user with 10 private networks
62 61
    (independent of any he receives from projects):
63 62

  
64
    6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.network.private 10 0
63
    6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.network.private 10
65 64

  
66
    When setting quota from the command line, specify only capacity.
67
    Quantity and import/export limit will get default values. Example:
65
    Similar syntax is used when setting quota from the command line:
68 66

  
69 67
    --set-capacity 6119a50b-cbc7-42c0-bafc-4b6570e3f6ac cyclades.vm 10
70 68

  
......
134 132

  
135 133
        args = AddResourceArgs(resource=resource,
136 134
                               capacity=capacity,
137
                               quantity=0,
138 135
                               )
139 136

  
140 137
        try:
b/snf-astakos-app/astakos/im/management/commands/user-show.py
67 67

  
68 68
        for user in users:
69 69
            quotas = user.all_quotas()
70
            showable_quotas = {}
71
            for resource, limits in quotas.iteritems():
72
                showable_quotas[resource] = limits.capacity
73 70

  
74 71
            settings_dict = {}
75 72
            settings = user.settings()
......
107 104
            if settings_dict:
108 105
                kv['settings'] = settings_dict
109 106

  
110
            kv['resources'] = showable_quotas
107
            kv['resources'] = quotas
111 108

  
112 109
            if get_latest_terms():
113 110
                has_signed_terms = user.signed_terms
b/snf-astakos-app/astakos/im/models.py
74 74
from astakos.im.endpoints.qh import (
75 75
    send_quotas, qh_get_quotas,
76 76
    register_resources, qh_add_quota, QuotaLimits,
77
    QuotaValues, add_quota_values)
77
    )
78 78
from astakos.im import auth_providers as auth
79 79

  
80 80
import astakos.im.messages as astakos_messages
......
255 255

  
256 256
    register_resources(rs)
257 257

  
258
def _quota_values(capacity):
259
    return QuotaValues(
260
        quantity = 0,
261
        capacity = capacity,
262
        )
263 258

  
264 259
def get_default_quota():
265 260
    _DEFAULT_QUOTA = {}
266 261
    resources = Resource.objects.select_related('service').all()
267 262
    for resource in resources:
268 263
        capacity = resource.uplimit
269
        limits = _quota_values(capacity)
270
        _DEFAULT_QUOTA[resource.full_name()] = limits
264
        _DEFAULT_QUOTA[resource.full_name()] = capacity
271 265

  
272 266
    return _DEFAULT_QUOTA
273 267

  
......
463 457
            self.add_resource_policy(**p)
464 458

  
465 459
    def add_resource_policy(
466
            self, resource, capacity, quantity,
460
            self, resource, capacity,
467 461
            update=True):
468 462
        """Raises ObjectDoesNotExist, IntegrityError"""
469 463
        s, sep, r = resource.partition(RESOURCE_SEPARATOR)
......
472 466
            AstakosUserQuota.objects.update_or_create(
473 467
                user=self, resource=resource, defaults={
474 468
                    'capacity':capacity,
475
                    'quantity': quantity,
476 469
                    })
477 470
        else:
478 471
            q = self.astakosuserquota_set
......
795 788
        uuid = user_quota.user.uuid
796 789
        user_init = initial.get(uuid, {})
797 790
        resource = user_quota.resource.full_name()
798
        user_init[resource] = user_quota.quota_values()
791
        user_init[resource] = user_quota.capacity
799 792
        initial[uuid] = user_init
800 793

  
801 794
    return initial
......
827 820
                continue
828 821
            resource = grant.resource.full_name()
829 822
            prev = userquotas.get(resource, 0)
830
            new = add_quota_values(prev, grant.member_quota_values())
823
            new = prev + grant.member_capacity
831 824
            userquotas[resource] = new
832 825
        quotas[uuid] = userquotas
833 826

  
......
1058 1051
    class Meta:
1059 1052
        unique_together = ("resource", "user")
1060 1053

  
1061
    def quota_values(self):
1062
        return QuotaValues(
1063
            quantity = self.quantity,
1064
            capacity = self.capacity,
1065
            )
1066

  
1067 1054

  
1068 1055
class ApprovalTerms(models.Model):
1069 1056
    """
......
1784 1771
    class Meta:
1785 1772
        unique_together = ("resource", "project_application")
1786 1773

  
1787
    def member_quota_values(self):
1788
        return QuotaValues(
1789
            quantity = 0,
1790
            capacity = self.member_capacity,
1791
            )
1792

  
1793 1774
    def display_member_capacity(self):
1794 1775
        if self.member_capacity:
1795 1776
            if self.resource.unit:

Also available in: Unified diff