Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / register.py @ a24b5bda

History | View | Annotate | Download (4.5 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 Resource, Service, Endpoint, EndpointData
35

    
36
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource
37
import logging
38

    
39
logger = logging.getLogger(__name__)
40

    
41
resource_fields = ['desc', 'unit', 'allow_in_projects']
42

    
43

    
44
class ResourceException(Exception):
45
    pass
46

    
47

    
48
def add_resource(resource_dict):
49
    name = resource_dict.get('name')
50
    service_type = resource_dict.get('service_type')
51
    if not name or not service_type:
52
        raise ResourceException("Malformed resource dict.")
53

    
54
    try:
55
        r = Resource.objects.get_for_update(name=name)
56
        exists = True
57
        if r.service_type != service_type:
58
            m = ("There already exists a resource named %s with service "
59
                 "type %s." % (name, r.service_type))
60
            raise ResourceException(m)
61
    except Resource.DoesNotExist:
62
        r = Resource(name=name,
63
                     uplimit=0,
64
                     service_type=service_type)
65
        exists = False
66

    
67
    for field in resource_fields:
68
        value = resource_dict.get(field)
69
        if value is not None:
70
            setattr(r, field, value)
71

    
72
    r.save()
73
    if not exists:
74
        qh_sync_new_resource(r, 0)
75

    
76
    if exists:
77
        logger.info("Updated resource %s." % (name))
78
    else:
79
        logger.info("Added resource %s." % (name))
80
    return r, exists
81

    
82

    
83
def update_resource(resource, uplimit):
84
    old_uplimit = resource.uplimit
85
    resource.uplimit = uplimit
86
    resource.save()
87

    
88
    logger.info("Updated resource %s with limit %s."
89
                % (resource.name, uplimit))
90
    diff = uplimit - old_uplimit
91
    if diff != 0:
92
        qh_add_resource_limit(resource, diff)
93

    
94

    
95
def get_resources(resources=None, services=None):
96
    if resources is None:
97
        rs = Resource.objects.all()
98
    else:
99
        rs = Resource.objects.filter(name__in=resources)
100

    
101
    if services is not None:
102
        rs = rs.filter(service__in=services)
103

    
104
    resource_dict = {}
105
    for r in rs:
106
        resource_dict[r.full_name()] = r.get_info()
107

    
108
    return resource_dict
109

    
110

    
111
def add_endpoint(service, endpoint_dict):
112
    endpoint = Endpoint.objects.create(service=service)
113
    for key, value in endpoint_dict.iteritems():
114
        EndpointData.objects.create(
115
            endpoint=endpoint, key=key, value=value)
116

    
117

    
118
class ServiceException(Exception):
119
    pass
120

    
121

    
122
def add_service(component, name, service_type, endpoints):
123
    defaults = {'component': component,
124
                'type': service_type,
125
                }
126
    service, created = Service.objects.get_or_create(
127
        name=name, defaults=defaults)
128

    
129
    if not created:
130
        if service.component != component:
131
            m = ("There is already a service named %s registered by %s." %
132
                 (name, service.component.name))
133
            raise ServiceException(m)
134
        service.endpoints.all().delete()
135
    else:
136
        service.component = component
137
        service.type = service_type
138
        service.save()
139

    
140
    for endpoint in endpoints:
141
        add_endpoint(service, endpoint)
142

    
143
    return not created