Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (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 RegisterException(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
    service_origin = resource_dict.get('service_origin')
52
    if not name or not service_type or not service_origin:
53
        raise RegisterException("Malformed resource dict.")
54

    
55
    try:
56
        Service.objects.get(name=service_origin)
57
    except Service.DoesNotExist:
58
        m = "There is no service %s." % service_origin
59
        raise RegisterException(m)
60

    
61
    try:
62
        r = Resource.objects.get_for_update(name=name)
63
        exists = True
64
        if r.service_type != service_type:
65
            m = ("There already exists a resource named %s with service "
66
                 "type %s." % (name, r.service_type))
67
            raise RegisterException(m)
68
        if r.service_origin != service_origin:
69
            m = ("There already exists a resource named %s registered for "
70
                 "service %s." % (name, r.service_origin))
71
            raise RegisterException(m)
72

    
73
    except Resource.DoesNotExist:
74
        r = Resource(name=name,
75
                     uplimit=0,
76
                     service_type=service_type,
77
                     service_origin=service_origin)
78
        exists = False
79

    
80
    for field in resource_fields:
81
        value = resource_dict.get(field)
82
        if value is not None:
83
            setattr(r, field, value)
84

    
85
    r.save()
86
    if not exists:
87
        qh_sync_new_resource(r, 0)
88

    
89
    if exists:
90
        logger.info("Updated resource %s." % (name))
91
    else:
92
        logger.info("Added resource %s." % (name))
93
    return r, exists
94

    
95

    
96
def update_resource(resource, uplimit):
97
    old_uplimit = resource.uplimit
98
    resource.uplimit = uplimit
99
    resource.save()
100

    
101
    logger.info("Updated resource %s with limit %s."
102
                % (resource.name, uplimit))
103
    diff = uplimit - old_uplimit
104
    if diff != 0:
105
        qh_add_resource_limit(resource, diff)
106

    
107

    
108
def get_resources(resources=None, services=None):
109
    if resources is None:
110
        rs = Resource.objects.all()
111
    else:
112
        rs = Resource.objects.filter(name__in=resources)
113

    
114
    if services is not None:
115
        rs = rs.filter(service__in=services)
116

    
117
    resource_dict = {}
118
    for r in rs:
119
        resource_dict[r.full_name()] = r.get_info()
120

    
121
    return resource_dict
122

    
123

    
124
def add_endpoint(service, endpoint_dict):
125
    endpoint = Endpoint.objects.create(service=service)
126
    for key, value in endpoint_dict.iteritems():
127
        EndpointData.objects.create(
128
            endpoint=endpoint, key=key, value=value)
129

    
130

    
131
def add_service(component, name, service_type, endpoints):
132
    defaults = {'component': component,
133
                'type': service_type,
134
                }
135
    service, created = Service.objects.get_or_create(
136
        name=name, defaults=defaults)
137

    
138
    if not created:
139
        if service.component != component:
140
            m = ("There is already a service named %s registered by %s." %
141
                 (name, service.component.name))
142
            raise RegisterException(m)
143
        service.endpoints.all().delete()
144
        for key, value in defaults.iteritems():
145
            setattr(service, key, value)
146
        service.save()
147

    
148
    for endpoint in endpoints:
149
        add_endpoint(service, endpoint)
150

    
151
    return not created