Revision ba777b02 snf-cyclades-app/synnefo/quotas/__init__.py

b/snf-cyclades-app/synnefo/quotas/__init__.py
43 43
log = logging.getLogger(__name__)
44 44

  
45 45

  
46
QUOTABLE_RESOURCES = [VirtualMachine, Network, IPAddress]
47

  
48

  
46 49
DEFAULT_SOURCE = 'system'
47 50
RESOURCES = [
48 51
    "cyclades.vm",
......
115 118
                                             force=force,
116 119
                                             auto_accept=auto_accept)
117 120

  
118
    if serial:
119
        serial_info = {"serial": serial}
120
        if auto_accept:
121
            serial_info["pending"] = False
122
            serial_info["accept"] = True
123
            serial_info["resolved"] = True
124
        return QuotaHolderSerial.objects.create(**serial_info)
125
    else:
121
    if not serial:
126 122
        raise Exception("No serial")
127 123

  
124
    serial_info = {"serial": serial}
125
    if auto_accept:
126
        serial_info["pending"] = False
127
        serial_info["accept"] = True
128
        serial_info["resolved"] = True
129

  
130
    serial = QuotaHolderSerial.objects.create(**serial_info)
131

  
132
    # Correlate the serial with the resource. Resolved serials are not
133
    # attached to resources
134
    if not auto_accept:
135
        resource.serial = serial
136
        resource.save()
128 137

  
129
def accept_serial(serial, strict=True):
138
    return serial
139

  
140

  
141
def accept_resource_serial(resource, strict=True):
142
    serial = resource.serial
130 143
    assert serial.pending or serial.accept, "%s can't be accepted" % serial
131
    response = resolve_commissions(accept=[serial.serial], strict=strict)
132
    return response
144
    log.debug("Accepting serial %s of resource %s", serial, resource)
145
    _resolve_commissions(accept=[serial.serial], strict=strict)
146
    resource.serial = None
147
    resource.save()
148
    return resource
133 149

  
134 150

  
135
def reject_serial(serial, strict=True):
151
def reject_resource_serial(resource, strict=True):
152
    serial = resource.serial
136 153
    assert serial.pending or not serial.accept, "%s can't be rejected" % serial
137
    response = resolve_commissions(reject=[serial.serial], strict=strict)
138
    return response
154
    log.debug("Rejecting serial %s of resource %s", serial, resource)
155
    _resolve_commissions(reject=[serial.serial], strict=strict)
156
    resource.serial = None
157
    resource.save()
158
    return resource
139 159

  
140 160

  
141
def resolve_commissions(accept=None, reject=None, strict=True):
161
def _resolve_commissions(accept=None, reject=None, strict=True):
142 162
    if accept is None:
143 163
        accept = []
144 164
    if reject is None:
......
167 187
    return response
168 188

  
169 189

  
190
def reconcile_resolve_commissions(accept=None, reject=None, strict=True):
191
    response = _resolve_commissions(accept=accept,
192
                                    reject=reject,
193
                                    strict=strict)
194
    affected = response.get("accepted", []) + response.get("rejected", [])
195
    for resource in QUOTABLE_RESOURCES:
196
        resource.objects.filter(serial__in=affected).update(serial=None)
197

  
198

  
170 199
def resolve_pending_commissions():
171 200
    """Resolve quotaholder pending commissions.
172 201

  
......
247 276
                                        action_fields=action_fields,
248 277
                                        commission_name=commission_reason)
249 278

  
279
    if serial is None:
280
        return
281

  
250 282
    # Mark the serial as one to accept and associate it with the resource
251 283
    serial.pending = False
252 284
    serial.accept = True
......
255 287

  
256 288
    try:
257 289
        # Accept the commission to quotaholder
258
        accept_serial(serial)
290
        accept_resource_serial(resource)
259 291
    except:
260 292
        # Do not crash if we can not accept commission to Quotaholder. Quotas
261 293
        # have already been reserved and the resource already exists in DB.
262 294
        # Just log the error
263
        log.exception("Failed to accept commission: %s", serial)
264

  
265
    return serial
295
        log.exception("Failed to accept commission: %s", resource.serial)
266 296

  
267 297

  
268 298
def get_commission_info(resource, action, action_fields=None):
......
342 372
    # The one who succeeds will be finally accepted, and all other will be
343 373
    # rejected
344 374
    force = force or (action == "DESTROY")
345
    resolve_commission(resource.serial, force=force)
375
    resolve_resource_commission(resource, force=force)
346 376

  
347 377
    serial = issue_commission(resource, action, name=commission_name,
348 378
                              force=force, auto_accept=auto_accept,
349 379
                              action_fields=action_fields)
350
    resource.serial = serial
351
    resource.save()
352 380
    return serial
353 381

  
354 382

  
......
356 384
    pass
357 385

  
358 386

  
359
def resolve_commission(serial, force=False):
387
def resolve_resource_commission(resource, force=False):
388
    serial = resource.serial
360 389
    if serial is None or serial.resolved:
361 390
        return
362 391
    if serial.pending and not force:
......
364 393
        raise ResolveError(m)
365 394
    log.warning("Resolving pending commission: %s", serial)
366 395
    if not serial.pending and serial.accept:
367
        accept_serial(serial)
396
        accept_resource_serial(resource)
368 397
    else:
369
        reject_serial(serial)
398
        reject_resource_serial(resource)

Also available in: Unified diff