cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
vm.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.views.admin_cm.vm
22 # @author Krzysztof Danielowski
23 # @author Piotr Wójcik
24 # @date 03.02.2012
25 #
26 from django.shortcuts import render_to_response
27 from django.template import RequestContext
28 from django.template.loader import render_to_string
29 
30 from common.states import vm_states
31 from wi.models.user import parse_user
32 from wi.utils import messages_ajax
33 from wi.utils.decorators import admin_cm_permission
34 from wi.utils.decorators import django_view
35 from wi.utils.formatters import time_from_sec
36 from wi.utils.messages_ajax import ajax_request
37 from wi.utils.states import vm_states_reversed
38 from wi.utils.views import prep_data
39 
40 
41 @django_view
42 @admin_cm_permission
43 ##
44 #
45 # View handling the page embeding the VM list.
46 #
47 def cma_vms(request, template_name='admin_cm/show_vm.html'):
48  users = prep_data(('admin_cm/user/get_list/', {'short': True}), request.session)
49  return render_to_response(template_name, {'all_users': users}, context_instance=RequestContext(request))
50 
51 
52 @django_view
53 @ajax_request
54 @admin_cm_permission
55 ##
56 #
57 # Ajax view for fetching VM list.
58 #
59 def cma_vms_ajax_get_table(request, user_id):
60  if request.method == 'GET':
61 
62  vms = prep_data(('admin_cm/vm/get_list/', {'user_id': int(user_id)}), request.session)
63 
64  for item in vms:
65  item['stateName'] = unicode(vm_states_reversed[item['state']])
66  item['pub_ip'] = []
67  for i in item['leases']:
68  if i['public_ip'] != "":
69  item['pub_ip'].append(i['public_ip']['address'])
70  item['stringIP'] = ', '.join(item['pub_ip'])
71  item['stringISO'] = ', '.join([iso['name'] for iso in item['iso_images']])
72  item['stringDisk'] = ', '.join([disk['name'] for disk in item['storage_images']])
73 
74  return messages_ajax.success(vms)
75 
76 
77 @django_view
78 @ajax_request
79 @admin_cm_permission
80 ##
81 #
82 # Ajax view for fetching VM details.
83 #
84 def cma_vms_ajax_vm_details(request, vm_id, template_name='admin_cm/ajax/vm_details.html'):
85  if request.method == 'POST':
86 
87  vm = prep_data(('admin_cm/vm/get_by_id/', {'vm_id': vm_id}), request.session)
88 
89  rest_data2 = prep_data({'user': ('admin_cm/user/get_by_id/', {'user_id': vm['user_id']})}, request.session)
90  owner = parse_user(rest_data2['user'])
91 
92  if vm['state'] == vm_states['closed']:
93  return messages_ajax.success('', status=1)
94 
95  vm['uptime'] = time_from_sec(vm['uptime'])
96 
97  return messages_ajax.success(
98  render_to_string(template_name,
99  {'vm_id': vm_id,
100  'item': vm,
101  'states_reversed': vm_states_reversed,
102  'states': vm_states,
103  'owner': owner},
104  context_instance=RequestContext(request)))
105