1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 Nebula, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
from django.core.urlresolvers import reverse
18
from django.utils import html
19
from django.utils.http import urlencode
20
from django.utils import safestring
21
from django.utils.translation import ugettext_lazy as _
23
from horizon import tables
25
from openstack_dashboard import api
26
from openstack_dashboard.api import cinder
28
from openstack_dashboard.dashboards.project.volumes \
29
import tables as volume_tables
32
class DeleteVolumeSnapshot(tables.DeleteAction):
33
data_type_singular = _("Volume Snapshot")
34
data_type_plural = _("Volume Snapshots")
35
action_past = _("Scheduled deletion of %(data_type)s")
36
policy_rules = (("volume", "volume:delete_snapshot"),)
38
def get_policy_target(self, request, datum=None):
41
project_id = getattr(datum,
42
"os-extended-snapshot-attributes:project_id",
44
return {"project_id": project_id}
46
def delete(self, request, obj_id):
47
api.cinder.volume_snapshot_delete(request, obj_id)
50
class CreateVolumeFromSnapshot(tables.LinkAction):
51
name = "create_from_snapshot"
52
verbose_name = _("Create Volume")
53
url = "horizon:project:volumes:create"
54
classes = ("ajax-modal", "btn-camera")
55
policy_rules = (("volume", "volume:create"),)
57
def get_link_url(self, datum):
58
base_url = reverse(self.url)
59
params = urlencode({"snapshot_id": self.table.get_object_id(datum)})
60
return "?".join([base_url, params])
62
def allowed(self, request, volume=None):
63
return volume.status == "available" if volume else False
66
class UpdateRow(tables.Row):
69
def get_data(self, request, snapshot_id):
70
snapshot = cinder.volume_snapshot_get(request, snapshot_id)
71
snapshot._volume = cinder.volume_get(request, snapshot.volume_id)
75
class SnapshotVolumeNameColumn(tables.Column):
76
def get_raw_data(self, snapshot):
77
volume = snapshot._volume
79
volume_name = volume.display_name or volume.id
80
volume_name = html.escape(volume_name)
82
volume_name = _("Unknown")
83
return safestring.mark_safe(volume_name)
85
def get_link_url(self, snapshot):
86
volume = snapshot._volume
89
return reverse(self.link, args=(volume_id,))
92
class VolumeSnapshotsTable(volume_tables.VolumesTableBase):
93
name = tables.Column("display_name",
94
verbose_name=_("Name"),
95
link="horizon:project:images_and_snapshots:detail")
96
volume_name = SnapshotVolumeNameColumn("display_name",
97
verbose_name=_("Volume Name"),
98
link="horizon:project:volumes:detail")
101
name = "volume_snapshots"
102
verbose_name = _("Volume Snapshots")
103
table_actions = (DeleteVolumeSnapshot,)
104
row_actions = (CreateVolumeFromSnapshot, DeleteVolumeSnapshot)
105
row_class = UpdateRow
106
status_columns = ("status",)
107
permissions = ['openstack.services.volume']