Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / resources.py @ 08043775

History | View | Annotate | Download (3.3 kB)

1
# Copyright 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 astakos.im.models import Service, Resource
35
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource
36
import logging
37

    
38
logger = logging.getLogger(__name__)
39

    
40

    
41
def add_resource(service, resource, uplimit):
42
    try:
43
        s = Service.objects.get(name=service)
44
    except Service.DoesNotExist:
45
        raise Exception("Service %s is not registered." % (service))
46

    
47
    name = resource['name']
48
    try:
49
        r = Resource.objects.get_for_update(name=name)
50
        old_uplimit = r.uplimit
51
    except Resource.DoesNotExist:
52
        r = Resource()
53
        old_uplimit = None
54

    
55
    r.uplimit = uplimit
56
    r.service = service
57
    for key, value in resource.iteritems():
58
        setattr(r, key, value)
59

    
60
    r.save()
61

    
62
    if old_uplimit is not None:
63
        logger.info("Updated resource %s with limit %s." % (name, uplimit))
64
    else:
65
        logger.info("Added resource %s with limit %s." % (name, uplimit))
66

    
67
    if old_uplimit is not None:
68
        diff = uplimit - old_uplimit
69
        if diff != 0:
70
            qh_add_resource_limit(r, diff)
71
    else:
72
        qh_sync_new_resource(r, uplimit)
73

    
74

    
75
def update_resource(name, uplimit):
76
    r = Resource.objects.get_for_update(name=name)
77
    old_uplimit = r.uplimit
78
    r.uplimit = uplimit
79
    r.save()
80

    
81
    logger.info("Updated resource %s with limit %s." % (name, uplimit))
82
    diff = uplimit - old_uplimit
83
    if diff != 0:
84
        qh_add_resource_limit(r, diff)
85

    
86

    
87
def get_resources(resources=None, services=None):
88
    if resources is None:
89
        rs = Resource.objects.all()
90
    else:
91
        rs = Resource.objects.filter(name__in=resources)
92

    
93
    if services is not None:
94
        rs = rs.filter(service__in=services)
95

    
96
    resource_dict = {}
97
    for r in rs:
98
        resource_dict[r.full_name()] = r.get_info()
99

    
100
    return resource_dict