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