Adding rebuild action under Project/Instances

Fixes: bug #1188885
Change-Id: I1871500cac752e5034730aac31188c8ead4b40e7
This commit is contained in:
Justin Shepherd 2013-05-23 12:27:05 -05:00 committed by galstrom21
parent fe659b231a
commit 89b86c266d
8 changed files with 333 additions and 1 deletions
openstack_dashboard
api
dashboards/project/instances

@ -511,6 +511,11 @@ def server_reboot(request, instance_id, hardness=REBOOT_HARD):
server.reboot(hardness)
def server_rebuild(request, instance_id, image_id, password=None):
return novaclient(request).servers.rebuild(instance_id, image_id,
password)
def server_update(request, instance_id, name):
return novaclient(request).servers.update(instance_id, name=name)

@ -0,0 +1,91 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 Openstack Foundation
# All Rights Reserved.
#
# 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.
from django.core.urlresolvers import reverse
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.debug import sensitive_variables
from horizon import exceptions
from horizon import forms
from horizon import messages
from horizon.utils.fields import SelectWidget
from horizon.utils import validators
from openstack_dashboard import api
from openstack_dashboard.dashboards.project.images_and_snapshots.utils \
import get_available_images
def _image_choice_title(img):
gb = filesizeformat(img.bytes)
return '%s (%s)' % (img.display_name, gb)
class RebuildInstanceForm(forms.SelfHandlingForm):
instance_id = forms.CharField(widget=forms.HiddenInput())
image = forms.ChoiceField(label=_("Select Image"),
widget=SelectWidget(attrs={'class': 'image-selector'},
data_attrs=('size', 'display-name'),
transform=_image_choice_title))
password = forms.RegexField(label=_("Rebuild Password"),
required=False,
widget=forms.PasswordInput(render_value=False),
regex=validators.password_validator(),
error_messages={'invalid': validators.password_validator_msg()})
confirm_password = forms.CharField(label=_("Confirm Rebuild Password"),
required=False,
widget=forms.PasswordInput(render_value=False))
def __init__(self, request, *args, **kwargs):
super(RebuildInstanceForm, self).__init__(request, *args, **kwargs)
instance_id = kwargs.get('initial', {}).get('instance_id')
self.fields['instance_id'].initial = instance_id
images = get_available_images(request, request.user.tenant_id)
choices = [(image.id, image.name) for image in images]
if choices:
choices.insert(0, ("", _("Select Image")))
else:
choices.insert(0, ("", _("No images available.")))
self.fields['image'].choices = choices
def clean(self):
cleaned_data = super(RebuildInstanceForm, self).clean()
if 'password' in cleaned_data:
passwd = cleaned_data.get('password')
confirm = cleaned_data.get('confirm_password')
if passwd is not None and confirm is not None:
if passwd != confirm:
raise forms.ValidationError(_("Passwords do not match."))
return cleaned_data
# We have to protect the entire "data" dict because it contains the
# password and confirm_password strings.
@sensitive_variables('data', 'password')
def handle(self, request, data):
instance = data.get('instance_id')
image = data.get('image')
password = data.get('password') or None
try:
api.nova.server_rebuild(request, instance, image, password)
messages.success(request, _('Rebuilding instance %s.') % instance)
except Exception:
redirect = reverse('horizon:project:instances:index')
exceptions.handle(request, _("Unable to rebuild instance."),
redirect=redirect)
return True

@ -332,6 +332,22 @@ class RevertResize(tables.Action):
api.nova.server_revert_resize(request, instance)
class RebuildInstance(tables.LinkAction):
name = "rebuild"
verbose_name = _("Rebuild Instance")
classes = ("btn-rebuild", "ajax-modal")
url = "horizon:project:instances:rebuild"
def allowed(self, request, instance):
return ((instance.status in ACTIVE_STATES
or instance.status == 'SHUTOFF')
and not is_deleting(instance))
def get_link_url(self, datum):
instance_id = self.table.get_object_id(datum)
return urlresolvers.reverse(self.url, args=[instance_id])
class AssociateIP(tables.LinkAction):
name = "associate"
verbose_name = _("Associate Floating IP")
@ -572,4 +588,4 @@ class InstancesTable(tables.DataTable):
EditInstanceSecurityGroups, ConsoleLink, LogLink,
TogglePause, ToggleSuspend, ResizeLink,
SoftRebootInstance, RebootInstance, StopInstance,
TerminateInstance)
RebuildInstance, TerminateInstance)

@ -0,0 +1,28 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% load url from future %}
{% block form_id %}rebuild_instance_form{% endblock %}
{% block form_action %}{% url "horizon:project:instances:rebuild" instance_id %}{% endblock %}
{% block modal_id %}rebuild_instance_modal{% endblock %}
{% block modal-header %}{% trans "Rebuild Instance" %}{% endblock %}
{% block modal-body %}
<div class="left">
<fieldset>
{% include "horizon/common/_form_fields.html" %}
</fieldset>
</div>
<div class="right">
<h3>{% trans "Description" %}:</h3>
<p>{% trans "Select the image to rebuild your instance." %}</p>
<p>{% trans "You may optionally set a password on the rebuilt instance." %}</p>
</div>
{% endblock %}
{% block modal-footer %}
<input class="btn btn-primary pull-right" type="submit" value="{% trans "Rebuild Instance" %}" />
<a href="{% url "horizon:project:instances:index" %}" class="btn secondary cancel close">{% trans "Cancel" %}</a>
{% endblock %}

@ -0,0 +1,11 @@
{% extends "base.html" %}
{% load i18n %}
{% block title %}{% trans "Rebuild Instance" %}{% endblock %}
{% block page_header %}
{% include "horizon/common/_page_header.html" with title=_("Rebuild Instance") %}
{% endblock %}
{% block main %}
{% include "project/instances/_rebuild.html" %}
{% endblock %}

@ -1743,3 +1743,165 @@ class InstanceTests(test.TestCase):
res = self._instance_resize_post(server.id, flavor.id)
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_rebuild_instance_get(self):
server = self.servers.first()
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'is_public': True,
'status': 'active'}) \
.AndReturn([self.images.list(), False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([[], False])
self.mox.ReplayAll()
url = reverse('horizon:project:instances:rebuild', args=[server.id])
res = self.client.get(url)
self.assertTemplateUsed(res, 'project/instances/rebuild.html')
def _instance_rebuild_post(self, server_id, image_id,
password=None, confirm_password=None):
form_data = {'instance_id': server_id,
'image': image_id}
if password is not None:
form_data.update(password=password)
if confirm_password is not None:
form_data.update(confirm_password=confirm_password)
url = reverse('horizon:project:instances:rebuild',
args=[server_id])
return self.client.post(url, form_data)
instance_rebuild_post_stubs = {
api.nova: ('server_rebuild',),
api.glance: ('image_list_detailed',)}
@test.create_stubs(instance_rebuild_post_stubs)
def test_rebuild_instance_post_with_password(self):
server = self.servers.first()
image = self.images.first()
password = u'testpass'
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'is_public': True,
'status': 'active'}) \
.AndReturn([self.images.list(), False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([[], False])
api.nova.server_rebuild(IsA(http.HttpRequest),
server.id,
image.id,
password).AndReturn([])
self.mox.ReplayAll()
res = self._instance_rebuild_post(server.id, image.id,
password=password,
confirm_password=password)
self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs(instance_rebuild_post_stubs)
def test_rebuild_instance_post_with_password_equals_none(self):
server = self.servers.first()
image = self.images.first()
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'is_public': True,
'status': 'active'}) \
.AndReturn([self.images.list(), False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([[], False])
api.nova.server_rebuild(IsA(http.HttpRequest),
server.id,
image.id,
None).AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
res = self._instance_rebuild_post(server.id, image.id,
password=None,
confirm_password=None)
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs(instance_rebuild_post_stubs)
def test_rebuild_instance_post_password_do_not_match(self):
server = self.servers.first()
image = self.images.first()
pass1 = u'somepass'
pass2 = u'notsomepass'
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'is_public': True,
'status': 'active'}) \
.AndReturn([self.images.list(), False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([[], False])
self.mox.ReplayAll()
res = self._instance_rebuild_post(server.id, image.id,
password=pass1,
confirm_password=pass2)
self.assertContains(res, "Passwords do not match.")
@test.create_stubs(instance_rebuild_post_stubs)
def test_rebuild_instance_post_with_empty_string(self):
server = self.servers.first()
image = self.images.first()
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'is_public': True,
'status': 'active'}) \
.AndReturn([self.images.list(), False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([[], False])
api.nova.server_rebuild(IsA(http.HttpRequest),
server.id,
image.id,
None).AndReturn([])
self.mox.ReplayAll()
res = self._instance_rebuild_post(server.id, image.id,
password=u'',
confirm_password=u'')
self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs(instance_rebuild_post_stubs)
def test_rebuild_instance_post_api_exception(self):
server = self.servers.first()
image = self.images.first()
password = u'testpass'
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'is_public': True,
'status': 'active'}) \
.AndReturn([self.images.list(), False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([[], False])
api.nova.server_rebuild(IsA(http.HttpRequest),
server.id,
image.id,
password).AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
res = self._instance_rebuild_post(server.id, image.id,
password=password,
confirm_password=password)
self.assertRedirectsNoFollow(res, INDEX_URL)

@ -25,6 +25,7 @@ from openstack_dashboard.dashboards.project.instances.views import DetailView
from openstack_dashboard.dashboards.project.instances.views import IndexView
from openstack_dashboard.dashboards.project.instances.views import \
LaunchInstanceView
from openstack_dashboard.dashboards.project.instances.views import RebuildView
from openstack_dashboard.dashboards.project.instances.views import ResizeView
from openstack_dashboard.dashboards.project.instances.views import UpdateView
@ -38,6 +39,7 @@ urlpatterns = patterns(VIEW_MOD,
url(r'^launch$', LaunchInstanceView.as_view(), name='launch'),
url(r'^(?P<instance_id>[^/]+)/$', DetailView.as_view(), name='detail'),
url(INSTANCES % 'update', UpdateView.as_view(), name='update'),
url(INSTANCES % 'rebuild', RebuildView.as_view(), name='rebuild'),
url(INSTANCES % 'console', 'console', name='console'),
url(INSTANCES % 'vnc', 'vnc', name='vnc'),
url(INSTANCES % 'spice', 'spice', name='spice'),

@ -31,11 +31,14 @@ from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon import tabs
from horizon import workflows
from openstack_dashboard import api
from openstack_dashboard.dashboards.project.instances.forms import \
RebuildInstanceForm
from openstack_dashboard.dashboards.project.instances.tables import \
InstancesTable
from openstack_dashboard.dashboards.project.instances.tabs import \
@ -176,6 +179,20 @@ class UpdateView(workflows.WorkflowView):
return initial
class RebuildView(forms.ModalFormView):
form_class = RebuildInstanceForm
template_name = 'project/instances/rebuild.html'
success_url = reverse_lazy('horizon:project:instances:index')
def get_context_data(self, **kwargs):
context = super(RebuildView, self).get_context_data(**kwargs)
context['instance_id'] = self.kwargs['instance_id']
return context
def get_initial(self):
return {'instance_id': self.kwargs['instance_id']}
class DetailView(tabs.TabView):
tab_group_class = InstanceDetailTabs
template_name = 'project/instances/detail.html'