Skip to content

Commit

Permalink
Converts images and snapshots to new tables, modals, error handling, …
Browse files Browse the repository at this point in the history
…etc.

Fixed bug 905235. Fixed bug 906487.

Change-Id: Ida68d82508357fe31695c0c66f7aaeabdc911105
  • Loading branch information
gabrielhurley committed Jan 18, 2012
1 parent 1e9a178 commit 9f9d4ab
Show file tree
Hide file tree
Showing 25 changed files with 400 additions and 666 deletions.
2 changes: 0 additions & 2 deletions horizon/horizon/dashboards/nova/containers/forms.py
Expand Up @@ -69,9 +69,7 @@ class CopyObject(forms.SelfHandlingForm):

def __init__(self, *args, **kwargs):
containers = kwargs.pop('containers')

super(CopyObject, self).__init__(*args, **kwargs)

self.fields['new_container_name'].choices = containers

def handle(self, request, data):
Expand Down
176 changes: 55 additions & 121 deletions horizon/horizon/dashboards/nova/images_and_snapshots/images/forms.py
Expand Up @@ -24,8 +24,9 @@

import logging

from django import shortcuts
from django.contrib import messages
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from django.utils.text import normalize_newlines
from django.utils.translation import ugettext as _
from glance.common import exception as glance_exception
Expand All @@ -50,57 +51,34 @@ class UpdateImageForm(forms.SelfHandlingForm):
disk_format = forms.CharField(label=_("Disk Format"))

def handle(self, request, data):
# TODO add public flag to image meta properties
image_id = data['image_id']
tenant_id = request.user.tenant_id
error_retrieving = _('Unable to retrieve image info from glance: %s'
% image_id)
error_updating = _('Error updating image with id: %s' % image_id)
error_retrieving = _('Unable to retrieve image "%s".')
error_updating = _('Unable to update image "%s".')

try:
image = api.image_get_meta(request, image_id)
except glance_exception.ClientConnectionError, e:
LOG.exception(_('Error connecting to glance'))
messages.error(request, error_retrieving)
except glance_exception.Error, e:
LOG.exception(error_retrieving)
messages.error(request, error_retrieving)

if image.owner == request.user.tenant_id:
try:
meta = {
'is_public': True,
'disk_format': data['disk_format'],
'container_format': data['container_format'],
'name': data['name'],
}
# TODO add public flag to properties
meta['properties'] = {}
if data['kernel']:
meta['properties']['kernel_id'] = data['kernel']

if data['ramdisk']:
meta['properties']['ramdisk_id'] = data['ramdisk']

if data['architecture']:
meta['properties']['architecture'] = data['architecture']

api.image_update(request, image_id, meta)
messages.success(request, _('Image was successfully updated.'))

except glance_exception.ClientConnectionError, e:
LOG.exception(_('Error connecting to glance'))
messages.error(request, error_retrieving)
except glance_exception.Error, e:
LOG.exception(error_updating)
messages.error(request, error_updating)
except:
LOG.exception(_('Unspecified Exception in image update'))
messages.error(request, error_updating)
return redirect('dash_images_update', tenant_id, image_id)
else:
messages.info(request, _('Unable to delete the requested image '
'because you are not its owner.'))
return redirect('dash_images_update', tenant_id, image_id)
except:
exceptions.handle(request, error_retrieving % image_id)

meta = {'is_public': True,
'disk_format': data['disk_format'],
'container_format': data['container_format'],
'name': data['name'],
'properties': {}}
if data['kernel']:
meta['properties']['kernel_id'] = data['kernel']
if data['ramdisk']:
meta['properties']['ramdisk_id'] = data['ramdisk']
if data['architecture']:
meta['properties']['architecture'] = data['architecture']

try:
api.image_update(request, image_id, meta)
messages.success(request, _('Image was successfully updated.'))
except:
exceptions.handle(request, error_updating % image_id)
return shortcuts.redirect('horizon:nova:images_and_snapshots:index')


class LaunchForm(forms.SelfHandlingForm):
Expand All @@ -110,87 +88,43 @@ class LaunchForm(forms.SelfHandlingForm):
user_data = forms.CharField(widget=forms.Textarea,
label=_("User Data"),
required=False)
flavor = forms.ChoiceField(label=_("Flavor"),
help_text=_("Size of image to launch."))
keypair = forms.ChoiceField(label=_("Keypair"),
required=False,
help_text=_("Which keypair to use for "
"authentication."))
security_groups = forms.MultipleChoiceField(
label=_("Security Groups"),
required=True,
initial=["default"],
widget=forms.CheckboxSelectMultiple(),
help_text=_("Launch instance in these "
"security groups."))

# make the dropdown populate when the form is loaded not when django is
# started
def __init__(self, *args, **kwargs):
flavor_list = kwargs.pop('flavor_list')
keypair_list = kwargs.pop('keypair_list')
security_group_list = kwargs.pop('security_group_list')
super(LaunchForm, self).__init__(*args, **kwargs)
flavorlist = kwargs.get('initial', {}).get('flavorlist', [])
self.fields['flavor'] = forms.ChoiceField(
choices=flavorlist,
label=_("Flavor"),
help_text="Size of Image to launch")

keynamelist = kwargs.get('initial', {}).get('keynamelist', [])
self.fields['key_name'] = forms.ChoiceField(choices=keynamelist,
label=_("Key Name"),
required=False,
help_text="Which keypair to use for authentication")

securitygrouplist = kwargs.get('initial', {}).get(
'securitygrouplist', [])
self.fields['security_groups'] = forms.MultipleChoiceField(
choices=securitygrouplist,
label=_("Security Groups"),
required=True,
initial=['default'],
widget=forms.CheckboxSelectMultiple(),
help_text="Launch instance in these Security Groups")
# setting self.fields.keyOrder seems to break validation,
# so ordering fields manually
field_list = (
'name',
'user_data',
'flavor',
'key_name')
for field in field_list[::-1]:
self.fields.insert(0, field, self.fields.pop(field))
self.fields['flavor'].choices = flavor_list
self.fields['keypair'].choices = keypair_list
self.fields['security_groups'].choices = security_group_list

def handle(self, request, data):
image_id = data['image_id']
tenant_id = data['tenant_id']
try:
image = api.image_get_meta(request, image_id)
flavor = api.flavor_get(request, data['flavor'])
api.server_create(request,
data['name'],
image,
flavor,
data.get('key_name'),
data['image_id'],
data['flavor'],
data.get('keypair'),
normalize_newlines(data.get('user_data')),
data.get('security_groups'))

msg = _('Instance was successfully launched')
LOG.info(msg)
messages.success(request, msg)
return redirect(
'horizon:nova:instances_and_volumes:index')

messages.success(request,
_('Instance "%s" launched.') % data["name"])
except:
exceptions.handle(request, _('Unable to launch instance.'))


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

def handle(self, request, data):
image_id = data['image_id']
tenant_id = request.user.tenant_id
try:
image = api.image_get_meta(request, image_id)
if image.owner == request.user.username:
api.image_delete(request, image_id)
else:
messages.info(request, _("Unable to delete image, you are not \
its owner."))
return redirect('dash_images_update', tenant_id, 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: %(image)s: %i(msg)s")
% {"image": image_id, "msg": e.message})
return redirect(request.build_absolute_uri())
redirect = reverse("horizon:nova:images_and_snapshots:index")
exceptions.handle(request,
_('Unable to launch instance: %(exc)s'),
redirect=redirect)
return shortcuts.redirect('horizon:nova:instances_and_volumes:index')
@@ -0,0 +1,81 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# 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.

import logging

from django import shortcuts
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.template import defaultfilters as filters
from novaclient import exceptions as novaclient_exceptions

from horizon import api
from horizon import tables


LOG = logging.getLogger(__name__)


class DeleteImage(tables.DeleteAction):
data_type_singular = _("Image")
data_type_plural = _("Images")

def allowed(self, request, image=None):
if image:
return image.owner == request.user.id
return True

def delete(self, request, obj_id):
api.image_delete(request, obj_id)


class LaunchImage(tables.LinkAction):
name = "launch"
verbose_name = _("Launch")
url = "horizon:nova:images_and_snapshots:images:launch"
attrs = {"class": "ajax-modal"}


class EditImage(tables.LinkAction):
name = "edit"
verbose_name = _("Edit")
url = "horizon:nova:images_and_snapshots:images:update"
attrs = {"class": "ajax-modal"}


def get_image_type(image):
return getattr(image.properties, "image_type", "Image")


class ImagesTable(tables.DataTable):
name = tables.Column("name")
image_type = tables.Column(get_image_type,
verbose_name=_("Type"),
filters=(filters.title,))
status = tables.Column("status", filters=(filters.title,))
public = tables.Column("is_public",
verbose_name=_("Public"),
empty_value=False,
filters=(filters.yesno, filters.capfirst))
container_format = tables.Column("container_format",
verbose_name=_("Container Format"),
filters=(unicode.upper,))

class Meta:
name = "images"
verbose_name = _("Images")
table_actions = (DeleteImage,)
row_actions = (LaunchImage, EditImage, DeleteImage)

0 comments on commit 9f9d4ab

Please sign in to comment.