Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.9 kB)

1
# Copyright 2012 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
35

    
36
from synnefo.db.models import VirtualMachine, Network
37
from synnefo.quotas import get_quota_holder
38
from synnefo.lib.quotaholder.api.exception import NoEntityError
39

    
40

    
41
def get_db_holdings(users=None):
42
    """Get holdings from Cyclades DB."""
43
    holdings = {}
44

    
45
    vms = VirtualMachine.objects
46
    networks = Network.objects
47

    
48
    if users:
49
        assert(type(users) is list)
50
        vms = vms.filter(userid__in=users)
51
        networks = networks.filter(userid__in=users)
52

    
53
    # Get resources related with VMs
54
    vm_resources = vms.values("userid").annotate(num=Count("id"),
55
                                                 ram=Sum("flavor__ram"),
56
                                                 cpu=Sum("flavor__cpu"),
57
                                                 disk=Sum("flavor__disk"))
58
    for vm_res in vm_resources:
59
        user = vm_res['userid']
60
        res = {"vm": vm_res["num"],
61
               "cpu": vm_res["cpu"],
62
               "disk": 1073741824 * vm_res["disk"],
63
               "ram": 1048576 * vm_res["ram"]}
64
        holdings[user] = res
65

    
66
    # Get resources related with networks
67
    net_resources = networks.values("userid")\
68
                                    .annotate(num=Count("id"))
69
    for net_res in net_resources:
70
        user = net_res['userid']
71
        if user not in holdings:
72
            holdings[user] = {}
73
        holdings[user]["network.private"] = net_res["num"]
74

    
75
    return holdings
76

    
77

    
78
def get_quotaholder_holdings(users=[]):
79
    """Get holdings from Quotaholder.
80

81
    If the entity for the user does not exist in quotaholder, no holding
82
    is returned.
83
    """
84
    holdings = {}
85
    with get_quota_holder() as qh:
86
        for user in users:
87
            try:
88
                (qh_holdings, _) = \
89
                    qh.list_holdings(context={}, list_holdings=[(user, "1")])
90
                if not qh_holdings:
91
                    continue
92
                qh_holdings = qh_holdings[0]
93
                qh_holdings = filter(lambda x: x[1].startswith("cyclades."),
94
                                     qh_holdings)
95
                holdings[user] = dict(map(decode_holding, qh_holdings))
96
            except NoEntityError:
97
                pass
98
    return holdings
99

    
100

    
101
def decode_holding(holding):
102
    entity, resource, imported, exported, returned, released = \
103
            holding
104
    res = resource.replace("cyclades.", "")
105
    return (res, imported - exported + returned - released)