Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7 kB)

1
# Copyright 2013-2014 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
main_fields = ['desc', 'unit']
42
config_fields = ['ui_visible', 'api_visible']
43

    
44

    
45
class RegisterException(Exception):
46
    pass
47

    
48

    
49
def different_component(service, resource):
50
    try:
51
        registered_for = Service.objects.get(name=resource.service_origin)
52
        return registered_for.component != service.component
53
    except Service.DoesNotExist:
54
        return False
55

    
56

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

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

    
70
    try:
71
        r = Resource.objects.select_for_update().get(name=name)
72
        exists = True
73
        if r.service_type != service_type and \
74
                different_component(service, r):
75
            m = ("There already exists a resource named %s with service "
76
                 "type %s." % (name, r.service_type))
77
            raise RegisterException(m)
78
        if r.service_origin != service_origin and \
79
                different_component(service, r):
80
            m = ("There already exists a resource named %s registered for "
81
                 "service %s." % (name, r.service_origin))
82
            raise RegisterException(m)
83
        r.service_origin = service_origin
84
        r.service_type = service_type
85
    except Resource.DoesNotExist:
86
        r = Resource(name=name,
87
                     uplimit=units.PRACTICALLY_INFINITE,
88
                     project_default=units.PRACTICALLY_INFINITE,
89
                     service_type=service_type,
90
                     service_origin=service_origin)
91
        exists = False
92
        for field in config_fields:
93
            value = resource_dict.get(field)
94
            if value is not None:
95
                setattr(r, field, value)
96

    
97
    for field in main_fields:
98
        value = resource_dict.get(field)
99
        if value is not None:
100
            setattr(r, field, value)
101

    
102
    if r.ui_visible and not r.api_visible:
103
        m = "Flag 'ui_visible' should entail 'api_visible'."
104
        raise RegisterException(m)
105

    
106
    r.save()
107
    if not exists:
108
        quotas.qh_sync_new_resource(r)
109

    
110
    if exists:
111
        logger.info("Updated resource %s." % (name))
112
    else:
113
        logger.info("Added resource %s." % (name))
114
    return r, exists
115

    
116

    
117
def update_base_default(resource, base_default):
118
    old_base_default = resource.uplimit
119
    if base_default == old_base_default:
120
        logger.info("Resource %s has base default %s; no need to update."
121
                    % (resource.name, base_default))
122
    else:
123
        resource.uplimit = base_default
124
        resource.save()
125
        logger.info("Updated resource %s with base default %s."
126
                    % (resource.name, base_default))
127

    
128

    
129
def update_project_default(resource, project_default):
130
    old_project_default = resource.project_default
131
    if project_default == old_project_default:
132
        logger.info("Resource %s has project default %s; no need to update."
133
                    % (resource.name, project_default))
134
    else:
135
        resource.project_default = project_default
136
        resource.save()
137
        logger.info("Updated resource %s with project default %s."
138
                    % (resource.name, project_default))
139

    
140

    
141
def resources_to_dict(resources):
142
    resource_dict = {}
143
    for r in resources:
144
        resource_dict[r.name] = r.get_info()
145
    return resource_dict
146

    
147

    
148
def get_resources(resources=None, services=None):
149
    if resources is None:
150
        rs = Resource.objects.all()
151
    else:
152
        rs = Resource.objects.filter(name__in=resources)
153

    
154
    if services is not None:
155
        rs = rs.filter(service__in=services)
156

    
157
    return rs
158

    
159

    
160
def get_api_visible_resources(resources=None, services=None):
161
    rs = get_resources(resources, services)
162
    return rs.filter(api_visible=True)
163

    
164

    
165
def add_endpoint(component, service, endpoint_dict, out=None):
166
    endpoint = Endpoint.objects.create(service=service)
167
    for key, value in endpoint_dict.iteritems():
168
        base_url = component.base_url
169
        if key == "publicURL" and (base_url is None or
170
                                   not value.startswith(base_url)):
171
            warn = out.write if out is not None else logger.warning
172
            warn("Warning: Endpoint URL '%s' does not start with "
173
                 "assumed component base URL '%s'.\n" % (value, base_url))
174
        EndpointData.objects.create(
175
            endpoint=endpoint, key=key, value=value)
176

    
177

    
178
def add_service(component, name, service_type, endpoints, out=None):
179
    defaults = {'component': component,
180
                'type': service_type,
181
                }
182
    service, created = Service.objects.get_or_create(
183
        name=name, defaults=defaults)
184

    
185
    if not created:
186
        if service.component != component:
187
            m = ("There is already a service named %s registered by %s." %
188
                 (name, service.component.name))
189
            raise RegisterException(m)
190
        service.endpoints.all().delete()
191
        for key, value in defaults.iteritems():
192
            setattr(service, key, value)
193
        service.save()
194

    
195
    for endpoint in endpoints:
196
        add_endpoint(component, service, endpoint, out=out)
197

    
198
    return not created