30
31
from horizon import exceptions
31
32
from horizon import forms
32
33
from horizon import tabs
33
from .forms import UpdateImageForm, LaunchForm
34
from .forms import UpdateImageForm
34
35
from .tabs import ImageDetailTabs
37
38
LOG = logging.getLogger(__name__)
40
class LaunchView(forms.ModalFormView):
41
form_class = LaunchForm
42
template_name = 'nova/images_and_snapshots/images/launch.html'
43
context_object_name = 'image'
45
def get_form_kwargs(self):
46
kwargs = super(LaunchView, self).get_form_kwargs()
47
kwargs['flavor_list'] = self.flavor_list()
48
kwargs['keypair_list'] = self.keypair_list()
49
kwargs['security_group_list'] = self.security_group_list()
50
kwargs['volume_list'] = self.volume_list()
53
def get_object(self, *args, **kwargs):
54
image_id = self.kwargs["image_id"]
56
self.object = api.image_get_meta(self.request, image_id)
58
msg = _('Unable to retrieve image "%s".') % image_id
59
redirect = reverse('horizon:nova:images_and_snapshots:index')
60
exceptions.handle(self.request, msg, redirect=redirect)
63
def get_context_data(self, **kwargs):
64
context = super(LaunchView, self).get_context_data(**kwargs)
66
context['usages'] = api.tenant_quota_usages(self.request)
68
exceptions.handle(self.request)
71
def get_initial(self):
72
return {'image_id': self.kwargs["image_id"],
73
'tenant_id': self.request.user.tenant_id}
75
def flavor_list(self):
76
display = '%(name)s (%(vcpus)sVCPU / %(disk)sGB Disk / %(ram)sMB Ram )'
78
flavors = api.flavor_list(self.request)
79
flavor_list = [(flavor.id, display % {"name": flavor.name,
80
"vcpus": flavor.vcpus,
83
for flavor in flavors]
86
exceptions.handle(self.request,
87
_('Unable to retrieve instance flavors.'))
88
return sorted(flavor_list)
90
def keypair_list(self):
92
keypairs = api.keypair_list(self.request)
93
keypair_list = [(kp.name, kp.name) for kp in keypairs]
96
exceptions.handle(self.request,
97
_('Unable to retrieve keypairs.'))
100
def security_group_list(self):
102
groups = api.security_group_list(self.request)
103
security_group_list = [(sg.name, sg.name) for sg in groups]
105
exceptions.handle(self.request,
106
_('Unable to retrieve list of security groups'))
107
security_group_list = []
108
return security_group_list
110
def volume_list(self):
111
volume_options = [("", _("Select Volume"))]
113
def _get_volume_select_item(volume):
114
if hasattr(volume, "volume_id"):
116
visible_label = _("Snapshot")
119
visible_label = _("Volume")
120
return (("%s:%s" % (volume.id, vol_type)),
121
("%s - %s GB (%s)" % (volume.display_name,
125
# First add volumes to the list
127
volumes = [v for v in api.volume_list(self.request) \
128
if v.status == api.VOLUME_STATE_AVAILABLE]
129
volume_options.extend(
130
[_get_volume_select_item(vol) for vol in volumes])
132
exceptions.handle(self.request,
133
_('Unable to retrieve list of volumes'))
135
# Next add volume snapshots to the list
137
snapshots = api.volume_snapshot_list(self.request)
138
snapshots = [s for s in snapshots \
139
if s.status == api.VOLUME_STATE_AVAILABLE]
140
volume_options.extend(
141
[_get_volume_select_item(snap) for snap in snapshots])
143
exceptions.handle(self.request,
144
_('Unable to retrieve list of volumes'))
146
return volume_options
149
41
class UpdateView(forms.ModalFormView):
150
42
form_class = UpdateImageForm
151
43
template_name = 'nova/images_and_snapshots/images/update.html'
154
46
def get_object(self, *args, **kwargs):
156
self.object = api.image_get_meta(self.request, kwargs['image_id'])
48
self.object = api.image_get(self.request, kwargs['image_id'])
158
msg = _('Unable to retrieve image "%s".') % kwargs['image_id']
50
msg = _('Unable to retrieve image.')
159
51
redirect = reverse('horizon:nova:images_and_snapshots:index')
160
52
exceptions.handle(self.request, msg, redirect=redirect)
161
53
return self.object
163
55
def get_initial(self):
164
properties = self.object['properties']
56
properties = self.object.properties
57
# NOTE(gabriel): glanceclient currently treats "is_public" as a string
58
# rather than a boolean. This should be fixed in the client.
59
public = self.object.is_public == "True"
165
60
return {'image_id': self.kwargs['image_id'],
166
'name': self.object.get('name', ''),
61
'name': self.object.name,
167
62
'kernel': properties.get('kernel_id', ''),
168
63
'ramdisk': properties.get('ramdisk_id', ''),
169
64
'architecture': properties.get('architecture', ''),
170
'container_format': self.object.get('container_format', ''),
171
'disk_format': self.object.get('disk_format', ''), }
65
'container_format': self.object.container_format,
66
'disk_format': self.object.disk_format,
174
70
class DetailView(tabs.TabView):