root / snf-cyclades-app / synnefo / quotas / util.py @ 41a7fae7
History | View | Annotate | Download (3.9 kB)
1 |
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from django.db.models import Sum, Count, Q |
35 |
|
36 |
from synnefo.db.models import VirtualMachine, Network |
37 |
from synnefo.quotas import Quotaholder, ASTAKOS_TOKEN |
38 |
|
39 |
|
40 |
def get_db_holdings(user=None): |
41 |
"""Get holdings from Cyclades DB."""
|
42 |
holdings = {} |
43 |
|
44 |
vms = VirtualMachine.objects.filter(deleted=False)
|
45 |
networks = Network.objects.filter(deleted=False)
|
46 |
|
47 |
if user is not None: |
48 |
vms = vms.filter(userid=user) |
49 |
networks = networks.filter(userid=user) |
50 |
|
51 |
# Get resources related with VMs
|
52 |
vm_resources = vms.values("userid").annotate(num=Count("id"), |
53 |
ram=Sum("flavor__ram"),
|
54 |
cpu=Sum("flavor__cpu"),
|
55 |
disk=Sum("flavor__disk"))
|
56 |
vm_active_resources = \ |
57 |
vms.values("userid")\
|
58 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") |\ |
59 |
Q(operstate="ERROR"))\
|
60 |
.annotate(active_ram=Sum("flavor__ram"),
|
61 |
active_cpu=Sum("flavor__cpu"))
|
62 |
|
63 |
for vm_res in vm_resources: |
64 |
user = vm_res['userid']
|
65 |
res = {"cyclades.vm": vm_res["num"], |
66 |
"cyclades.cpu": vm_res["cpu"], |
67 |
"cyclades.disk": 1073741824 * vm_res["disk"], |
68 |
"cyclades.ram": 1048576 * vm_res["ram"]} |
69 |
holdings[user] = res |
70 |
|
71 |
for vm_res in vm_active_resources: |
72 |
user = vm_res['userid']
|
73 |
holdings[user]["cyclades.active_cpu"] = vm_res["active_cpu"] |
74 |
holdings[user]["cyclades.active_ram"] = 1048576 * vm_res["active_ram"] |
75 |
|
76 |
# Get resources related with networks
|
77 |
net_resources = networks.values("userid")\
|
78 |
.annotate(num=Count("id"))
|
79 |
for net_res in net_resources: |
80 |
user = net_res['userid']
|
81 |
if user not in holdings: |
82 |
holdings[user] = {} |
83 |
holdings[user]["cyclades.network.private"] = net_res["num"] |
84 |
|
85 |
return holdings
|
86 |
|
87 |
|
88 |
def get_quotaholder_holdings(user=None): |
89 |
"""Get quotas from Quotaholder for all Cyclades resources.
|
90 |
|
91 |
Returns quotas for all users, unless a single user is specified.
|
92 |
"""
|
93 |
qh = Quotaholder.get() |
94 |
return qh.service_get_quotas(ASTAKOS_TOKEN, user)
|
95 |
|
96 |
|
97 |
def transform_quotas(quotas): |
98 |
d = {} |
99 |
for resource, counters in quotas.iteritems(): |
100 |
used = counters['usage']
|
101 |
limit = counters['limit']
|
102 |
pending = counters['pending']
|
103 |
d[resource] = (used, limit, pending) |
104 |
return d
|