Revision c8cb5d53 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 |
from collections import defaultdict
|
|
39 | 39 |
|
40 |
QuotaDict = lambda: defaultdict(lambda: defaultdict(dict)) |
|
40 | 41 |
|
41 | 42 |
MiB = 2 ** 20 |
42 | 43 |
GiB = 2 ** 30 |
... | ... | |
44 | 45 |
|
45 | 46 |
def get_db_holdings(user=None, project=None): |
46 | 47 |
"""Get holdings from Cyclades DB.""" |
47 |
holdings = {}
|
|
48 |
holdings = QuotaDict()
|
|
48 | 49 |
|
49 | 50 |
vms = VirtualMachine.objects.filter(deleted=False) |
50 | 51 |
networks = Network.objects.filter(deleted=False) |
... | ... | |
74 | 75 |
"cyclades.total_cpu": vm_res["total_cpu"], |
75 | 76 |
"cyclades.disk": vm_res["disk"] * GiB, |
76 | 77 |
"cyclades.total_ram": vm_res["total_ram"] * MiB} |
77 |
set_path(holdings, [user, project], res, createpath=True)
|
|
78 |
holdings[user][project] = res
|
|
78 | 79 |
|
79 | 80 |
vm_active_resources = vms.values("userid", "project")\ |
80 | 81 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") | |
... | ... | |
85 | 86 |
for vm_res in vm_active_resources.iterator(): |
86 | 87 |
user = vm_res['userid'] |
87 | 88 |
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) |
|
89 |
holdings[user][project]["cyclades.cpu"] = vm_res["cpu"] |
|
90 |
holdings[user][project]["cyclades.ram"] = vm_res["ram"] * MiB |
|
92 | 91 |
|
93 | 92 |
# Get resources related with networks |
94 | 93 |
net_resources = networks.values("userid", "project")\ |
... | ... | |
99 | 98 |
if user is None: |
100 | 99 |
continue |
101 | 100 |
project = net_res['project'] |
102 |
set_path(holdings, [user, project, "cyclades.network.private"], |
|
103 |
net_res["num"], createpath=True) |
|
101 |
holdings[user][project]["cyclades.network.private"] = net_res["num"] |
|
104 | 102 |
|
105 | 103 |
floating_ips_resources = floating_ips.values("userid", "project")\ |
106 | 104 |
.annotate(num=Count("id")) |
... | ... | |
108 | 106 |
for floating_ip_res in floating_ips_resources.iterator(): |
109 | 107 |
user = floating_ip_res["userid"] |
110 | 108 |
project = floating_ip_res["project"] |
111 |
set_path(holdings, [user, project, "cyclades.floating_ip"],
|
|
112 |
floating_ip_res["num"], createpath=True)
|
|
109 |
holdings[user][project]["cyclades.floating_ip"] = \
|
|
110 |
floating_ip_res["num"]
|
|
113 | 111 |
|
114 | 112 |
return holdings |
115 | 113 |
|
116 | 114 |
|
117 | 115 |
def get_db_project_holdings(project=None): |
118 | 116 |
"""Get holdings from Cyclades DB.""" |
119 |
holdings = {}
|
|
117 |
holdings = QuotaDict()
|
|
120 | 118 |
|
121 | 119 |
vms = VirtualMachine.objects.filter(deleted=False) |
122 | 120 |
networks = Network.objects.filter(deleted=False) |
... | ... | |
140 | 138 |
"cyclades.total_cpu": vm_res["total_cpu"], |
141 | 139 |
"cyclades.disk": vm_res["disk"] * GiB, |
142 | 140 |
"cyclades.total_ram": vm_res["total_ram"] * MiB} |
143 |
set_path(holdings, [project], res, createpath=True)
|
|
141 |
holdings[project] = res
|
|
144 | 142 |
|
145 | 143 |
vm_active_resources = vms.values("project")\ |
146 | 144 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") | |
... | ... | |
150 | 148 |
|
151 | 149 |
for vm_res in vm_active_resources.iterator(): |
152 | 150 |
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) |
|
151 |
holdings[project]["cyclades.cpu"] = vm_res["cpu"] |
|
152 |
holdings[project]["cyclades.ram"] = vm_res["ram"] * MiB |
|
157 | 153 |
|
158 | 154 |
# Get resources related with networks |
159 | 155 |
net_resources = networks.values("project").annotate(num=Count("id")) |
... | ... | |
162 | 158 |
project = net_res['project'] |
163 | 159 |
if project is None: |
164 | 160 |
continue |
165 |
set_path(holdings, [project, "cyclades.network.private"], |
|
166 |
net_res["num"], createpath=True) |
|
161 |
holdings[project]["cyclades.network.private"] = net_res["num"] |
|
167 | 162 |
|
168 | 163 |
floating_ips_resources = floating_ips.values("project")\ |
169 | 164 |
.annotate(num=Count("id")) |
170 | 165 |
|
171 | 166 |
for floating_ip_res in floating_ips_resources.iterator(): |
172 | 167 |
project = floating_ip_res["project"] |
173 |
set_path(holdings, [project, "cyclades.floating_ip"], |
|
174 |
floating_ip_res["num"], createpath=True) |
|
168 |
holdings[project]["cyclades.floating_ip"] = floating_ip_res["num"] |
|
175 | 169 |
|
176 | 170 |
return holdings |
177 | 171 |
|
Also available in: Unified diff