Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / quotas / util.py @ a8333def

History | View | Annotate | Download (3.4 kB)

1 629acc65 Giorgos Korfiatis
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 600a3d4d Christos Stavrakakis
#
3 600a3d4d Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 600a3d4d Christos Stavrakakis
# without modification, are permitted provided that the following
5 600a3d4d Christos Stavrakakis
# conditions are met:
6 600a3d4d Christos Stavrakakis
#
7 600a3d4d Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 600a3d4d Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 600a3d4d Christos Stavrakakis
#      disclaimer.
10 600a3d4d Christos Stavrakakis
#
11 600a3d4d Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 600a3d4d Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 600a3d4d Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 600a3d4d Christos Stavrakakis
#      provided with the distribution.
15 600a3d4d Christos Stavrakakis
#
16 600a3d4d Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 600a3d4d Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 600a3d4d Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 600a3d4d Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 600a3d4d Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 600a3d4d Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 600a3d4d Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 600a3d4d Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 600a3d4d Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 600a3d4d Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 600a3d4d Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 600a3d4d Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 600a3d4d Christos Stavrakakis
#
29 600a3d4d Christos Stavrakakis
# The views and conclusions contained in the software and
30 600a3d4d Christos Stavrakakis
# documentation are those of the authors and should not be
31 600a3d4d Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 600a3d4d Christos Stavrakakis
# or implied, of GRNET S.A.
33 600a3d4d Christos Stavrakakis
34 600a3d4d Christos Stavrakakis
from django.db.models import Sum, Count
35 600a3d4d Christos Stavrakakis
36 600a3d4d Christos Stavrakakis
from synnefo.db.models import VirtualMachine, Network
37 629acc65 Giorgos Korfiatis
from synnefo.quotas import Quotaholder, ASTAKOS_TOKEN
38 600a3d4d Christos Stavrakakis
39 c4e45b57 Christos Stavrakakis
40 656cf771 Christos Stavrakakis
def get_db_holdings(user=None):
41 600a3d4d Christos Stavrakakis
    """Get holdings from Cyclades DB."""
42 600a3d4d Christos Stavrakakis
    holdings = {}
43 600a3d4d Christos Stavrakakis
44 5d3675bb Christos Stavrakakis
    vms = VirtualMachine.objects.filter(deleted=False)
45 5d3675bb Christos Stavrakakis
    networks = Network.objects.filter(deleted=False)
46 600a3d4d Christos Stavrakakis
47 656cf771 Christos Stavrakakis
    if user is not None:
48 656cf771 Christos Stavrakakis
        vms = vms.filter(userid=user)
49 656cf771 Christos Stavrakakis
        networks = networks.filter(userid=user)
50 600a3d4d Christos Stavrakakis
51 600a3d4d Christos Stavrakakis
    # Get resources related with VMs
52 600a3d4d Christos Stavrakakis
    vm_resources = vms.values("userid").annotate(num=Count("id"),
53 600a3d4d Christos Stavrakakis
                                                 ram=Sum("flavor__ram"),
54 600a3d4d Christos Stavrakakis
                                                 cpu=Sum("flavor__cpu"),
55 600a3d4d Christos Stavrakakis
                                                 disk=Sum("flavor__disk"))
56 600a3d4d Christos Stavrakakis
    for vm_res in vm_resources:
57 600a3d4d Christos Stavrakakis
        user = vm_res['userid']
58 656cf771 Christos Stavrakakis
        res = {"cyclades.vm": vm_res["num"],
59 656cf771 Christos Stavrakakis
               "cyclades.cpu": vm_res["cpu"],
60 656cf771 Christos Stavrakakis
               "cyclades.disk": 1073741824 * vm_res["disk"],
61 656cf771 Christos Stavrakakis
               "cyclades.ram": 1048576 * vm_res["ram"]}
62 600a3d4d Christos Stavrakakis
        holdings[user] = res
63 600a3d4d Christos Stavrakakis
64 600a3d4d Christos Stavrakakis
    # Get resources related with networks
65 600a3d4d Christos Stavrakakis
    net_resources = networks.values("userid")\
66 68b952f9 Christos Stavrakakis
                            .annotate(num=Count("id"))
67 600a3d4d Christos Stavrakakis
    for net_res in net_resources:
68 600a3d4d Christos Stavrakakis
        user = net_res['userid']
69 600a3d4d Christos Stavrakakis
        if user not in holdings:
70 600a3d4d Christos Stavrakakis
            holdings[user] = {}
71 656cf771 Christos Stavrakakis
        holdings[user]["cyclades.network.private"] = net_res["num"]
72 600a3d4d Christos Stavrakakis
73 600a3d4d Christos Stavrakakis
    return holdings
74 600a3d4d Christos Stavrakakis
75 600a3d4d Christos Stavrakakis
76 629acc65 Giorgos Korfiatis
def get_quotaholder_holdings(user=None):
77 629acc65 Giorgos Korfiatis
    """Get quotas from Quotaholder for all Cyclades resources.
78 600a3d4d Christos Stavrakakis

79 629acc65 Giorgos Korfiatis
    Returns quotas for all users, unless a single user is specified.
80 600a3d4d Christos Stavrakakis
    """
81 629acc65 Giorgos Korfiatis
    qh = Quotaholder.get()
82 656cf771 Christos Stavrakakis
    return qh.service_get_quotas(ASTAKOS_TOKEN, user)
83 600a3d4d Christos Stavrakakis
84 600a3d4d Christos Stavrakakis
85 629acc65 Giorgos Korfiatis
def transform_quotas(quotas):
86 629acc65 Giorgos Korfiatis
    d = {}
87 629acc65 Giorgos Korfiatis
    for resource, counters in quotas.iteritems():
88 656cf771 Christos Stavrakakis
        used = counters['usage']
89 629acc65 Giorgos Korfiatis
        limit = counters['limit']
90 656cf771 Christos Stavrakakis
        pending = counters['pending']
91 656cf771 Christos Stavrakakis
        d[resource] = (used, limit, pending)
92 629acc65 Giorgos Korfiatis
    return d