1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Copyright 2012 Nebula, Inc.
9
# Licensed under the Apache License, Version 2.0 (the "License"); you may
10
# not use this file except in compliance with the License. You may obtain
11
# a copy of the License at
13
# http://www.apache.org/licenses/LICENSE-2.0
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
# License for the specific language governing permissions and limitations
22
Views for managing Nova images.
27
from django import shortcuts
28
from django.contrib import messages
29
from django.core.urlresolvers import reverse
30
from django.utils.text import normalize_newlines
31
from django.utils.translation import ugettext as _
33
from horizon import api
34
from horizon import exceptions
35
from horizon import forms
37
LOG = logging.getLogger(__name__)
40
class UpdateImageForm(forms.SelfHandlingForm):
41
image_id = forms.CharField(widget=forms.HiddenInput())
42
name = forms.CharField(max_length="25", label=_("Name"))
43
kernel = forms.CharField(max_length="25", label=_("Kernel ID"),
45
ramdisk = forms.CharField(max_length="25", label=_("Ramdisk ID"),
47
architecture = forms.CharField(label=_("Architecture"), required=False)
48
container_format = forms.CharField(label=_("Container Format"),
50
disk_format = forms.CharField(label=_("Disk Format"))
52
def handle(self, request, data):
53
# TODO add public flag to image meta properties
54
image_id = data['image_id']
55
error_updating = _('Unable to update image "%s".')
57
meta = {'is_public': True,
58
'disk_format': data['disk_format'],
59
'container_format': data['container_format'],
63
meta['properties']['kernel_id'] = data['kernel']
65
meta['properties']['ramdisk_id'] = data['ramdisk']
66
if data['architecture']:
67
meta['properties']['architecture'] = data['architecture']
70
api.image_update(request, image_id, meta)
71
messages.success(request, _('Image was successfully updated.'))
73
exceptions.handle(request, error_updating % image_id)
74
return shortcuts.redirect('horizon:nova:images_and_snapshots:index')
77
class LaunchForm(forms.SelfHandlingForm):
78
name = forms.CharField(max_length=80, label=_("Server Name"))
79
image_id = forms.CharField(widget=forms.HiddenInput())
80
tenant_id = forms.CharField(widget=forms.HiddenInput())
81
user_data = forms.CharField(widget=forms.Textarea,
84
flavor = forms.ChoiceField(label=_("Flavor"),
85
help_text=_("Size of image to launch."))
86
keypair = forms.ChoiceField(label=_("Keypair"),
88
help_text=_("Which keypair to use for "
90
count = forms.CharField(label=_("Instance Count"),
93
widget=forms.TextInput(),
94
help_text=_("Number of instances to launch."))
95
security_groups = forms.MultipleChoiceField(
96
label=_("Security Groups"),
99
widget=forms.CheckboxSelectMultiple(),
100
help_text=_("Launch instance in these "
102
volume = forms.ChoiceField(label=_("Volume or Volume Snapshot"),
104
help_text=_("Volume to boot from."))
105
device_name = forms.CharField(label=_("Device Name"),
108
help_text=_("Volume mount point (e.g. 'vda' "
109
"mounts at '/dev/vda')."))
110
delete_on_terminate = forms.BooleanField(
111
label=_("Delete on Terminate"),
114
help_text=_("Delete volume on instance termiante"))
116
def __init__(self, *args, **kwargs):
117
flavor_list = kwargs.pop('flavor_list')
118
keypair_list = kwargs.pop('keypair_list')
119
security_group_list = kwargs.pop('security_group_list')
120
volume_list = kwargs.pop('volume_list')
121
super(LaunchForm, self).__init__(*args, **kwargs)
122
self.fields['flavor'].choices = flavor_list
123
self.fields['keypair'].choices = keypair_list
124
self.fields['security_groups'].choices = security_group_list
125
self.fields['volume'].choices = volume_list
127
def handle(self, request, data):
129
if(len(data['volume']) > 0):
130
if(data['delete_on_terminate']):
131
delete_on_terminate = 1
133
delete_on_terminate = 0
134
dev_mapping = {data['device_name']:
135
("%s::%s" % (data['volume'], delete_on_terminate))}
139
api.server_create(request,
144
normalize_newlines(data.get('user_data')),
145
data.get('security_groups'),
147
instance_count=int(data.get('count')))
148
messages.success(request,
149
_('Instance "%s" launched.') % data["name"])
151
redirect = reverse("horizon:nova:images_and_snapshots:index")
152
exceptions.handle(request,
153
_('Unable to launch instance: %(exc)s'),
155
return shortcuts.redirect('horizon:nova:instances_and_volumes:index')