From a85687cb7ee4563dfb9e3c3c57c6c4e2c400bb56 Mon Sep 17 00:00:00 2001 From: manchandavishal Date: Wed, 6 Mar 2019 14:04:34 +0000 Subject: [PATCH] Add Validation msg in Create Extra Spec Form under Volume Type Panel This patch add a validation message when a user try to 'Create' a extra spec with an existing key name under Volume Type panel. Change-Id: I6a1eac269e616ba0861a41eda5eac1b1a4cdd1d0 Closes-Bug: #1818826 --- .../dashboards/admin/volume_types/extras/forms.py | 14 ++++++++++++++ .../dashboards/admin/volume_types/extras/tests.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/openstack_dashboard/dashboards/admin/volume_types/extras/forms.py b/openstack_dashboard/dashboards/admin/volume_types/extras/forms.py index 8366eef3f7..e667efc63d 100644 --- a/openstack_dashboard/dashboards/admin/volume_types/extras/forms.py +++ b/openstack_dashboard/dashboards/admin/volume_types/extras/forms.py @@ -34,6 +34,20 @@ class CreateExtraSpec(forms.SelfHandlingForm): error_messages=KEY_ERROR_MESSAGES) value = forms.CharField(max_length=255, label=_("Value")) + def clean(self): + data = super(CreateExtraSpec, self).clean() + type_id = self.initial['type_id'] + extra_list = api.cinder.volume_type_extra_get(self.request, + type_id) + for extra in extra_list: + if extra.key.lower() == data['key'].lower(): + error_msg = _('Key with name "%s" already exists. Use Edit to ' + 'update the value, else create key with ' + 'different name.') % data['key'] + raise forms.ValidationError(error_msg) + + return data + def handle(self, request, data): type_id = self.initial['type_id'] try: diff --git a/openstack_dashboard/dashboards/admin/volume_types/extras/tests.py b/openstack_dashboard/dashboards/admin/volume_types/extras/tests.py index 1c09ba36d5..caf038bfab 100644 --- a/openstack_dashboard/dashboards/admin/volume_types/extras/tests.py +++ b/openstack_dashboard/dashboards/admin/volume_types/extras/tests.py @@ -54,7 +54,8 @@ class VolTypeExtrasTests(test.BaseAdminViewTests): self.mock_volume_type_extra_get.assert_called_once_with( test.IsHttpRequest(), vol_type.id) - @test.create_mocks({api.cinder: ('volume_type_extra_set', )}) + @test.create_mocks({api.cinder: ('volume_type_extra_set', + 'volume_type_extra_get')}) def test_extra_create_post(self): vol_type = self.cinder_volume_types.first() create_url = reverse(