File: views.py

package info (click to toggle)
horizon 2012.1.1-10
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: python: 12,012; sh: 471; makefile: 172; xml: 8
file content (177 lines) | stat: -rw-r--r-- 6,801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Views for managing Nova images.
"""

import logging
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _

from horizon import api
from horizon import exceptions
from horizon import forms
from horizon import tabs
from .forms import UpdateImageForm, LaunchForm
from .tabs import ImageDetailTabs


LOG = logging.getLogger(__name__)


class LaunchView(forms.ModalFormView):
    form_class = LaunchForm
    template_name = 'nova/images_and_snapshots/images/launch.html'
    context_object_name = 'image'

    def get_form_kwargs(self):
        kwargs = super(LaunchView, self).get_form_kwargs()
        kwargs['flavor_list'] = self.flavor_list()
        kwargs['keypair_list'] = self.keypair_list()
        kwargs['security_group_list'] = self.security_group_list()
        kwargs['volume_list'] = self.volume_list()
        return kwargs

    def get_object(self, *args, **kwargs):
        image_id = self.kwargs["image_id"]
        try:
            self.object = api.image_get_meta(self.request, image_id)
        except:
            msg = _('Unable to retrieve image "%s".') % image_id
            redirect = reverse('horizon:nova:images_and_snapshots:index')
            exceptions.handle(self.request, msg, redirect=redirect)
        return self.object

    def get_context_data(self, **kwargs):
        context = super(LaunchView, self).get_context_data(**kwargs)
        try:
            context['usages'] = api.tenant_quota_usages(self.request)
        except:
            exceptions.handle(self.request)
        return context

    def get_initial(self):
        return {'image_id': self.kwargs["image_id"],
                'tenant_id': self.request.user.tenant_id}

    def flavor_list(self):
        display = '%(name)s (%(vcpus)sVCPU / %(disk)sGB Disk / %(ram)sMB Ram )'
        try:
            flavors = api.flavor_list(self.request)
            flavor_list = [(flavor.id, display % {"name": flavor.name,
                                                  "vcpus": flavor.vcpus,
                                                  "disk": flavor.disk,
                                                  "ram": flavor.ram})
                                                for flavor in flavors]
        except:
            flavor_list = []
            exceptions.handle(self.request,
                              _('Unable to retrieve instance flavors.'))
        return sorted(flavor_list)

    def keypair_list(self):
        try:
            keypairs = api.keypair_list(self.request)
            keypair_list = [(kp.name, kp.name) for kp in keypairs]
        except:
            keypair_list = []
            exceptions.handle(self.request,
                              _('Unable to retrieve keypairs.'))
        return keypair_list

    def security_group_list(self):
        try:
            groups = api.security_group_list(self.request)
            security_group_list = [(sg.name, sg.name) for sg in groups]
        except:
            exceptions.handle(self.request,
                              _('Unable to retrieve list of security groups'))
            security_group_list = []
        return security_group_list

    def volume_list(self):
        volume_options = [("", _("Select Volume"))]

        def _get_volume_select_item(volume):
            if hasattr(volume, "volume_id"):
                vol_type = "snap"
                visible_label = _("Snapshot")
            else:
                vol_type = "vol"
                visible_label = _("Volume")
            return (("%s:%s:%s" % (volume.id, vol_type, volume.size)),
                    ("%s - %s GB (%s)" % (volume.display_name,
                                         volume.size,
                                         visible_label)))

        # First add volumes to the list
        try:
            volumes = [v for v in api.volume_list(self.request) \
                       if v.status == api.VOLUME_STATE_AVAILABLE]
            volume_options.extend(
                    [_get_volume_select_item(vol) for vol in volumes])
        except:
            exceptions.handle(self.request,
                              _('Unable to retrieve list of volumes'))

        # Next add volume snapshots to the list
        try:
            snapshots = api.volume_snapshot_list(self.request)
            snapshots = [s for s in snapshots \
                         if s.status == api.VOLUME_STATE_AVAILABLE]
            volume_options.extend(
                    [_get_volume_select_item(snap) for snap in snapshots])
        except:
            exceptions.handle(self.request,
                              _('Unable to retrieve list of volumes'))

        return volume_options


class UpdateView(forms.ModalFormView):
    form_class = UpdateImageForm
    template_name = 'nova/images_and_snapshots/images/update.html'
    context_object_name = 'image'

    def get_object(self, *args, **kwargs):
        try:
            self.object = api.image_get_meta(self.request, kwargs['image_id'])
        except:
            msg = _('Unable to retrieve image "%s".') % kwargs['image_id']
            redirect = reverse('horizon:nova:images_and_snapshots:index')
            exceptions.handle(self.request, msg, redirect=redirect)
        return self.object

    def get_initial(self):
        properties = self.object['properties']
        return {'image_id': self.kwargs['image_id'],
                'name': self.object.get('name', ''),
                'kernel': properties.get('kernel_id', ''),
                'ramdisk': properties.get('ramdisk_id', ''),
                'architecture': properties.get('architecture', ''),
                'container_format': self.object.get('container_format', ''),
                'disk_format': self.object.get('disk_format', ''),
                'public': self.object.get('is_public')}


class DetailView(tabs.TabView):
    tab_group_class = ImageDetailTabs
    template_name = 'nova/images_and_snapshots/images/detail.html'