Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.5 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 bd1f667b Giorgos Korfiatis
from astakos.im.models import Resource, Service, Endpoint, EndpointData
35 bd1f667b Giorgos Korfiatis
36 9747707e Giorgos Korfiatis
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource
37 9747707e Giorgos Korfiatis
import logging
38 b1ea24f3 Giorgos Korfiatis
39 9747707e Giorgos Korfiatis
logger = logging.getLogger(__name__)
40 b1ea24f3 Giorgos Korfiatis
41 bd1f667b Giorgos Korfiatis
resource_fields = ['desc', 'unit', 'allow_in_projects']
42 9747707e Giorgos Korfiatis
43 b1ea24f3 Giorgos Korfiatis
44 3a527b3a Giorgos Korfiatis
class ResourceException(Exception):
45 3a527b3a Giorgos Korfiatis
    pass
46 3a527b3a Giorgos Korfiatis
47 3a527b3a Giorgos Korfiatis
48 bd1f667b Giorgos Korfiatis
def add_resource(resource_dict):
49 3a527b3a Giorgos Korfiatis
    name = resource_dict.get('name')
50 bd1f667b Giorgos Korfiatis
    service_type = resource_dict.get('service_type')
51 bd1f667b Giorgos Korfiatis
    if not name or not service_type:
52 3a527b3a Giorgos Korfiatis
        raise ResourceException("Malformed resource dict.")
53 3a527b3a Giorgos Korfiatis
54 9747707e Giorgos Korfiatis
    try:
55 9747707e Giorgos Korfiatis
        r = Resource.objects.get_for_update(name=name)
56 3a527b3a Giorgos Korfiatis
        exists = True
57 bd1f667b Giorgos Korfiatis
        if r.service_type != service_type:
58 bd1f667b Giorgos Korfiatis
            m = ("There already exists a resource named %s with service "
59 bd1f667b Giorgos Korfiatis
                 "type %s." % (name, r.service_type))
60 bd1f667b Giorgos Korfiatis
            raise ResourceException(m)
61 9747707e Giorgos Korfiatis
    except Resource.DoesNotExist:
62 bd1f667b Giorgos Korfiatis
        r = Resource(name=name,
63 bd1f667b Giorgos Korfiatis
                     uplimit=0,
64 bd1f667b Giorgos Korfiatis
                     service_type=service_type)
65 3a527b3a Giorgos Korfiatis
        exists = False
66 b1ea24f3 Giorgos Korfiatis
67 bd1f667b Giorgos Korfiatis
    for field in resource_fields:
68 3a527b3a Giorgos Korfiatis
        value = resource_dict.get(field)
69 3a527b3a Giorgos Korfiatis
        if value is not None:
70 3a527b3a Giorgos Korfiatis
            setattr(r, field, value)
71 b1ea24f3 Giorgos Korfiatis
72 9747707e Giorgos Korfiatis
    r.save()
73 3a527b3a Giorgos Korfiatis
    if not exists:
74 3a527b3a Giorgos Korfiatis
        qh_sync_new_resource(r, 0)
75 b1ea24f3 Giorgos Korfiatis
76 3a527b3a Giorgos Korfiatis
    if exists:
77 3a527b3a Giorgos Korfiatis
        logger.info("Updated resource %s." % (name))
78 9747707e Giorgos Korfiatis
    else:
79 3a527b3a Giorgos Korfiatis
        logger.info("Added resource %s." % (name))
80 73c02f75 Giorgos Korfiatis
    return r, exists
81 720955ff Giorgos Korfiatis
82 720955ff Giorgos Korfiatis
83 3a527b3a Giorgos Korfiatis
def update_resource(resource, uplimit):
84 3a527b3a Giorgos Korfiatis
    old_uplimit = resource.uplimit
85 3a527b3a Giorgos Korfiatis
    resource.uplimit = uplimit
86 3a527b3a Giorgos Korfiatis
    resource.save()
87 720955ff Giorgos Korfiatis
88 3a527b3a Giorgos Korfiatis
    logger.info("Updated resource %s with limit %s."
89 3a527b3a Giorgos Korfiatis
                % (resource.name, uplimit))
90 720955ff Giorgos Korfiatis
    diff = uplimit - old_uplimit
91 720955ff Giorgos Korfiatis
    if diff != 0:
92 3a527b3a Giorgos Korfiatis
        qh_add_resource_limit(resource, diff)
93 08043775 Giorgos Korfiatis
94 08043775 Giorgos Korfiatis
95 08043775 Giorgos Korfiatis
def get_resources(resources=None, services=None):
96 08043775 Giorgos Korfiatis
    if resources is None:
97 08043775 Giorgos Korfiatis
        rs = Resource.objects.all()
98 08043775 Giorgos Korfiatis
    else:
99 08043775 Giorgos Korfiatis
        rs = Resource.objects.filter(name__in=resources)
100 08043775 Giorgos Korfiatis
101 08043775 Giorgos Korfiatis
    if services is not None:
102 08043775 Giorgos Korfiatis
        rs = rs.filter(service__in=services)
103 08043775 Giorgos Korfiatis
104 08043775 Giorgos Korfiatis
    resource_dict = {}
105 08043775 Giorgos Korfiatis
    for r in rs:
106 08043775 Giorgos Korfiatis
        resource_dict[r.full_name()] = r.get_info()
107 08043775 Giorgos Korfiatis
108 08043775 Giorgos Korfiatis
    return resource_dict
109 bd1f667b Giorgos Korfiatis
110 bd1f667b Giorgos Korfiatis
111 bd1f667b Giorgos Korfiatis
def add_endpoint(service, endpoint_dict):
112 bd1f667b Giorgos Korfiatis
    endpoint = Endpoint.objects.create(service=service)
113 bd1f667b Giorgos Korfiatis
    for key, value in endpoint_dict.iteritems():
114 bd1f667b Giorgos Korfiatis
        EndpointData.objects.create(
115 bd1f667b Giorgos Korfiatis
            endpoint=endpoint, key=key, value=value)
116 bd1f667b Giorgos Korfiatis
117 bd1f667b Giorgos Korfiatis
118 bd1f667b Giorgos Korfiatis
class ServiceException(Exception):
119 bd1f667b Giorgos Korfiatis
    pass
120 bd1f667b Giorgos Korfiatis
121 bd1f667b Giorgos Korfiatis
122 bd1f667b Giorgos Korfiatis
def add_service(component, name, service_type, endpoints):
123 bd1f667b Giorgos Korfiatis
    defaults = {'component': component,
124 bd1f667b Giorgos Korfiatis
                'type': service_type,
125 bd1f667b Giorgos Korfiatis
                }
126 bd1f667b Giorgos Korfiatis
    service, created = Service.objects.get_or_create(
127 bd1f667b Giorgos Korfiatis
        name=name, defaults=defaults)
128 bd1f667b Giorgos Korfiatis
129 bd1f667b Giorgos Korfiatis
    if not created:
130 bd1f667b Giorgos Korfiatis
        if service.component != component:
131 bd1f667b Giorgos Korfiatis
            m = ("There is already a service named %s registered by %s." %
132 bd1f667b Giorgos Korfiatis
                 (name, service.component.name))
133 bd1f667b Giorgos Korfiatis
            raise ServiceException(m)
134 bd1f667b Giorgos Korfiatis
        service.endpoints.all().delete()
135 bd1f667b Giorgos Korfiatis
    else:
136 bd1f667b Giorgos Korfiatis
        service.component = component
137 bd1f667b Giorgos Korfiatis
        service.type = service_type
138 bd1f667b Giorgos Korfiatis
        service.save()
139 bd1f667b Giorgos Korfiatis
140 bd1f667b Giorgos Korfiatis
    for endpoint in endpoints:
141 bd1f667b Giorgos Korfiatis
        add_endpoint(service, endpoint)
142 bd1f667b Giorgos Korfiatis
143 bd1f667b Giorgos Korfiatis
    return not created