root / snf-astakos-app / astakos / im / register.py @ 85ae5a4c
History | View | Annotate | Download (6.4 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 | 2e46be99 | Giorgos Korfiatis | from synnefo.util import units |
35 | bd1f667b | Giorgos Korfiatis | from astakos.im.models import Resource, Service, Endpoint, EndpointData |
36 | 30edd93d | Giorgos Korfiatis | from astakos.im import quotas |
37 | 9747707e | Giorgos Korfiatis | import logging |
38 | b1ea24f3 | Giorgos Korfiatis | |
39 | 9747707e | Giorgos Korfiatis | logger = logging.getLogger(__name__) |
40 | b1ea24f3 | Giorgos Korfiatis | |
41 | 2dc27ac1 | Giorgos Korfiatis | resource_fields = ['desc', 'unit', 'ui_visible', 'api_visible'] |
42 | 9747707e | Giorgos Korfiatis | |
43 | b1ea24f3 | Giorgos Korfiatis | |
44 | 38807757 | Giorgos Korfiatis | class RegisterException(Exception): |
45 | 3a527b3a | Giorgos Korfiatis | pass
|
46 | 3a527b3a | Giorgos Korfiatis | |
47 | 3a527b3a | Giorgos Korfiatis | |
48 | ccc5048f | Giorgos Korfiatis | def different_component(service, resource): |
49 | ccc5048f | Giorgos Korfiatis | try:
|
50 | ccc5048f | Giorgos Korfiatis | registered_for = Service.objects.get(name=resource.service_origin) |
51 | ccc5048f | Giorgos Korfiatis | return registered_for.component != service.component
|
52 | ccc5048f | Giorgos Korfiatis | except Service.DoesNotExist:
|
53 | ccc5048f | Giorgos Korfiatis | return False |
54 | ccc5048f | Giorgos Korfiatis | |
55 | ccc5048f | Giorgos Korfiatis | |
56 | bd1f667b | Giorgos Korfiatis | def add_resource(resource_dict): |
57 | 3a527b3a | Giorgos Korfiatis | name = resource_dict.get('name')
|
58 | bd1f667b | Giorgos Korfiatis | service_type = resource_dict.get('service_type')
|
59 | 0e08e08e | Giorgos Korfiatis | service_origin = resource_dict.get('service_origin')
|
60 | 0e08e08e | Giorgos Korfiatis | if not name or not service_type or not service_origin: |
61 | 38807757 | Giorgos Korfiatis | raise RegisterException("Malformed resource dict.") |
62 | 3a527b3a | Giorgos Korfiatis | |
63 | 9747707e | Giorgos Korfiatis | try:
|
64 | ccc5048f | Giorgos Korfiatis | service = Service.objects.get(name=service_origin) |
65 | 0e08e08e | Giorgos Korfiatis | except Service.DoesNotExist:
|
66 | 0e08e08e | Giorgos Korfiatis | m = "There is no service %s." % service_origin
|
67 | 38807757 | Giorgos Korfiatis | raise RegisterException(m)
|
68 | 0e08e08e | Giorgos Korfiatis | |
69 | 0e08e08e | Giorgos Korfiatis | try:
|
70 | c7e03d20 | Giorgos Korfiatis | r = Resource.objects.select_for_update().get(name=name) |
71 | 3a527b3a | Giorgos Korfiatis | exists = True
|
72 | ccc5048f | Giorgos Korfiatis | if r.service_type != service_type and \ |
73 | ccc5048f | Giorgos Korfiatis | different_component(service, r): |
74 | bd1f667b | Giorgos Korfiatis | m = ("There already exists a resource named %s with service "
|
75 | bd1f667b | Giorgos Korfiatis | "type %s." % (name, r.service_type))
|
76 | 38807757 | Giorgos Korfiatis | raise RegisterException(m)
|
77 | ccc5048f | Giorgos Korfiatis | if r.service_origin != service_origin and \ |
78 | ccc5048f | Giorgos Korfiatis | different_component(service, r): |
79 | 0e08e08e | Giorgos Korfiatis | m = ("There already exists a resource named %s registered for "
|
80 | 0e08e08e | Giorgos Korfiatis | "service %s." % (name, r.service_origin))
|
81 | 38807757 | Giorgos Korfiatis | raise RegisterException(m)
|
82 | ccc5048f | Giorgos Korfiatis | r.service_origin = service_origin |
83 | ccc5048f | Giorgos Korfiatis | r.service_type = service_type |
84 | 9747707e | Giorgos Korfiatis | except Resource.DoesNotExist:
|
85 | bd1f667b | Giorgos Korfiatis | r = Resource(name=name, |
86 | 2e46be99 | Giorgos Korfiatis | uplimit=units.PRACTICALLY_INFINITE, |
87 | 0e08e08e | Giorgos Korfiatis | service_type=service_type, |
88 | 0e08e08e | Giorgos Korfiatis | service_origin=service_origin) |
89 | 3a527b3a | Giorgos Korfiatis | exists = False
|
90 | b1ea24f3 | Giorgos Korfiatis | |
91 | bd1f667b | Giorgos Korfiatis | for field in resource_fields: |
92 | 3a527b3a | Giorgos Korfiatis | value = resource_dict.get(field) |
93 | 3a527b3a | Giorgos Korfiatis | if value is not None: |
94 | 3a527b3a | Giorgos Korfiatis | setattr(r, field, value)
|
95 | b1ea24f3 | Giorgos Korfiatis | |
96 | 2dc27ac1 | Giorgos Korfiatis | if r.ui_visible and not r.api_visible: |
97 | 2dc27ac1 | Giorgos Korfiatis | m = "Flag 'ui_visible' should entail 'api_visible'."
|
98 | 2dc27ac1 | Giorgos Korfiatis | raise RegisterException(m)
|
99 | 2dc27ac1 | Giorgos Korfiatis | |
100 | 9747707e | Giorgos Korfiatis | r.save() |
101 | 3a527b3a | Giorgos Korfiatis | if not exists: |
102 | 2e46be99 | Giorgos Korfiatis | quotas.qh_sync_new_resource(r) |
103 | b1ea24f3 | Giorgos Korfiatis | |
104 | 3a527b3a | Giorgos Korfiatis | if exists:
|
105 | 3a527b3a | Giorgos Korfiatis | logger.info("Updated resource %s." % (name))
|
106 | 9747707e | Giorgos Korfiatis | else:
|
107 | 3a527b3a | Giorgos Korfiatis | logger.info("Added resource %s." % (name))
|
108 | 73c02f75 | Giorgos Korfiatis | return r, exists
|
109 | 720955ff | Giorgos Korfiatis | |
110 | 720955ff | Giorgos Korfiatis | |
111 | f72ba65d | Giorgos Korfiatis | def update_resources(updates): |
112 | f72ba65d | Giorgos Korfiatis | resources = [] |
113 | f72ba65d | Giorgos Korfiatis | for resource, uplimit in updates: |
114 | f72ba65d | Giorgos Korfiatis | resources.append(resource) |
115 | f72ba65d | Giorgos Korfiatis | old_uplimit = resource.uplimit |
116 | f72ba65d | Giorgos Korfiatis | if uplimit == old_uplimit:
|
117 | f72ba65d | Giorgos Korfiatis | logger.info("Resource %s has limit %s; no need to update."
|
118 | f72ba65d | Giorgos Korfiatis | % (resource.name, uplimit)) |
119 | f72ba65d | Giorgos Korfiatis | else:
|
120 | f72ba65d | Giorgos Korfiatis | resource.uplimit = uplimit |
121 | f72ba65d | Giorgos Korfiatis | resource.save() |
122 | f72ba65d | Giorgos Korfiatis | logger.info("Updated resource %s with limit %s."
|
123 | f72ba65d | Giorgos Korfiatis | % (resource.name, uplimit)) |
124 | 08043775 | Giorgos Korfiatis | |
125 | 08043775 | Giorgos Korfiatis | |
126 | 85ae5a4c | Giorgos Korfiatis | def resources_to_dict(resources): |
127 | 85ae5a4c | Giorgos Korfiatis | resource_dict = {} |
128 | 85ae5a4c | Giorgos Korfiatis | for r in resources: |
129 | 85ae5a4c | Giorgos Korfiatis | resource_dict[r.name] = r.get_info() |
130 | 85ae5a4c | Giorgos Korfiatis | return resource_dict
|
131 | 85ae5a4c | Giorgos Korfiatis | |
132 | 85ae5a4c | Giorgos Korfiatis | |
133 | 08043775 | Giorgos Korfiatis | def get_resources(resources=None, services=None): |
134 | 08043775 | Giorgos Korfiatis | if resources is None: |
135 | 08043775 | Giorgos Korfiatis | rs = Resource.objects.all() |
136 | 08043775 | Giorgos Korfiatis | else:
|
137 | 08043775 | Giorgos Korfiatis | rs = Resource.objects.filter(name__in=resources) |
138 | 08043775 | Giorgos Korfiatis | |
139 | 08043775 | Giorgos Korfiatis | if services is not None: |
140 | 08043775 | Giorgos Korfiatis | rs = rs.filter(service__in=services) |
141 | 08043775 | Giorgos Korfiatis | |
142 | 85ae5a4c | Giorgos Korfiatis | return rs
|
143 | 08043775 | Giorgos Korfiatis | |
144 | 85ae5a4c | Giorgos Korfiatis | |
145 | 85ae5a4c | Giorgos Korfiatis | def get_api_visible_resources(resources=None, services=None): |
146 | 85ae5a4c | Giorgos Korfiatis | rs = get_resources(resources, services) |
147 | 85ae5a4c | Giorgos Korfiatis | return rs.filter(api_visible=True) |
148 | bd1f667b | Giorgos Korfiatis | |
149 | bd1f667b | Giorgos Korfiatis | |
150 | eb765213 | Giorgos Korfiatis | def add_endpoint(component, service, endpoint_dict, out=None): |
151 | bd1f667b | Giorgos Korfiatis | endpoint = Endpoint.objects.create(service=service) |
152 | bd1f667b | Giorgos Korfiatis | for key, value in endpoint_dict.iteritems(): |
153 | eb765213 | Giorgos Korfiatis | base_url = component.base_url |
154 | 6f8a3e6a | Giorgos Korfiatis | if key == "publicURL" and (base_url is None or |
155 | 6f8a3e6a | Giorgos Korfiatis | not value.startswith(base_url)):
|
156 | eb765213 | Giorgos Korfiatis | warn = out.write if out is not None else logger.warning |
157 | eb765213 | Giorgos Korfiatis | warn("Warning: Endpoint URL '%s' does not start with "
|
158 | eb765213 | Giorgos Korfiatis | "assumed component base URL '%s'.\n" % (value, base_url))
|
159 | bd1f667b | Giorgos Korfiatis | EndpointData.objects.create( |
160 | bd1f667b | Giorgos Korfiatis | endpoint=endpoint, key=key, value=value) |
161 | bd1f667b | Giorgos Korfiatis | |
162 | bd1f667b | Giorgos Korfiatis | |
163 | eb765213 | Giorgos Korfiatis | def add_service(component, name, service_type, endpoints, out=None): |
164 | bd1f667b | Giorgos Korfiatis | defaults = {'component': component,
|
165 | bd1f667b | Giorgos Korfiatis | 'type': service_type,
|
166 | bd1f667b | Giorgos Korfiatis | } |
167 | bd1f667b | Giorgos Korfiatis | service, created = Service.objects.get_or_create( |
168 | bd1f667b | Giorgos Korfiatis | name=name, defaults=defaults) |
169 | bd1f667b | Giorgos Korfiatis | |
170 | bd1f667b | Giorgos Korfiatis | if not created: |
171 | bd1f667b | Giorgos Korfiatis | if service.component != component:
|
172 | bd1f667b | Giorgos Korfiatis | m = ("There is already a service named %s registered by %s." %
|
173 | bd1f667b | Giorgos Korfiatis | (name, service.component.name)) |
174 | 38807757 | Giorgos Korfiatis | raise RegisterException(m)
|
175 | bd1f667b | Giorgos Korfiatis | service.endpoints.all().delete() |
176 | 51356707 | Giorgos Korfiatis | for key, value in defaults.iteritems(): |
177 | 51356707 | Giorgos Korfiatis | setattr(service, key, value)
|
178 | bd1f667b | Giorgos Korfiatis | service.save() |
179 | bd1f667b | Giorgos Korfiatis | |
180 | bd1f667b | Giorgos Korfiatis | for endpoint in endpoints: |
181 | eb765213 | Giorgos Korfiatis | add_endpoint(component, service, endpoint, out=out) |
182 | bd1f667b | Giorgos Korfiatis | |
183 | bd1f667b | Giorgos Korfiatis | return not created |