Revision 2e2fc330 snf-cyclades-app/synnefo/quotas/util.py
b/snf-cyclades-app/synnefo/quotas/util.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
from synnefo.db.models import VirtualMachine, Network, IPAddress |
37 | 37 |
from synnefo.quotas import Quotaholder |
38 |
from synnefo.util.keypath import set_path |
|
38 | 39 |
|
39 | 40 |
|
40 |
def get_db_holdings(user=None): |
|
41 |
MiB = 2 ** 20 |
|
42 |
GiB = 2 ** 30 |
|
43 |
|
|
44 |
|
|
45 |
def get_db_holdings(user=None, project=None): |
|
41 | 46 |
"""Get holdings from Cyclades DB.""" |
42 | 47 |
holdings = {} |
43 | 48 |
|
... | ... | |
50 | 55 |
networks = networks.filter(userid=user) |
51 | 56 |
floating_ips = floating_ips.filter(userid=user) |
52 | 57 |
|
58 |
if project is not None: |
|
59 |
vms = vms.filter(project=project) |
|
60 |
networks = networks.filter(project=project) |
|
61 |
floating_ips = floating_ips.filter(project=project) |
|
62 |
|
|
53 | 63 |
# Get resources related with VMs |
54 |
vm_resources = vms.values("userid")\ |
|
55 |
.annotate(num=Count("id"), |
|
56 |
total_ram=Sum("flavor__ram"), |
|
57 |
total_cpu=Sum("flavor__cpu"), |
|
58 |
disk=Sum("flavor__disk")) |
|
59 |
vm_active_resources = \ |
|
60 |
vms.values("userid")\ |
|
61 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") | |
|
62 |
Q(operstate="ERROR"))\ |
|
63 |
.annotate(ram=Sum("flavor__ram"), |
|
64 |
cpu=Sum("flavor__cpu")) |
|
64 |
vm_resources = vms.values("userid", "project")\ |
|
65 |
.annotate(num=Count("id"), |
|
66 |
total_ram=Sum("flavor__ram"), |
|
67 |
total_cpu=Sum("flavor__cpu"), |
|
68 |
disk=Sum("flavor__disk")) |
|
65 | 69 |
|
66 | 70 |
for vm_res in vm_resources.iterator(): |
67 | 71 |
user = vm_res['userid'] |
72 |
project = vm_res['project'] |
|
68 | 73 |
res = {"cyclades.vm": vm_res["num"], |
69 | 74 |
"cyclades.total_cpu": vm_res["total_cpu"], |
70 |
"cyclades.disk": 1073741824 * vm_res["disk"], |
|
71 |
"cyclades.total_ram": 1048576 * vm_res["total_ram"]} |
|
72 |
holdings[user] = res |
|
75 |
"cyclades.disk": vm_res["disk"] * GiB, |
|
76 |
"cyclades.total_ram": vm_res["total_ram"] * MiB} |
|
77 |
set_path(holdings, [user, project], res, createpath=True) |
|
78 |
|
|
79 |
vm_active_resources = vms.values("userid", "project")\ |
|
80 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") | |
|
81 |
Q(operstate="ERROR"))\ |
|
82 |
.annotate(ram=Sum("flavor__ram"), |
|
83 |
cpu=Sum("flavor__cpu")) |
|
73 | 84 |
|
74 | 85 |
for vm_res in vm_active_resources.iterator(): |
75 | 86 |
user = vm_res['userid'] |
76 |
holdings[user]["cyclades.cpu"] = vm_res["cpu"] |
|
77 |
holdings[user]["cyclades.ram"] = 1048576 * vm_res["ram"] |
|
87 |
project = vm_res['project'] |
|
88 |
set_path(holdings, [user, project, "cyclades.cpu"], vm_res["cpu"], |
|
89 |
createpath=True) |
|
90 |
set_path(holdings, [user, project, "cyclades.ram"], |
|
91 |
vm_res["ram"] * MiB, createpath=True) |
|
78 | 92 |
|
79 | 93 |
# Get resources related with networks |
80 |
net_resources = networks.values("userid")\ |
|
94 |
net_resources = networks.values("userid", "project")\
|
|
81 | 95 |
.annotate(num=Count("id")) |
96 |
|
|
82 | 97 |
for net_res in net_resources.iterator(): |
83 | 98 |
user = net_res['userid'] |
84 |
holdings.setdefault(user, {}) |
|
85 |
holdings[user]["cyclades.network.private"] = net_res["num"] |
|
99 |
if user is None: |
|
100 |
continue |
|
101 |
project = net_res['project'] |
|
102 |
set_path(holdings, [user, project, "cyclades.network.private"], |
|
103 |
net_res["num"], createpath=True) |
|
86 | 104 |
|
87 |
floating_ips_resources = floating_ips.values("userid")\ |
|
105 |
floating_ips_resources = floating_ips.values("userid", "project")\
|
|
88 | 106 |
.annotate(num=Count("id")) |
107 |
|
|
89 | 108 |
for floating_ip_res in floating_ips_resources.iterator(): |
90 | 109 |
user = floating_ip_res["userid"] |
91 |
holdings.setdefault(user, {}) |
|
92 |
holdings[user]["cyclades.floating_ip"] = floating_ip_res["num"] |
|
110 |
project = floating_ip_res["project"] |
|
111 |
set_path(holdings, [user, project, "cyclades.floating_ip"], |
|
112 |
floating_ip_res["num"], createpath=True) |
|
113 |
|
|
114 |
return holdings |
|
115 |
|
|
116 |
|
|
117 |
def get_db_project_holdings(project=None): |
|
118 |
"""Get holdings from Cyclades DB.""" |
|
119 |
holdings = {} |
|
120 |
|
|
121 |
vms = VirtualMachine.objects.filter(deleted=False) |
|
122 |
networks = Network.objects.filter(deleted=False) |
|
123 |
floating_ips = IPAddress.objects.filter(deleted=False, floating_ip=True) |
|
124 |
|
|
125 |
if project is not None: |
|
126 |
vms = vms.filter(project=project) |
|
127 |
networks = networks.filter(project=project) |
|
128 |
floating_ips = floating_ips.filter(project=project) |
|
129 |
|
|
130 |
# Get resources related with VMs |
|
131 |
vm_resources = vms.values("project")\ |
|
132 |
.annotate(num=Count("id"), |
|
133 |
total_ram=Sum("flavor__ram"), |
|
134 |
total_cpu=Sum("flavor__cpu"), |
|
135 |
disk=Sum("flavor__disk")) |
|
136 |
|
|
137 |
for vm_res in vm_resources.iterator(): |
|
138 |
project = vm_res['project'] |
|
139 |
res = {"cyclades.vm": vm_res["num"], |
|
140 |
"cyclades.total_cpu": vm_res["total_cpu"], |
|
141 |
"cyclades.disk": vm_res["disk"] * GiB, |
|
142 |
"cyclades.total_ram": vm_res["total_ram"] * MiB} |
|
143 |
set_path(holdings, [project], res, createpath=True) |
|
144 |
|
|
145 |
vm_active_resources = vms.values("project")\ |
|
146 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") | |
|
147 |
Q(operstate="ERROR"))\ |
|
148 |
.annotate(ram=Sum("flavor__ram"), |
|
149 |
cpu=Sum("flavor__cpu")) |
|
150 |
|
|
151 |
for vm_res in vm_active_resources.iterator(): |
|
152 |
project = vm_res['project'] |
|
153 |
set_path(holdings, [project, "cyclades.cpu"], vm_res["cpu"], |
|
154 |
createpath=True) |
|
155 |
set_path(holdings, [project, "cyclades.ram"], |
|
156 |
vm_res["ram"] * MiB, createpath=True) |
|
157 |
|
|
158 |
# Get resources related with networks |
|
159 |
net_resources = networks.values("project").annotate(num=Count("id")) |
|
160 |
|
|
161 |
for net_res in net_resources.iterator(): |
|
162 |
project = net_res['project'] |
|
163 |
if project is None: |
|
164 |
continue |
|
165 |
set_path(holdings, [project, "cyclades.network.private"], |
|
166 |
net_res["num"], createpath=True) |
|
167 |
|
|
168 |
floating_ips_resources = floating_ips.values("project")\ |
|
169 |
.annotate(num=Count("id")) |
|
170 |
|
|
171 |
for floating_ip_res in floating_ips_resources.iterator(): |
|
172 |
project = floating_ip_res["project"] |
|
173 |
set_path(holdings, [project, "cyclades.floating_ip"], |
|
174 |
floating_ip_res["num"], createpath=True) |
|
93 | 175 |
|
94 | 176 |
return holdings |
95 | 177 |
|
Also available in: Unified diff