Revision 8d97deff logic/credits.py
b/logic/credits.py | ||
---|---|---|
6 | 6 |
|
7 | 7 |
from datetime import datetime |
8 | 8 |
|
9 |
from db.models import Debit, FlavorCost, VirtualMachine
|
|
9 |
from db.models import Debit, FlavorCost |
|
10 | 10 |
|
11 | 11 |
from django.db import transaction |
12 | 12 |
|
13 |
import synnefo.settings as settings |
|
14 |
|
|
15 | 13 |
@transaction.commit_on_success |
16 | 14 |
def debit_account(user , amount, vm, description): |
17 | 15 |
"""Charges the user with the specified amount of credits for a vm (resource)""" |
... | ... | |
54 | 52 |
|
55 | 53 |
# remember, we charge only for Started and Stopped |
56 | 54 |
if vm._operstate == 'STARTED': |
57 |
cost_list = vm.flavor.get_cost_active(start_datetime, vm.charged)
|
|
55 |
cost_list = get_cost_active(vm.flavor, start_datetime, vm.charged)
|
|
58 | 56 |
elif vm._operstate == 'STOPPED': |
59 |
cost_list = vm.flavor.get_cost_inactive(start_datetime, vm.charged)
|
|
57 |
cost_list = get_cost_inactive(vm.flavor, start_datetime, vm.charged)
|
|
60 | 58 |
|
61 | 59 |
# find the total vost |
62 | 60 |
total_cost = sum([x[1] for x in cost_list]) |
... | ... | |
67 | 65 |
|
68 | 66 |
vm.save() |
69 | 67 |
|
68 |
|
|
70 | 69 |
def get_costs(vm, start_datetime, end_datetime, active): |
71 | 70 |
"""Return a list with FlavorCost objects for the specified duration""" |
72 | 71 |
def between(enh_fc, a_date): |
... | ... | |
115 | 114 |
|
116 | 115 |
return results |
117 | 116 |
|
118 |
def id_from_instance_name(name): |
|
119 |
"""Returns VirtualMachine's Django id, given a ganeti machine name. |
|
120 |
|
|
121 |
Strips the ganeti prefix atm. Needs a better name! |
|
122 |
|
|
123 |
""" |
|
124 |
if not str(name).startswith(settings.BACKEND_PREFIX_ID): |
|
125 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
|
126 |
ns = str(name).lstrip(settings.BACKEND_PREFIX_ID) |
|
127 |
if not ns.isdigit(): |
|
128 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
|
129 |
|
|
130 |
return int(ns) |
|
131 |
|
|
132 |
|
|
133 |
def get_rsapi_state(vm): |
|
134 |
"""Returns the RSAPI state for a virtual machine""" |
|
135 |
try: |
|
136 |
r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm._operstate] |
|
137 |
except KeyError: |
|
138 |
return "UNKNOWN" |
|
139 |
# A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress |
|
140 |
if r == 'ACTIVE' and vm._backendopcode == 'OP_INSTANCE_REBOOT' and \ |
|
141 |
vm._backendjobstatus in ('queued', 'waiting', 'running'): |
|
142 |
return "REBOOT" |
|
143 |
return r |
|
144 |
|
|
145 | 117 |
|
146 | 118 |
def calculate_cost(start_date, end_date, cost): |
147 | 119 |
"""Calculate the total cost for the specified duration""" |
... | ... | |
151 | 123 |
total_cost = float(cost)*total_hours |
152 | 124 |
|
153 | 125 |
return round(total_cost) |
126 |
|
|
127 |
|
|
128 |
def get_cost_active(vm, start_datetime, end_datetime): |
|
129 |
"""Returns a list with the active costs for the specified duration""" |
|
130 |
return get_costs(vm, start_datetime, end_datetime, True) |
|
131 |
|
|
132 |
|
|
133 |
def get_cost_inactive(vm, start_datetime, end_datetime): |
|
134 |
"""Returns a list with the inactive costs for the specified duration""" |
|
135 |
return get_costs(vm, start_datetime, end_datetime, False) |
Also available in: Unified diff