views.py 4.3 KB
Newer Older
1
from django.http import HttpResponse
2
from django.views.generic import TemplateView, DetailView
3 4 5
from django_tables2 import SingleTableView

from tables import VmListTable
Kálmán Viktor committed
6

7
from vm.models import Instance, InstanceTemplate, InterfaceTemplate
8
from firewall.models import Vlan
9
from storage.models import Disk
10
from django.core import signing
11
from os import getenv
12

13 14
import json

15 16

class IndexView(TemplateView):
Kálmán Viktor committed
17
    template_name = "dashboard/index.html"
18

19
    def get_context_data(self, **kwargs):
20 21 22 23 24
        if self.request.user.is_authenticated():
            user = self.request.user
        else:
            user = None

25
        instances = Instance.objects.filter(owner=user)
26 27
        context = super(IndexView, self).get_context_data(**kwargs)
        context.update({
28
            'instances': instances[:5],
29
            'more_instances': instances.count() - len(instances[:5])
30 31 32
        })

        context.update({
33 34
            'running_vms': instances.filter(state='RUNNING'),
            'running_vm_num': instances.filter(state='RUNNING').count(),
35 36
            'stopped_vm_num': instances.exclude(
                state__in=['RUNNING', 'NOSTATE']).count()
37 38 39
        })
        return context

40

41
class VmDetailView(DetailView):
Kálmán Viktor committed
42
    template_name = "dashboard/vm-detail.html"
43 44 45 46 47 48 49
    queryset = Instance.objects.all()

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        instance = context['instance']
        if instance.node:
            port = instance.vnc_port
50
            host = str(instance.node.host.ipv4)
51
            value = signing.dumps({'host': host,
52 53
                                   'port': port},
                                  key=getenv("PROXY_SECRET", 'asdasd')),
54 55 56 57
            context.update({
                'vnc_url': '%s' % value
            })
        return context
Kálmán Viktor committed
58 59


60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
class TemplateDetail(DetailView):
    model = InstanceTemplate

    def get(self, request, *args, **kwargs):
        if request.is_ajax():
            template = InstanceTemplate.objects.get(pk=kwargs['pk'])
            template = {
                'num_cores': template.num_cores,
                'ram_size': template.ram_size,
                'priority': template.priority,
                'arch': template.arch,
                'description': template.description,
                'system': template.system,
                'name': template.name,
                'disks': [{'pk': d.pk, 'name': d.name}
                          for d in template.disks.all()],
                'network': [
                    {'vlan_pk': i.vlan.pk, 'vlan': i.vlan.name,
                     'managed': i.managed}
                    for i in InterfaceTemplate.objects.filter(
                        template=self.get_object()).all()
                ]
            }
            return HttpResponse(json.dumps(template),
                                content_type="application/json")
        else:
            # return super(TemplateDetail, self).get(request, *args, **kwargs)
            return HttpResponse('soon')


90
class VmList(SingleTableView):
Kálmán Viktor committed
91
    template_name = "dashboard/vm-list.html"
92 93
    model = Instance
    table_class = VmListTable
94
    table_pagination = False
95 96 97 98 99 100


class VmCreate(TemplateView):

    def get_template_names(self):
        if self.request.is_ajax():
101
            return ['dashboard/modal-wrapper.html']
102
        else:
103
            return ['dashboard/nojs-wrapper.html']
104 105 106

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
107 108 109 110
        context.update({
            'template': 'dashboard/vm-create.html',
            'box_title': 'Create a VM'
        })
111
        return self.render_to_response(context)
112 113 114

    def get_context_data(self, **kwargs):
        context = super(VmCreate, self).get_context_data(**kwargs)
115
        # TODO acl
116
        context.update({
117
            'templates': InstanceTemplate.objects.all(),
118
            'vlans': Vlan.objects.all(),
119
            'disks': Disk.objects.exclude(type="qcow2-snap")
120 121 122
        })

        return context
123 124 125 126 127 128 129 130

    def post(self, request, *args, **kwargs):
        resp = request.POST.copy()
        resp['managed-vlans'] = request.POST.getlist('managed-vlans')
        resp['unmanaged-vlans'] = request.POST.getlist('unmanaged-vlans')
        resp['disks'] = request.POST.getlist('disks')

        return HttpResponse(json.dumps(resp), content_type="application/json")