Display volume snapshots only in volume field
Add an attribute include_volumes to the volume form element. If set to true (the default -- maintains backward compatibility), the list will include available volumes. If set to false, the list will not include volumes. To display available snapshots, set attributes include_snapshots to true and include_volumes to false. DocImpact Closes-Bug: #1732208 Change-Id: I243326e9a68eafcdf367a4fbbdaea7b447550b9a
This commit is contained in:
parent
63d2226fd4
commit
259cb419e3
|
@ -548,9 +548,11 @@ class AZoneChoiceField(ChoiceField):
|
|||
|
||||
class VolumeChoiceField(ChoiceField):
|
||||
def __init__(self,
|
||||
include_volumes=True,
|
||||
include_snapshots=True,
|
||||
*args,
|
||||
**kwargs):
|
||||
self.include_volumes = include_volumes
|
||||
self.include_snapshots = include_snapshots
|
||||
super(VolumeChoiceField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
@ -558,14 +560,16 @@ class VolumeChoiceField(ChoiceField):
|
|||
def update(self, request, **kwargs):
|
||||
"""This widget allows selection of Volumes and Volume Snapshots"""
|
||||
available = {'status': cinder.VOLUME_STATE_AVAILABLE}
|
||||
try:
|
||||
choices = [(volume.id, volume.name)
|
||||
for volume in cinder.volume_list(request,
|
||||
search_opts=available)]
|
||||
except Exception:
|
||||
choices = []
|
||||
exceptions.handle(request,
|
||||
_("Unable to retrieve volume list."))
|
||||
choices = []
|
||||
|
||||
if self.include_volumes:
|
||||
try:
|
||||
choices.extend((volume.id, volume.name)
|
||||
for volume in cinder.volume_list(request,
|
||||
search_opts=available))
|
||||
except Exception:
|
||||
exceptions.handle(request,
|
||||
_("Unable to retrieve volume list."))
|
||||
|
||||
if self.include_snapshots:
|
||||
try:
|
||||
|
|
|
@ -693,7 +693,7 @@ class TestVolumeChoiceField(testtools.TestCase):
|
|||
baz_snap.configure_mock(name='baz_snap', id='baz_id', status='error')
|
||||
mock_cinder.volume_list.return_value = [foo_vol]
|
||||
mock_cinder.volume_snapshot_list.return_value = [bar_snap]
|
||||
volume_choice_field = fields.VolumeChoiceField(include_snapshots=True)
|
||||
volume_choice_field = fields.VolumeChoiceField()
|
||||
volume_choice_field.choices = []
|
||||
volume_choice_field.update(self.request)
|
||||
|
||||
|
@ -727,6 +727,26 @@ class TestVolumeChoiceField(testtools.TestCase):
|
|||
self.assertEqual(sorted(expected_choices),
|
||||
sorted(volume_choice_field.choices))
|
||||
|
||||
@mock.patch.object(fields, 'cinder')
|
||||
def test_update_withoutvolume(self, mock_cinder):
|
||||
foo_vol = mock.Mock()
|
||||
baz_snap = mock.Mock()
|
||||
foo_vol.configure_mock(name='foo_vol', id='foo_id', status='available')
|
||||
baz_snap.configure_mock(name='baz_snap', id='baz_id',
|
||||
status='available')
|
||||
mock_cinder.volume_list.return_value = [foo_vol]
|
||||
mock_cinder.volume_snapshot_list.return_value = [baz_snap]
|
||||
volume_choice_field = fields.VolumeChoiceField(include_volumes=False)
|
||||
volume_choice_field.choices = []
|
||||
volume_choice_field.update(self.request)
|
||||
|
||||
expected_choices = [
|
||||
('', _('Select volume')), ('baz_id', 'baz_snap')
|
||||
]
|
||||
|
||||
self.assertEqual(sorted(expected_choices),
|
||||
sorted(volume_choice_field.choices))
|
||||
|
||||
@mock.patch.object(fields, 'exceptions')
|
||||
@mock.patch.object(fields, 'cinder')
|
||||
def test_update_except_snapshot_list_exception(self, mock_cinder,
|
||||
|
@ -737,7 +757,8 @@ class TestVolumeChoiceField(testtools.TestCase):
|
|||
bar_vol.configure_mock(name='bar_vol', id='bar_id', status='error')
|
||||
mock_cinder.volume_list.return_value = [foo_vol]
|
||||
mock_cinder.volume_snapshot_list.side_effect = Exception
|
||||
volume_choice_field = fields.VolumeChoiceField(include_snapshots=True)
|
||||
volume_choice_field = fields.VolumeChoiceField(include_volumes=True,
|
||||
include_snapshots=True)
|
||||
volume_choice_field.choices = []
|
||||
volume_choice_field.update(self.request)
|
||||
|
||||
|
@ -755,13 +776,12 @@ class TestVolumeChoiceField(testtools.TestCase):
|
|||
def test_update_except_volume_list_exception(self, mock_cinder,
|
||||
mock_exceptions):
|
||||
bar_snap = mock.Mock()
|
||||
baz_snap = mock.Mock()
|
||||
bar_snap.configure_mock(name='bar_snap', id='bar_id',
|
||||
status='available')
|
||||
baz_snap.configure_mock(name='baz_snap', id='baz_id', status='error')
|
||||
mock_cinder.volume_list.side_effect = Exception
|
||||
mock_cinder.volume_snapshot_list.return_value = [bar_snap]
|
||||
volume_choice_field = fields.VolumeChoiceField(include_snapshots=True)
|
||||
volume_choice_field = fields.VolumeChoiceField(include_volumes=True,
|
||||
include_snapshots=True)
|
||||
volume_choice_field.choices = []
|
||||
volume_choice_field.update(self.request)
|
||||
|
||||
|
@ -778,7 +798,8 @@ class TestVolumeChoiceField(testtools.TestCase):
|
|||
def test_update_except_exception(self, mock_cinder, mock_exceptions):
|
||||
mock_cinder.volume_list.side_effect = Exception
|
||||
mock_cinder.volume_snapshot_list.side_effect = Exception
|
||||
volume_choice_field = fields.VolumeChoiceField(include_snapshots=True)
|
||||
volume_choice_field = fields.VolumeChoiceField(include_volumes=True,
|
||||
include_snapshots=True)
|
||||
volume_choice_field.choices = []
|
||||
volume_choice_field.update(self.request)
|
||||
|
||||
|
|
Loading…
Reference in New Issue