Revision 22e52ede

b/db/models.py
151 151

  
152 152
            return enh_fc.effective_from <= a_date and enh_fc.effective_to >= a_date
153 153

  
154
        def calculate_cost(start_date, end_date, cost):
155
            """Calculate the total cost for the specified duration"""
156
            td = end_date - start_date
157
            sec = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
158
            total_hours = float(sec) / float(60.0*60.0)
159
            total_cost = float(cost)*total_hours
160

  
161
            return round(total_cost)
162

  
163 154
        # Get the related FlavorCost objects, sorted.
164 155
        price_list = FlavorCost.objects.filter(flavor=self).order_by('effective_from')
165 156

  
......
195 186
            else:
196 187
                cost = p.cost_inactive
197 188

  
198
            results.append( ( p.effective_from, calculate_cost(p.effective_from, p.effective_to, cost)) )
189
            results.append( ( p.effective_from, utils.calculate_cost(p.effective_from, p.effective_to, cost)) )
199 190

  
200 191
        return results
201 192

  
......
345 336
            # breaking VirtualMachine.object.create() among other things.
346 337
            self._operstate = 'BUILD'
347 338

  
348
    def start_action(self, action):
349
        """Update the state of a VM when a new action is initiated."""
350
        if not action in [x[0] for x in VirtualMachine.ACTIONS]:
351
            raise VirtualMachine.InvalidActionError(action)
352

  
353
        # No actions to deleted and no actions beside destroy to suspended VMs
354
        if self.deleted:
355
            raise VirtualMachine.InvalidActionError(action)
356

  
357
        self._action = action
358
        self._backendjobid = None
359
        self._backendopcode = None
360
        self._backendjobstatus = None
361
        self._backendlogmsg = None
362

  
363
        # Update the relevant flags if the VM is being suspended or destroyed
364
        if action == "DESTROY":
365
            self.deleted = True
366
        elif action == "SUSPEND":
367
            self.suspended = True
368
        elif action == "START":
369
            self.suspended = False
370
        self.save()
371

  
372 339
    # FIXME: leave this here to preserve the property rsapistate
373 340
    def _get_rsapi_state(self):
374 341
        return utils.get_rsapi_state(self)
......
397 364

  
398 365
        # Call charge() unconditionally before any change of
399 366
        # internal state.
400
        credits.charge()
367
        credits.charge(self)
401 368
        self._operstate = new_operstate
402 369

  
403 370

  
b/logic/backend.py
27 27
    # Any other notification of failure leaves the operating state unchanged
28 28

  
29 29
    vm.save()
30

  
31
def start_action(vm, action):
32
    """Update the state of a VM when a new action is initiated."""
33
    if not action in [x[0] for x in VirtualMachine.ACTIONS]:
34
        raise VirtualMachine.InvalidActionError(action)
35

  
36
    # No actions to deleted and no actions beside destroy to suspended VMs
37
    if vm.deleted:
38
        raise VirtualMachine.InvalidActionError(action)
39

  
40
    vm._action = action
41
    vm._backendjobid = None
42
    vm._backendopcode = None
43
    vm._backendjobstatus = None
44
    vm._backendlogmsg = None
45

  
46
    # Update the relevant flags if the VM is being suspended or destroyed
47
    if action == "DESTROY":
48
        vm.deleted = True
49
    elif action == "SUSPEND":
50
        vm.suspended = True
51
    elif action == "START":
52
        vm.suspended = False
53
    vm.save()
b/logic/utils.py
33 33
        vm._backendjobstatus in ('queued', 'waiting', 'running'):
34 34
        return "REBOOT"
35 35
    return r
36

  
37

  
38
def calculate_cost(start_date, end_date, cost):
39
    """Calculate the total cost for the specified duration"""
40
    td = end_date - start_date
41
    sec = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
42
    total_hours = float(sec) / float(60.0*60.0)
43
    total_cost = float(cost)*total_hours
44

  
45
    return round(total_cost)

Also available in: Unified diff