Add update method of volume name and description

Change-Id: I19596f1b83b061a47a8784962e6f74f1f43048cf
Closes-Bug: #1264411
This commit is contained in:
Zhenguo Niu 2013-12-31 11:26:02 +08:00
parent 586002b7f2
commit dcd166e1c8
9 changed files with 138 additions and 1 deletions

View File

@ -105,6 +105,13 @@ def volume_delete(request, volume_id):
return cinderclient(request).volumes.delete(volume_id)
def volume_update(request, volume_id, name, description):
vol_data = {'display_name': name,
'display_description': description}
return cinderclient(request).volumes.update(volume_id,
**vol_data)
def volume_snapshot_get(request, snapshot_id):
return cinderclient(request).volume_snapshots.get(snapshot_id)

View File

@ -6,6 +6,7 @@
"admin_api": [["is_admin:True"]],
"volume:create": [],
"volume:update": [],
"volume:delete": [["rule:default"]],
"volume:get_all": [],
"volume:get_volume_metadata": [],

View File

@ -400,3 +400,24 @@ class CreateSnapshotForm(forms.SelfHandlingForm):
exceptions.handle(request,
_('Unable to create volume snapshot.'),
redirect=redirect)
class UpdateForm(forms.SelfHandlingForm):
name = forms.CharField(max_length="255", label=_("Volume Name"))
description = forms.CharField(widget=forms.Textarea,
label=_("Description"), required=False)
def handle(self, request, data):
volume_id = self.initial['volume_id']
try:
cinder.volume_update(request, volume_id, data['name'],
data['description'])
message = _('Updating volume "%s"') % data['name']
messages.info(request, message)
return True
except Exception:
redirect = reverse("horizon:project:volumes:index")
exceptions.handle(request,
_('Unable to update volume.'),
redirect=redirect)

View File

@ -126,6 +126,23 @@ class CreateSnapshot(tables.LinkAction):
return volume.status in ("available", "in-use")
class EditVolume(tables.LinkAction):
name = "edit"
verbose_name = _("Edit Volume")
url = "horizon:project:volumes:update"
classes = ("ajax-modal", "btn-edit")
policy_rules = (("volume", "volume:update"),)
def get_policy_target(self, request, datum=None):
project_id = None
if datum:
project_id = getattr(datum, "os-vol-tenant-attr:tenant_id", None)
return {"project_id": project_id}
def allowed(self, request, volume=None):
return volume.status in ("available", "in-use")
class UpdateRow(tables.Row):
ajax = True
@ -238,7 +255,8 @@ class VolumesTable(VolumesTableBase):
status_columns = ["status"]
row_class = UpdateRow
table_actions = (CreateVolume, DeleteVolume, VolumesFilterAction)
row_actions = (EditAttachments, CreateSnapshot, DeleteVolume)
row_actions = (EditAttachments, EditVolume,
CreateSnapshot, DeleteVolume)
class DetachVolume(tables.BatchAction):

View File

@ -0,0 +1,26 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% load url from future %}
{% block form_id %}{% endblock %}
{% block form_action %}{% url 'horizon:project:volumes:update' volume.id %}{% endblock %}
{% block modal_id %}update_volume_modal{% endblock %}
{% block modal-header %}{% trans "Edit Volume" %}{% 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 "From here you can modify name and description of a volume." %}</p>
</div>
{% endblock %}
{% block modal-footer %}
<input class="btn btn-primary pull-right" type="submit" value="{% trans "Edit Volume" %}" />
<a href="{% url 'horizon:project:volumes:index' %}" class="btn secondary cancel close">{% trans "Cancel" %}</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Edit Volume" %}{% endblock %}
{% block page_header %}
{% include "horizon/common/_page_header.html" with title=_("Edit Volume") %}
{% endblock page_header %}
{% block main %}
{% include 'project/volumes/_update.html' %}
{% endblock %}

View File

@ -837,3 +837,25 @@ class VolumeViewTests(test.TestCase):
res = self.client.get(url)
self.assertRedirectsNoFollow(res, VOLUME_INDEX_URL)
@test.create_stubs({cinder: ('volume_update',
'volume_get',)})
def test_update_volume(self):
volume = self.volumes.get(name="my_volume")
cinder.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume)
cinder.volume_update(IsA(http.HttpRequest),
volume.id,
volume.display_name,
volume.display_description)
self.mox.ReplayAll()
formData = {'method': 'UpdateForm',
'name': volume.display_name,
'description': volume.display_description}
url = reverse('horizon:project:volumes:update',
args=[volume.id])
res = self.client.post(url, formData)
self.assertRedirectsNoFollow(res, VOLUME_INDEX_URL)

View File

@ -32,4 +32,7 @@ urlpatterns = patterns('openstack_dashboard.dashboards.project.volumes.views',
url(r'^(?P<volume_id>[^/]+)/$',
views.DetailView.as_view(),
name='detail'),
url(r'^(?P<volume_id>[^/]+)/update/$',
views.UpdateView.as_view(),
name='update'),
)

View File

@ -160,6 +160,34 @@ class CreateSnapshotView(forms.ModalFormView):
return {'volume_id': self.kwargs["volume_id"]}
class UpdateView(forms.ModalFormView):
form_class = project_forms.UpdateForm
template_name = 'project/volumes/update.html'
success_url = reverse_lazy("horizon:project:volumes:index")
def get_object(self):
if not hasattr(self, "_object"):
vol_id = self.kwargs['volume_id']
try:
self._object = cinder.volume_get(self.request, vol_id)
except Exception:
msg = _('Unable to retrieve volume.')
url = reverse('horizon:project:volumes:index')
exceptions.handle(self.request, msg, redirect=url)
return self._object
def get_context_data(self, **kwargs):
context = super(UpdateView, self).get_context_data(**kwargs)
context['volume'] = self.get_object()
return context
def get_initial(self):
volume = self.get_object()
return {'volume_id': self.kwargs["volume_id"],
'name': volume.display_name,
'description': volume.display_description}
class EditAttachmentsView(tables.DataTableView, forms.ModalFormView):
table_class = project_tables.AttachmentsTable
form_class = project_forms.AttachForm