cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
parsing.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.utils.parsing
22 #
23 # @author Krzysztof Danielowski
24 # @author Piotr W√≥jcik
25 # @date 26.11.2010
26 #
27 
28 from django.utils.translation import ugettext as _
29 
30 from wi.commontags.templatetags.templatetags import filesizeformatmb
31 from wi.utils.states import image_states, image_platforms_reversed, ec2names_reversed
32 
33 
34 ##
35 #
36 # Returns list of the Images names grouped by type.
37 #
38 # @parameter{data,dict} dictionary with keys: @val{images_private}, @val{images_public}, @val{images_group}
39 #
40 # @returns{list} Images names.
41 #
43  emty_category_counter = 1
44  categories = []
45 
46  categories.append([_(' '), [[-emty_category_counter, _('< Select >')]]])
47  emty_category_counter += 1
48 
49  sub_categories = []
50 
51  for image in data['images_public']:
52  if isinstance(image, dict) and image['state'] == image_states['ok']:
53  sub_categories.append([image['image_id'], image['name']])
54  if not sub_categories:
55  sub_categories.append([-emty_category_counter, {'label': _('No images'), 'disabled': True}])
56  emty_category_counter += 1
57  public_category = [_('Public images'), sub_categories]
58  categories.append(public_category)
59 
60  sub_categories = []
61 
62  for image in data['images_private']:
63  if isinstance(image, dict) and image['state'] == image_states['ok']:
64  sub_categories.append([image['image_id'], image['name']])
65  if not sub_categories:
66  sub_categories.append([-emty_category_counter, {'label': _('No images'), 'disabled': True}])
67  emty_category_counter += 1
68  private_category = [_('Private images'), sub_categories]
69  categories.append(private_category)
70 
71  for group in data['images_group']:
72  sub_categories = []
73  for image in group['images']:
74  if image['state'] == image_states['ok']:
75  sub_categories.append([image['image_id'], image['name']])
76 
77  if not sub_categories:
78  sub_categories.append([-emty_category_counter, {'label': _('No images'), 'disabled': True}])
79  emty_category_counter += 1
80  group_category = [_('Group') + " " + group['name'], sub_categories]
81  categories.append(group_category)
82 
83  return categories
84 
85 
86 ##
87 #
88 # Returns list of the Templates names
89 #
90 # @parameter{data,dict}
91 #
92 # @returns{list} Templates names.
93 #
95  templates_list = []
96  for template in data['templates']:
97  if isinstance(template, dict):
98  if template['ec2name'] != 0:
99  templates_list.append([template['template_id'], template['name'] + ' [' + ec2names_reversed[template['ec2name']] + ']'])
100  else:
101  templates_list.append([template['template_id'], template['name']])
102  if templates_list == []:
103  templates_list.insert(0, (-1, _('None available')))
104  return templates_list
105 
106 
107 ##
108 #
109 # Returns list of the IPs.
110 #
111 # @parameter{data,dict}
112 # @parameter{select_flag,bool}
113 #
114 # @returns{list} IPs.
115 #
116 def parse_ips(data, select_flag=True):
117  ips_list = []
118  all_disabled = True
119  for ipa in data['ips']:
120  if ipa['lease_id'] == "":
121  ips_list.append([ipa['public_ip_id'], ipa['address']])
122  all_disabled = False
123  else:
124  ips_list.append([ipa['public_ip_id'], {'label': ipa['address'], 'disabled': True}])
125 
126  if all_disabled:
127  ips_list.insert(0, (-1, _('None available')))
128  else:
129  if select_flag == True:
130  ips_list.insert(0, [-1, _('None')])
131  return ips_list
132 
133 
134 ##
135 #
136 # Returns list of the assigned IPs to selected vm.
137 #
138 # @parameter{data,dict}
139 #
140 # @returns{list} IPs.
141 #
143  ips_list = []
144  for lease in data['vm']['leases']:
145  if lease['public_ip'] != "":
146  ips_list.append([lease['public_ip']['lease_id'], lease['public_ip']['ip']])
147  if ips_list == []:
148  ips_list.insert(0, (-1, _('None available')))
149  return ips_list
150 
151 
152 ##
153 #
154 # Returns list of the assigned IPs to selected vm.
155 #
156 # @parameter{data,dict}
157 #
158 # @returns{list} IPs.
159 #
160 def parse_leases(data):
161  ips_list = []
162  for lease in data['vm']['leases']:
163  if lease['public_ip'] == "":
164  ips_list.append([lease['lease_id'], lease['address']])
165  if ips_list == []:
166  ips_list.insert(0, (-1, _('None available')))
167  return ips_list
168 
169 
170 ##
171 #
172 # Returns list of the disks' names.
173 #
174 # @parameter{data,dict}
175 # @parameter{select_flag,boolean}
176 #
177 # @returns{list} disks' names.
178 #
179 def parse_disks(data, select_flag=True):
180  disks_list = []
181  all_disabled = True
182  for disk in data['disks']:
183  if disk['state'] == image_states['ok']:
184  label = '%s (%s)' % (disk['name'], filesizeformatmb(disk['size']))
185  if disk['vm_id'] == "":
186  disks_list.append([disk['storage_image_id'], label])
187  all_disabled = False
188  else:
189  disks_list.append([disk['storage_image_id'], {'label': label, 'disabled': True}])
190  if disks_list == []:
191  if select_flag == True:
192  disks_list.append((-1, {'label': _('None available'), 'disabled': True}))
193  else:
194  disks_list.append((-1, _('None available')))
195  else:
196  if select_flag == False and all_disabled:
197  disks_list.insert(0, [-1, _('None available')])
198 
199  return disks_list
200 
201 
202 ##
203 #
204 # Returns list of the disks' names.
205 #
206 # @parameter{data}
207 #
208 # @returns{list} disks' names.
209 #
211  live_attach = []
212  for item in data['disk_controllers']:
213  if item['live_attach'] == True:
214  live_attach.append(item['id'])
215  disks_list = []
216  all_disabled = True
217  for disk in data['vm']['storage_images']:
218  if disk['disk_controller'] in live_attach:
219  disks_list.append([disk['storage_image_id'], disk['name']])
220  all_disabled = False
221  else:
222  disks_list.append([disk['storage_image_id'], {'label': disk['name'], 'disabled': True}])
223  if all_disabled:
224  disks_list.insert(0, (-1, _('None available')))
225  return disks_list
226 
227 
228 ##
229 #
230 # Returns list of the ISO images' names.
231 #
232 # @parameter{data}
233 #
234 # @returns{list} ISO images' names.
235 #
236 def parse_iso(data):
237  iso_list = []
238  for iso in data['iso']:
239  if iso['state'] == image_states['ok']:
240  label = '%s (%s)' % (iso['name'], filesizeformatmb(iso['size']))
241  iso_list.append([iso['iso_image_id'], label])
242  if iso_list == []:
243  iso_list.insert(0, (-1, _('None available')))
244  else:
245  iso_list.insert(0, [-1, _('None')])
246  return iso_list
247 
248 
249 ##
250 #
251 # Returns list of the images' descriptions.
252 #
253 # @parameter{data}
254 #
255 # @returns{list} images' descriptions.
256 #
258  images_list = []
259 
260  for image in data['images_public']:
261  images_list.append((image['image_id'], image['description'], image['creation_date']))
262 
263  for image in data['images_private']:
264  images_list.append((image['image_id'], image['description'], image['creation_date']))
265 
266  for image in parse_group_images(data):
267  images_list.append((image['image_id'], image['description'], image['creation_date']))
268 
269  return images_list
270 
271 
272 ##
273 #
274 # Returns a list of group images.
275 #
276 # @parameter{data,dict}
277 #
279  images_group = []
280 
281  for group in data['images_group']:
282  for image in group['images']:
283  images_group.append(image)
284 
285  return images_group
286 
287 
288 ##
289 #
290 # Returns a list of the storages' names.
291 #
292 # @parameter{data,dict}
293 #
294 # @returns{list} storages' names.
295 #
297 
298  storages_list = []
299  if data['storages']:
300  storages_list.append(('0', _('All storages')))
301  for storage in data['storages']:
302  if isinstance(storage, dict):
303  storages_list.append((storage['storage_id'], storage['name']))
304  if storages_list == []:
305  storages_list.append((-1, _('None available')))
306 
307  return storages_list
308 
309 
310 ##
311 #
312 # Returns list of the nodes' names. (attaching storage to multiple nodes).
313 #
314 # @parameter{data}
315 #
316 # @returns{list} nodes' names.
317 #
319  nodes_list = []
320  if data['nodes']:
321  nodes_list.append(('0', _('All nodes')))
322  for node in data['nodes']:
323  if isinstance(node, dict):
324  nodes_list.append((node['node_id'], node['address']))
325 
326  if nodes_list == []:
327  nodes_list.append((-1, _('None available')))
328 
329  return nodes_list
330 
331 
332 ##
333 #
334 # Returns list of the groups' ids.
335 #
336 # @parameter{data}
337 #
338 # @returns{list} groups' ids.
339 #
341  groups_list = []
342 
343  for group in data['groups']:
344  groups_list.append(group['group_id'])
345 
346  return groups_list
347 
348 
349 ##
350 #
351 # Returns list of the groups' names.
352 #
353 # @parameter{data}
354 #
355 # @returns{list} groups' names.
356 #
357 def parse_groups(data):
358  groups_list = []
359 
360  for group in data['groups']:
361  groups_list.append([group['group_id'], group['name']])
362 
363  if groups_list == []:
364  groups_list.append((-1, _('No group available')))
365 
366  return groups_list
367 
368 
369 ##
370 #
371 # Returns list of the ownde groups' ids.
372 #
373 # @parameter{data}
374 #
375 # @returns{list} owned groups' ids.
376 #
378  groups_list = []
379 
380  for group in data['own_groups']:
381  groups_list.append(group['group_id'])
382 
383  return groups_list
384 
385 
386 ##
387 #
388 # @returns{list(key)} platforms.
389 #
391  platform_list = []
392  for key in image_platforms_reversed:
393  platform_list.append((key, image_platforms_reversed[key]))
394 
395  return platform_list
396 
397 
398 ##
399 #
400 # Returns a list of (potentially disabled) choices from a dictionary.
401 #
402 def parse_generic(data, key):
403  choices = []
404  for k, v in sorted(data[key].iteritems(), key=lambda item: item[1]):
405  choices.append([v, k])
406  return choices
407 
408 
409 ##
410 #
411 # Returns a list of (potentially disabled) choices from a dictionary.
412 #
413 def parse_generic_enabled(data, key):
414  choices = []
415  for item in data[key]:
416  if item.get('enabled') is not None and not item['enabled']:
417  choices.append([item['id'], {'label': item['name'], 'disabled': True}])
418  else:
419  choices.append([item['id'], item['name']])
420  return choices
421 
422 
423 ##
424 #
425 # @parameter{data}
426 #
427 # @returns{list} the SSH keys.
428 #
429 def parse_ssh_keys(data):
430  keys_list = []
431  for key in data['keys']:
432  keys_list.append((key['key_id'], key['name']))
433 
434  if keys_list == []:
435  keys_list.append((-1, _('None available')))
436 
437  return keys_list
438 
439 
440 ##
441 #
442 # @parameter{data}
443 #
444 # @returns{list} the CMs.
445 #
446 def parse_cm_list(data):
447  cm_list = []
448  for item in data['cms']:
449  cm_list.append((item['cluster_id'], item['name']))
450 
451  if cm_list == []:
452  cm_list.append((-1, _('None available')))
453 
454  return cm_list
455 
456 
457 ##
458 #
459 # @parameter{data}
460 #
461 # @returns{list} the CMs' users.
462 #
463 def parse_cm_users(data):
464  cm_user_list = []
465  for item in data['users']:
466  cm_user_list.append((item['user_id'], item['first'] + " " + item['last']))
467 
468  if cm_user_list == []:
469  cm_user_list.append((-1, _('None available')))
470 
471  return cm_user_list
472