1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
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 ##############################################################################
11 from django.utils import timezone
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
20 class Select_Host_Step(WorkflowStep):
21 template = "snapshot_workflow/steps/select_host.html"
23 description = "Choose which machine you want to snapshot"
26 def get_context(self):
27 context = super(Select_Host_Step, self).get_context()
28 context['form'] = SnapshotHostSelectForm()
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})
42 context['booking_hosts'] = booking_hosts
44 chosen_host = self.repo_get(self.repo.SNAPSHOT_MODELS, {}).get("host")
47 chosen['booking_id'] = self.repo_get(self.repo.SNAPSHOT_BOOKING_ID)
48 chosen['hostname'] = chosen_host.template.resource.name
49 context['chosen'] = chosen
52 def post(self, post_data, user):
53 host_data = post_data.get("host")
55 self.set_invalid("Please select a host")
57 host = json.loads(host_data)
58 if 'name' not in host or 'booking' not in host:
59 self.set_invalid("Invalid host selected")
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:
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)
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")
81 class Image_Meta_Step(WorkflowStep):
82 template = "snapshot_workflow/steps/meta.html"
83 title = "Additional Information"
84 description = "We need some more info"
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)
93 form = BasicMetaForm(initial={"name": name, "description": desc})
95 form = BasicMetaForm()
96 context['form'] = form
99 def post(self, post_data, user):
100 form = BasicMetaForm(post_data)
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)
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)
114 self.set_valid("Success")
116 self.set_invalid("Please Fill out the Form")