summaryrefslogtreecommitdiff
path: root/django-openstack/django_openstack/syspanel/views/images.py
blob: 887581a4f01a92deee5568c80b4d32431e1f1523 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2011 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.

import logging

from django import template
from django.contrib import messages
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext as _

from glance.common import exception as glance_exception

from django_openstack import api
from django_openstack import forms
from django_openstack.decorators import enforce_admin_access

LOG = logging.getLogger('django_openstack.sysadmin.views.images')


class DeleteImage(forms.SelfHandlingForm):
    image_id = forms.CharField(required=True)

    def handle(self, request, data):
        image_id = data['image_id']
        try:
            api.image_delete(request, image_id)
        except glance_exception.ClientConnectionError, e:
            LOG.exception("Error connecting to glance")
            messages.error(request,
                           _("Error connecting to glance: %s") % e.message)
        except glance_exception.Error, e:
            LOG.exception('Error deleting image with id "%s"' % image_id)
            messages.error(request, _("Error deleting image: %s") % e.message)
        return redirect(request.build_absolute_uri())


class ToggleImage(forms.SelfHandlingForm):
    image_id = forms.CharField(required=True)

    def handle(self, request, data):
        image_id = data['image_id']
        try:
            api.image_update(request, image_id,
                    image_meta={'is_public': False})
        except glance_exception.ClientConnectionError, e:
            LOG.exception("Error connecting to glance")
            messages.error(request,
                           _("Error connecting to glance: %s") % e.message)
        except glance_exception.Error, e:
            LOG.exception('Error updating image with id "%s"' % image_id)
            messages.error(request, _("Error updating image: %s") % e.message)
        return redirect(request.build_absolute_uri())


class UpdateImageForm(forms.Form):
    name = forms.CharField(max_length="25", label=_("Name"))
    kernel = forms.CharField(max_length="25", label=_("Kernel ID"),
            required=False)
    ramdisk = forms.CharField(max_length="25", label=_("Ramdisk ID"),
            required=False)
    architecture = forms.CharField(label=_("Architecture"), required=False)
    #project_id = forms.CharField(label=_("Project ID"))
    container_format = forms.CharField(label=_("Container Format"),
            required=False)
    disk_format = forms.CharField(label=_("Disk Format"))
    #is_public = forms.BooleanField(label=_("Publicly Available"),
    #                               required=False)


@login_required
@enforce_admin_access
def index(request):
    for f in (DeleteImage, ToggleImage):
        form, handled = f.maybe_handle(request)
        if handled:
            return handled

    # We don't have any way of showing errors for these, so don't bother
    # trying to reuse the forms from above
    delete_form = DeleteImage()
    toggle_form = ToggleImage()

    images = []
    try:
        images = api.image_list_detailed(request)
        if not images:
            messages.info(request, _("There are currently no images."))
    except glance_exception.ClientConnectionError, e:
        LOG.exception("Error connecting to glance")
        messages.error(request,
                       _("Error connecting to glance: %s") % e.message)
    except glance_exception.Error, e:
        LOG.exception("Error retrieving image list")
        messages.error(request,
                       _("Error retrieving image list: %s") % e.message)

    return render_to_response('django_openstack/syspanel/images/index.html', {
        'delete_form': delete_form,
        'toggle_form': toggle_form,
        'images': images,
    }, context_instance=template.RequestContext(request))


@login_required
@enforce_admin_access
def update(request, image_id):
    try:
        image = api.image_get(request, image_id)
    except glance_exception.ClientConnectionError, e:
        LOG.exception("Error connecting to glance")
        messages.error(request,
                       _("Error connecting to glance: %s") % e.message)
    except glance_exception.Error, e:
        LOG.exception('Error retrieving image with id "%s"' % image_id)
        messages.error(request,
                       _("Error retrieving image %(image)s: %(msg)s") %
                       {"image": image_id, "msg": e.message})

    if request.method == "POST":
        form = UpdateImageForm(request.POST)
        if form.is_valid():
            image_form = form.clean()
            metadata = {
                'is_public': True,
                'disk_format': image_form['disk_format'],
                'container_format': image_form['container_format'],
                'name': image_form['name'],
            }
            try:
                # TODO add public flag to properties
                metadata['properties'] = {}
                if image_form['kernel']:
                    metadata['properties']['kernel_id'] = image_form['kernel']
                if image_form['ramdisk']:
                    metadata['properties']['ramdisk_id'] = \
                            image_form['ramdisk']
                if image_form['architecture']:
                    metadata['properties']['architecture'] = \
                            image_form['architecture']
                api.image_update(request, image_id, metadata)
                messages.success(request, _("Image was successfully updated."))
            except glance_exception.ClientConnectionError, e:
                LOG.exception("Error connecting to glance")
                messages.error(request,
                               _("Error connecting to glance: %s") % e.message)
            except glance_exception.Error, e:
                LOG.exception('Error updating image with id "%s"' % image_id)
                messages.error(request,
                               _("Error updating image: %s") % e.message)
            except:
                LOG.exception('Unspecified Exception in image update')
                messages.error(request,
                            _("Image could not be updated, please try again."))
            return redirect('syspanel_images_update', image_id)
        else:
            LOG.exception('Image "%s" failed to update' % image['name'])
            messages.error(request,
                           _("Image could not be uploaded, please try agian."))
            form = UpdateImageForm(request.POST)
            return render_to_response('django_openstack/syspanel/images/'
            'update.html', {
                'image': image,
                'form': form,
            }, context_instance=template.RequestContext(request))
    else:
        form = UpdateImageForm(initial={
                'name': image.get('name', ''),
                'kernel': image['properties'].get('kernel_id', ''),
                'ramdisk': image['properties'].get('ramdisk_id', ''),
                'is_public': image.get('is_public', ''),
                'location': image.get('location', ''),
                'state': image['properties'].get('image_state', ''),
                'architecture': image['properties'].get('architecture', ''),
                'project_id': image['properties'].get('project_id', ''),
                'container_format': image.get('container_format', ''),
                'disk_format': image.get('disk_format', ''),
            })

        return render_to_response('django_openstack/syspanel/images/'
        'update.html', {
            'image': image,
            'form': form,
        }, context_instance=template.RequestContext(request))


@login_required
@enforce_admin_access
def upload(request):
    if request.method == "POST":
        form = UploadImageForm(request.POST)
        if form.is_valid():
            image = form.clean()
            metadata = {'is_public': image['is_public'],
                        'disk_format': 'ami',
                        'container_format': 'ami',
                        'name': image['name']}
            try:
                messages.success(request,
                                 _("Image was successfully uploaded."))
            except:
                # TODO add better error management
                messages.error(request,
                        _("Image could not be uploaded, please try again."))

            try:
                api.image_create(request, metadata, image['image_file'])
            except glance_exception.ClientConnectionError, e:
                LOG.exception('Error connecting to glance while trying to'
                              'upload image')
                messages.error(request,
                               _("Error connecting to glance: %s") % e.message)
            except glance_exception.Error, e:
                LOG.exception('Glance exception while uploading image')
                messages.error(request,
                               _("Error adding image: %s") % e.message)
        else:
            LOG.exception('Image "%s" failed to upload' % image['name'])
            messages.error(request,
                           _("Image could not be uploaded, please try agian."))
            form = UploadImageForm(request.POST)
            return render_to_response('django_nova_syspanel/images/'
                'upload.html', {
                'form': form,
            }, context_instance=template.RequestContext(request))

        return redirect('syspanel_images')
    else:
        form = UploadImageForm()
        return render_to_response('django_nova_syspanel/images/'
            'upload.html', {
            'form': form,
        }, context_instance=template.RequestContext(request))