summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2017-12-05 01:06:33 +0000
committerGerrit Code Review <review@openstack.org>2017-12-05 01:06:33 +0000
commit020be60db6e1fc2fbf0ca871c12d1fead04b06e7 (patch)
tree29d96f075d52d45f80254a91a59fb50efc135153
parenta79dd35beedd8f1fb9107bea7b5c9997022f5ad2 (diff)
parent259cb419e32739c4cde61c092b4dae9c28705eb9 (diff)
Merge "Display volume snapshots only in volume field"
-rw-r--r--muranodashboard/dynamic_ui/fields.py20
-rw-r--r--muranodashboard/tests/unit/dynamic_ui/test_fields.py33
2 files changed, 39 insertions, 14 deletions
diff --git a/muranodashboard/dynamic_ui/fields.py b/muranodashboard/dynamic_ui/fields.py
index 2733e63..6fbd369 100644
--- a/muranodashboard/dynamic_ui/fields.py
+++ b/muranodashboard/dynamic_ui/fields.py
@@ -548,9 +548,11 @@ class AZoneChoiceField(ChoiceField):
548 548
549class VolumeChoiceField(ChoiceField): 549class VolumeChoiceField(ChoiceField):
550 def __init__(self, 550 def __init__(self,
551 include_volumes=True,
551 include_snapshots=True, 552 include_snapshots=True,
552 *args, 553 *args,
553 **kwargs): 554 **kwargs):
555 self.include_volumes = include_volumes
554 self.include_snapshots = include_snapshots 556 self.include_snapshots = include_snapshots
555 super(VolumeChoiceField, self).__init__(*args, **kwargs) 557 super(VolumeChoiceField, self).__init__(*args, **kwargs)
556 558
@@ -558,14 +560,16 @@ class VolumeChoiceField(ChoiceField):
558 def update(self, request, **kwargs): 560 def update(self, request, **kwargs):
559 """This widget allows selection of Volumes and Volume Snapshots""" 561 """This widget allows selection of Volumes and Volume Snapshots"""
560 available = {'status': cinder.VOLUME_STATE_AVAILABLE} 562 available = {'status': cinder.VOLUME_STATE_AVAILABLE}
561 try: 563 choices = []
562 choices = [(volume.id, volume.name) 564
563 for volume in cinder.volume_list(request, 565 if self.include_volumes:
564 search_opts=available)] 566 try:
565 except Exception: 567 choices.extend((volume.id, volume.name)
566 choices = [] 568 for volume in cinder.volume_list(request,
567 exceptions.handle(request, 569 search_opts=available))
568 _("Unable to retrieve volume list.")) 570 except Exception:
571 exceptions.handle(request,
572 _("Unable to retrieve volume list."))
569 573
570 if self.include_snapshots: 574 if self.include_snapshots:
571 try: 575 try:
diff --git a/muranodashboard/tests/unit/dynamic_ui/test_fields.py b/muranodashboard/tests/unit/dynamic_ui/test_fields.py
index 4873786..e470fc4 100644
--- a/muranodashboard/tests/unit/dynamic_ui/test_fields.py
+++ b/muranodashboard/tests/unit/dynamic_ui/test_fields.py
@@ -693,7 +693,7 @@ class TestVolumeChoiceField(testtools.TestCase):
693 baz_snap.configure_mock(name='baz_snap', id='baz_id', status='error') 693 baz_snap.configure_mock(name='baz_snap', id='baz_id', status='error')
694 mock_cinder.volume_list.return_value = [foo_vol] 694 mock_cinder.volume_list.return_value = [foo_vol]
695 mock_cinder.volume_snapshot_list.return_value = [bar_snap] 695 mock_cinder.volume_snapshot_list.return_value = [bar_snap]
696 volume_choice_field = fields.VolumeChoiceField(include_snapshots=True) 696 volume_choice_field = fields.VolumeChoiceField()
697 volume_choice_field.choices = [] 697 volume_choice_field.choices = []
698 volume_choice_field.update(self.request) 698 volume_choice_field.update(self.request)
699 699
@@ -727,6 +727,26 @@ class TestVolumeChoiceField(testtools.TestCase):
727 self.assertEqual(sorted(expected_choices), 727 self.assertEqual(sorted(expected_choices),
728 sorted(volume_choice_field.choices)) 728 sorted(volume_choice_field.choices))
729 729
730 @mock.patch.object(fields, 'cinder')
731 def test_update_withoutvolume(self, mock_cinder):
732 foo_vol = mock.Mock()
733 baz_snap = mock.Mock()
734 foo_vol.configure_mock(name='foo_vol', id='foo_id', status='available')
735 baz_snap.configure_mock(name='baz_snap', id='baz_id',
736 status='available')
737 mock_cinder.volume_list.return_value = [foo_vol]
738 mock_cinder.volume_snapshot_list.return_value = [baz_snap]
739 volume_choice_field = fields.VolumeChoiceField(include_volumes=False)
740 volume_choice_field.choices = []
741 volume_choice_field.update(self.request)
742
743 expected_choices = [
744 ('', _('Select volume')), ('baz_id', 'baz_snap')
745 ]
746
747 self.assertEqual(sorted(expected_choices),
748 sorted(volume_choice_field.choices))
749
730 @mock.patch.object(fields, 'exceptions') 750 @mock.patch.object(fields, 'exceptions')
731 @mock.patch.object(fields, 'cinder') 751 @mock.patch.object(fields, 'cinder')
732 def test_update_except_snapshot_list_exception(self, mock_cinder, 752 def test_update_except_snapshot_list_exception(self, mock_cinder,
@@ -737,7 +757,8 @@ class TestVolumeChoiceField(testtools.TestCase):
737 bar_vol.configure_mock(name='bar_vol', id='bar_id', status='error') 757 bar_vol.configure_mock(name='bar_vol', id='bar_id', status='error')
738 mock_cinder.volume_list.return_value = [foo_vol] 758 mock_cinder.volume_list.return_value = [foo_vol]
739 mock_cinder.volume_snapshot_list.side_effect = Exception 759 mock_cinder.volume_snapshot_list.side_effect = Exception
740 volume_choice_field = fields.VolumeChoiceField(include_snapshots=True) 760 volume_choice_field = fields.VolumeChoiceField(include_volumes=True,
761 include_snapshots=True)
741 volume_choice_field.choices = [] 762 volume_choice_field.choices = []
742 volume_choice_field.update(self.request) 763 volume_choice_field.update(self.request)
743 764
@@ -755,13 +776,12 @@ class TestVolumeChoiceField(testtools.TestCase):
755 def test_update_except_volume_list_exception(self, mock_cinder, 776 def test_update_except_volume_list_exception(self, mock_cinder,
756 mock_exceptions): 777 mock_exceptions):
757 bar_snap = mock.Mock() 778 bar_snap = mock.Mock()
758 baz_snap = mock.Mock()
759 bar_snap.configure_mock(name='bar_snap', id='bar_id', 779 bar_snap.configure_mock(name='bar_snap', id='bar_id',
760 status='available') 780 status='available')
761 baz_snap.configure_mock(name='baz_snap', id='baz_id', status='error')
762 mock_cinder.volume_list.side_effect = Exception 781 mock_cinder.volume_list.side_effect = Exception
763 mock_cinder.volume_snapshot_list.return_value = [bar_snap] 782 mock_cinder.volume_snapshot_list.return_value = [bar_snap]
764 volume_choice_field = fields.VolumeChoiceField(include_snapshots=True) 783 volume_choice_field = fields.VolumeChoiceField(include_volumes=True,
784 include_snapshots=True)
765 volume_choice_field.choices = [] 785 volume_choice_field.choices = []
766 volume_choice_field.update(self.request) 786 volume_choice_field.update(self.request)
767 787
@@ -778,7 +798,8 @@ class TestVolumeChoiceField(testtools.TestCase):
778 def test_update_except_exception(self, mock_cinder, mock_exceptions): 798 def test_update_except_exception(self, mock_cinder, mock_exceptions):
779 mock_cinder.volume_list.side_effect = Exception 799 mock_cinder.volume_list.side_effect = Exception
780 mock_cinder.volume_snapshot_list.side_effect = Exception 800 mock_cinder.volume_snapshot_list.side_effect = Exception
781 volume_choice_field = fields.VolumeChoiceField(include_snapshots=True) 801 volume_choice_field = fields.VolumeChoiceField(include_volumes=True,
802 include_snapshots=True)
782 volume_choice_field.choices = [] 803 volume_choice_field.choices = []
783 volume_choice_field.update(self.request) 804 volume_choice_field.update(self.request)
784 805