Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / register.py @ 2e46be99

History | View | Annotate | Download (6 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 synnefo.util import units
35
from astakos.im.models import Resource, Service, Endpoint, EndpointData
36
from astakos.im import quotas
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 different_component(service, resource):
49
    try:
50
        registered_for = Service.objects.get(name=resource.service_origin)
51
        return registered_for.component != service.component
52
    except Service.DoesNotExist:
53
        return False
54

    
55

    
56
def add_resource(resource_dict):
57
    name = resource_dict.get('name')
58
    service_type = resource_dict.get('service_type')
59
    service_origin = resource_dict.get('service_origin')
60
    if not name or not service_type or not service_origin:
61
        raise RegisterException("Malformed resource dict.")
62

    
63
    try:
64
        service = Service.objects.get(name=service_origin)
65
    except Service.DoesNotExist:
66
        m = "There is no service %s." % service_origin
67
        raise RegisterException(m)
68

    
69
    try:
70
        r = Resource.objects.select_for_update().get(name=name)
71
        exists = True
72
        if r.service_type != service_type and \
73
                different_component(service, r):
74
            m = ("There already exists a resource named %s with service "
75
                 "type %s." % (name, r.service_type))
76
            raise RegisterException(m)
77
        if r.service_origin != service_origin and \
78
                different_component(service, r):
79
            m = ("There already exists a resource named %s registered for "
80
                 "service %s." % (name, r.service_origin))
81
            raise RegisterException(m)
82
        r.service_origin = service_origin
83
        r.service_type = service_type
84
    except Resource.DoesNotExist:
85
        r = Resource(name=name,
86
                     uplimit=units.PRACTICALLY_INFINITE,
87
                     service_type=service_type,
88
                     service_origin=service_origin)
89
        exists = False
90

    
91
    for field in resource_fields:
92
        value = resource_dict.get(field)
93
        if value is not None:
94
            setattr(r, field, value)
95

    
96
    r.save()
97
    if not exists:
98
        quotas.qh_sync_new_resource(r)
99

    
100
    if exists:
101
        logger.info("Updated resource %s." % (name))
102
    else:
103
        logger.info("Added resource %s." % (name))
104
    return r, exists
105

    
106

    
107
def update_resource(resource, uplimit):
108
    old_uplimit = resource.uplimit
109
    if uplimit == old_uplimit:
110
        logger.info("Resource %s has limit %s; no need to update."
111
                    % (resource.name, uplimit))
112
        return []
113
    else:
114
        resource.uplimit = uplimit
115
        resource.save()
116
        logger.info("Updated resource %s with limit %s."
117
                    % (resource.name, uplimit))
118
        affected = quotas.qh_change_resource_limit(resource)
119
        return affected
120

    
121

    
122
def get_resources(resources=None, services=None):
123
    if resources is None:
124
        rs = Resource.objects.all()
125
    else:
126
        rs = Resource.objects.filter(name__in=resources)
127

    
128
    if services is not None:
129
        rs = rs.filter(service__in=services)
130

    
131
    resource_dict = {}
132
    for r in rs:
133
        resource_dict[r.full_name()] = r.get_info()
134

    
135
    return resource_dict
136

    
137

    
138
def add_endpoint(component, service, endpoint_dict, out=None):
139
    endpoint = Endpoint.objects.create(service=service)
140
    for key, value in endpoint_dict.iteritems():
141
        base_url = component.base_url
142
        if key == "publicURL" and (base_url is None or
143
                                   not value.startswith(base_url)):
144
            warn = out.write if out is not None else logger.warning
145
            warn("Warning: Endpoint URL '%s' does not start with "
146
                 "assumed component base URL '%s'.\n" % (value, base_url))
147
        EndpointData.objects.create(
148
            endpoint=endpoint, key=key, value=value)
149

    
150

    
151
def add_service(component, name, service_type, endpoints, out=None):
152
    defaults = {'component': component,
153
                'type': service_type,
154
                }
155
    service, created = Service.objects.get_or_create(
156
        name=name, defaults=defaults)
157

    
158
    if not created:
159
        if service.component != component:
160
            m = ("There is already a service named %s registered by %s." %
161
                 (name, service.component.name))
162
            raise RegisterException(m)
163
        service.endpoints.all().delete()
164
        for key, value in defaults.iteritems():
165
            setattr(service, key, value)
166
        service.save()
167

    
168
    for endpoint in endpoints:
169
        add_endpoint(component, service, endpoint, out=out)
170

    
171
    return not created