Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / resources.py @ 67cf14bf

History | View | Annotate | Download (3.1 kB)

1 b1ea24f3 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 b1ea24f3 Giorgos Korfiatis
#
3 b1ea24f3 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 b1ea24f3 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 b1ea24f3 Giorgos Korfiatis
# conditions are met:
6 b1ea24f3 Giorgos Korfiatis
#
7 b1ea24f3 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 b1ea24f3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 b1ea24f3 Giorgos Korfiatis
#      disclaimer.
10 b1ea24f3 Giorgos Korfiatis
#
11 b1ea24f3 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 b1ea24f3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 b1ea24f3 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 b1ea24f3 Giorgos Korfiatis
#      provided with the distribution.
15 b1ea24f3 Giorgos Korfiatis
#
16 b1ea24f3 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 b1ea24f3 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 b1ea24f3 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 b1ea24f3 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 b1ea24f3 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 b1ea24f3 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 b1ea24f3 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 b1ea24f3 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 b1ea24f3 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 b1ea24f3 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 b1ea24f3 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 b1ea24f3 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 b1ea24f3 Giorgos Korfiatis
#
29 b1ea24f3 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 b1ea24f3 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 b1ea24f3 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 b1ea24f3 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 b1ea24f3 Giorgos Korfiatis
34 3a527b3a Giorgos Korfiatis
from astakos.im.models import Resource
35 9747707e Giorgos Korfiatis
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource
36 9747707e Giorgos Korfiatis
import logging
37 b1ea24f3 Giorgos Korfiatis
38 9747707e Giorgos Korfiatis
logger = logging.getLogger(__name__)
39 b1ea24f3 Giorgos Korfiatis
40 c8618788 Giorgos Korfiatis
fields = ['name', 'desc', 'unit', 'allow_in_projects']
41 9747707e Giorgos Korfiatis
42 b1ea24f3 Giorgos Korfiatis
43 3a527b3a Giorgos Korfiatis
class ResourceException(Exception):
44 3a527b3a Giorgos Korfiatis
    pass
45 3a527b3a Giorgos Korfiatis
46 3a527b3a Giorgos Korfiatis
47 3a527b3a Giorgos Korfiatis
def add_resource(service, resource_dict):
48 3a527b3a Giorgos Korfiatis
    name = resource_dict.get('name')
49 3a527b3a Giorgos Korfiatis
    if not name:
50 3a527b3a Giorgos Korfiatis
        raise ResourceException("Malformed resource dict.")
51 3a527b3a Giorgos Korfiatis
52 9747707e Giorgos Korfiatis
    try:
53 9747707e Giorgos Korfiatis
        r = Resource.objects.get_for_update(name=name)
54 3a527b3a Giorgos Korfiatis
        exists = True
55 9747707e Giorgos Korfiatis
    except Resource.DoesNotExist:
56 3a527b3a Giorgos Korfiatis
        r = Resource(uplimit=0)
57 3a527b3a Giorgos Korfiatis
        exists = False
58 b1ea24f3 Giorgos Korfiatis
59 3a527b3a Giorgos Korfiatis
    r.service = service
60 3a527b3a Giorgos Korfiatis
    for field in fields:
61 3a527b3a Giorgos Korfiatis
        value = resource_dict.get(field)
62 3a527b3a Giorgos Korfiatis
        if value is not None:
63 3a527b3a Giorgos Korfiatis
            setattr(r, field, value)
64 b1ea24f3 Giorgos Korfiatis
65 9747707e Giorgos Korfiatis
    r.save()
66 3a527b3a Giorgos Korfiatis
    if not exists:
67 3a527b3a Giorgos Korfiatis
        qh_sync_new_resource(r, 0)
68 b1ea24f3 Giorgos Korfiatis
69 3a527b3a Giorgos Korfiatis
    if exists:
70 3a527b3a Giorgos Korfiatis
        logger.info("Updated resource %s." % (name))
71 9747707e Giorgos Korfiatis
    else:
72 3a527b3a Giorgos Korfiatis
        logger.info("Added resource %s." % (name))
73 73c02f75 Giorgos Korfiatis
    return r, exists
74 720955ff Giorgos Korfiatis
75 720955ff Giorgos Korfiatis
76 3a527b3a Giorgos Korfiatis
def update_resource(resource, uplimit):
77 3a527b3a Giorgos Korfiatis
    old_uplimit = resource.uplimit
78 3a527b3a Giorgos Korfiatis
    resource.uplimit = uplimit
79 3a527b3a Giorgos Korfiatis
    resource.save()
80 720955ff Giorgos Korfiatis
81 3a527b3a Giorgos Korfiatis
    logger.info("Updated resource %s with limit %s."
82 3a527b3a Giorgos Korfiatis
                % (resource.name, uplimit))
83 720955ff Giorgos Korfiatis
    diff = uplimit - old_uplimit
84 720955ff Giorgos Korfiatis
    if diff != 0:
85 3a527b3a Giorgos Korfiatis
        qh_add_resource_limit(resource, diff)
86 08043775 Giorgos Korfiatis
87 08043775 Giorgos Korfiatis
88 08043775 Giorgos Korfiatis
def get_resources(resources=None, services=None):
89 08043775 Giorgos Korfiatis
    if resources is None:
90 08043775 Giorgos Korfiatis
        rs = Resource.objects.all()
91 08043775 Giorgos Korfiatis
    else:
92 08043775 Giorgos Korfiatis
        rs = Resource.objects.filter(name__in=resources)
93 08043775 Giorgos Korfiatis
94 08043775 Giorgos Korfiatis
    if services is not None:
95 08043775 Giorgos Korfiatis
        rs = rs.filter(service__in=services)
96 08043775 Giorgos Korfiatis
97 08043775 Giorgos Korfiatis
    resource_dict = {}
98 08043775 Giorgos Korfiatis
    for r in rs:
99 08043775 Giorgos Korfiatis
        resource_dict[r.full_name()] = r.get_info()
100 08043775 Giorgos Korfiatis
101 08043775 Giorgos Korfiatis
    return resource_dict