add nick
[laas.git] / src / workflow / snapshot_workflow.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from django.utils import timezone
12 import json
13
14 from booking.models import Booking
15 from resource_inventory.models import ResourceQuery, Image
16 from workflow.models import WorkflowStep
17 from workflow.forms import BasicMetaForm, SnapshotHostSelectForm
18
19
20 class Select_Host_Step(WorkflowStep):
21     template = "snapshot_workflow/steps/select_host.html"
22     title = "Select Host"
23     description = "Choose which machine you want to snapshot"
24     short_title = "host"
25
26     def get_context(self):
27         context = super(Select_Host_Step, self).get_context()
28         context['form'] = SnapshotHostSelectForm()
29         booking_hosts = {}
30         now = timezone.now()
31         user = self.repo_get(self.repo.SESSION_USER)
32         bookings = Booking.objects.filter(start__lt=now, end__gt=now, owner=user)
33         for booking in bookings:
34             booking_hosts[booking.id] = {}
35             booking_hosts[booking.id]['purpose'] = booking.purpose
36             booking_hosts[booking.id]['start'] = booking.start.strftime("%Y-%m-%d")
37             booking_hosts[booking.id]['end'] = booking.end.strftime("%Y-%m-%d")
38             booking_hosts[booking.id]['hosts'] = []
39             for genericHost in booking.resource.template.getResources():
40                 booking_hosts[booking.id]['hosts'].append({"name": genericHost.resource.name})
41
42         context['booking_hosts'] = booking_hosts
43
44         chosen_host = self.repo_get(self.repo.SNAPSHOT_MODELS, {}).get("host")
45         if chosen_host:
46             chosen = {}
47             chosen['booking_id'] = self.repo_get(self.repo.SNAPSHOT_BOOKING_ID)
48             chosen['hostname'] = chosen_host.template.resource.name
49             context['chosen'] = chosen
50         return context
51
52     def post(self, post_data, user):
53         host_data = post_data.get("host")
54         if not host_data:
55             self.set_invalid("Please select a host")
56             return
57         host = json.loads(host_data)
58         if 'name' not in host or 'booking' not in host:
59             self.set_invalid("Invalid host selected")
60             return
61         name = host['name']
62         booking_id = host['booking']
63         booking = Booking.objects.get(pk=booking_id)
64         host = ResourceQuery.get(bundle=booking.resource, template__resource__name=name)
65         models = self.repo_get(self.repo.SNAPSHOT_MODELS, {})
66         if "host" not in models:
67             models['host'] = host
68         if 'snapshot' not in models:
69             models['snapshot'] = Image()
70         self.repo_put(self.repo.SNAPSHOT_MODELS, models)
71         self.repo_put(self.repo.SNAPSHOT_BOOKING_ID, booking_id)
72
73         confirm = self.repo_get(self.repo.CONFIRMATION, {})
74         snap_confirm = confirm.get("snapshot", {})
75         snap_confirm['host'] = name
76         confirm['snapshot'] = snap_confirm
77         self.repo_put(self.repo.CONFIRMATION, confirm)
78         self.set_valid("Success")
79
80
81 class Image_Meta_Step(WorkflowStep):
82     template = "snapshot_workflow/steps/meta.html"
83     title = "Additional Information"
84     description = "We need some more info"
85     short_title = "info"
86
87     def get_context(self):
88         context = super(Image_Meta_Step, self).get_context()
89         name = self.repo_get(self.repo.SNAPSHOT_NAME, False)
90         desc = self.repo_get(self.repo.SNAPSHOT_DESC, False)
91         form = None
92         if name and desc:
93             form = BasicMetaForm(initial={"name": name, "description": desc})
94         else:
95             form = BasicMetaForm()
96         context['form'] = form
97         return context
98
99     def post(self, post_data, user):
100         form = BasicMetaForm(post_data)
101         if form.is_valid():
102             name = form.cleaned_data['name']
103             self.repo_put(self.repo.SNAPSHOT_NAME, name)
104             description = form.cleaned_data['description']
105             self.repo_put(self.repo.SNAPSHOT_DESC, description)
106
107             confirm = self.repo_get(self.repo.CONFIRMATION, {})
108             snap_confirm = confirm.get("snapshot", {})
109             snap_confirm['name'] = name
110             snap_confirm['description'] = description
111             confirm['snapshot'] = snap_confirm
112             self.repo_put(self.repo.CONFIRMATION, confirm)
113
114             self.set_valid("Success")
115         else:
116             self.set_invalid("Please Fill out the Form")