From 0e971152f70638f88da805e14741636bfb99a896 Mon Sep 17 00:00:00 2001 From: haixin Date: Fri, 20 Sep 2019 17:11:29 +0800 Subject: [PATCH] Add update share-type name description and/or public access Currently, only the name and public access of share-type is set when the share-type is created, and not allowed to be edited after the share-type is created. We can only set extra spec for share-type. This commit suppport update name,description or public access of share-type. Change-Id: Ia59ae574482fa8015f0151cd983ef880e180640a Partially-Implements: blueprint update-share-type-name-or-description --- doc/source/admin/index.rst | 3 ++ manila_ui/api/manila.py | 13 ++++++- .../dashboards/admin/share_types/forms.py | 22 ++++++++++- .../templates/share_types/_update.html | 3 +- .../dashboards/admin/share_types/views.py | 6 ++- manila_ui/tests/api/test_manila.py | 17 ++++++++- .../admin/share_types/test_forms.py | 38 ++++++++++++++++++- .../dashboards/admin/share_types/tests.py | 14 ++++++- ...-name-or-description-cc5357feee2a3b61.yaml | 6 +++ 9 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 releasenotes/notes/bp-update-share-type-name-or-description-cc5357feee2a3b61.yaml diff --git a/doc/source/admin/index.rst b/doc/source/admin/index.rst index 30417abf..52e83bb8 100644 --- a/doc/source/admin/index.rst +++ b/doc/source/admin/index.rst @@ -55,6 +55,9 @@ Update share type :guilabel:`Extra specs`: To add extra specs, use key=value. To unset extra specs, use key. + :guilabel:`Name`: To update share type name. + :guilabel:`Description`: To update share type description. + :guilabel:`Public`: To update share type visibility. #. Click :guilabel:`Update Share Type` button to confirm your changes. diff --git a/manila_ui/api/manila.py b/manila_ui/api/manila.py index d5bfe21d..a41c9ec8 100644 --- a/manila_ui/api/manila.py +++ b/manila_ui/api/manila.py @@ -28,7 +28,7 @@ from manilaclient import client as manila_client LOG = logging.getLogger(__name__) MANILA_UI_USER_AGENT_REPR = "manila_ui_plugin_for_horizon" -MANILA_VERSION = "2.46" +MANILA_VERSION = "2.50" MANILA_SERVICE_TYPE = "sharev2" # API static values @@ -428,6 +428,17 @@ def share_type_create(request, name, spec_driver_handles_share_servers, is_public=is_public) +def share_type_update(request, share_type_id, name=None, description=None, + is_public=None): + share_type_data = { + 'name': name, + 'description': description, + 'is_public': is_public + } + return manilaclient(request).share_types.get( + share_type_id).update(**share_type_data) + + def share_type_delete(request, share_type_id): return manilaclient(request).share_types.delete(share_type_id) diff --git a/manila_ui/dashboards/admin/share_types/forms.py b/manila_ui/dashboards/admin/share_types/forms.py index 8807ce40..3f15e5a1 100644 --- a/manila_ui/dashboards/admin/share_types/forms.py +++ b/manila_ui/dashboards/admin/share_types/forms.py @@ -93,6 +93,13 @@ class CreateShareType(forms.SelfHandlingForm): class UpdateShareType(forms.SelfHandlingForm): + name = forms.CharField(max_length="255", label=_("Name"), required=True) + description = forms.CharField(max_length="255", label=_("Description"), + required=False) + is_public = forms.BooleanField( + label=_("Public"), required=False, + help_text=("New visibility of the share type. If set to True, share " + "type will be available to all projects in the cloud.")) extra_specs = forms.CharField( required=False, label=_("Extra specs"), widget=forms.widgets.Textarea(attrs=ST_EXTRA_SPECS_FORM_ATTRS)) @@ -105,6 +112,11 @@ class UpdateShareType(forms.SelfHandlingForm): for k, v in self.initial["extra_specs"].items(): es_str += "%s=%s\r\n" % (k, v) self.initial["extra_specs"] = es_str + manila_features = getattr(settings, 'OPENSTACK_MANILA_FEATURES', {}) + self.enable_public_share_type_creation = manila_features.get( + 'enable_public_share_type_creation', True) + if not self.enable_public_share_type_creation: + self.fields.pop('is_public') def handle(self, request, data): try: @@ -121,7 +133,13 @@ class UpdateShareType(forms.SelfHandlingForm): if to_unset: manila.share_type_unset_extra_specs( request, self.initial["id"], to_unset) - msg = _("Successfully updated extra specs for share type '%s'.") + name = data.get("name", None) + description = data.get("description", None) + is_public = (self.enable_public_share_type_creation and + data.get("is_public", True)) + manila.share_type_update( + request, self.initial["id"], name, description, is_public) + msg = _("Successfully updated share type '%s'.") msg = msg % self.initial['name'] messages.success(request, msg) return True @@ -130,6 +148,6 @@ class UpdateShareType(forms.SelfHandlingForm): self.api_error(e.messages[0]) return False except Exception: - msg = _("Unable to update extra_specs for share type.") + msg = _("Unable to update share type.") exceptions.handle(request, msg) return False diff --git a/manila_ui/dashboards/admin/share_types/templates/share_types/_update.html b/manila_ui/dashboards/admin/share_types/templates/share_types/_update.html index 5fc83838..ad0ba7b8 100644 --- a/manila_ui/dashboards/admin/share_types/templates/share_types/_update.html +++ b/manila_ui/dashboards/admin/share_types/templates/share_types/_update.html @@ -2,7 +2,8 @@ {% load i18n %} {% block modal-body-right %}

{% trans "Description" %}:

-

+

{% trans "From here you can modify name, description, extra specs and visibility of a share type." %} +

Extra specs field:
{% trans "Here can be modified extra-specs for share type." %}
{% trans "One line - one action. Empty strings will be ignored." %}
{% trans "To add extra-specs use:" %} diff --git a/manila_ui/dashboards/admin/share_types/views.py b/manila_ui/dashboards/admin/share_types/views.py index 8bbead1c..afc27670 100644 --- a/manila_ui/dashboards/admin/share_types/views.py +++ b/manila_ui/dashboards/admin/share_types/views.py @@ -132,8 +132,12 @@ class UpdateShareTypeView(forms.ModalFormView): def get_initial(self): share_type = self.get_object() - return { + st_info = { "id": self.kwargs["share_type_id"], "name": share_type.name, "extra_specs": share_type.extra_specs, + "is_public": share_type.is_public, } + if hasattr(share_type, 'description'): + st_info['description'] = share_type.description + return st_info diff --git a/manila_ui/tests/api/test_manila.py b/manila_ui/tests/api/test_manila.py index cce0f670..ee1d037f 100644 --- a/manila_ui/tests/api/test_manila.py +++ b/manila_ui/tests/api/test_manila.py @@ -237,10 +237,25 @@ class ManilaApiTests(base.APITestCase): def test_migration_cancel(self): api.migration_cancel(self.request, 'fake_share') - self.manilaclient.shares.migration_cancel.assert_called_once_with( 'fake_share') + @ddt.data( + ('name_updated_1', 'description_updated', True), + ('name_updated_2', 'description_updated', False), + ('name_updated_3', None, True), + ) + @ddt.unpack + def test_share_type_update( + self, name, description, is_public): + api.share_type_update(self.request, self.id, name, + description, is_public) + + self.manilaclient.share_types.get.return_value.update.\ + assert_called_once_with(name=name, + description=description, + is_public=is_public) + def test_migration_get_progress(self): api.migration_get_progress(self.request, 'fake_share') diff --git a/manila_ui/tests/dashboards/admin/share_types/test_forms.py b/manila_ui/tests/dashboards/admin/share_types/test_forms.py index e0911464..93a5d609 100644 --- a/manila_ui/tests/dashboards/admin/share_types/test_forms.py +++ b/manila_ui/tests/dashboards/admin/share_types/test_forms.py @@ -79,7 +79,7 @@ class ManilaDashboardsAdminSharesUpdateShareTypeFormTests(base.APITestCase): self.assertTrue(result) mock_horizon_messages_success.assert_called_once_with( mock.ANY, mock.ANY) - self.manilaclient.share_types.get.assert_called_once_with( + self.manilaclient.share_types.get.assert_called_with( initial['id']) self.manilaclient.share_types.get.return_value.set_keys.\ assert_called_once_with({'foo': 'bar'}) @@ -160,6 +160,42 @@ class ManilaDashboardsAdminSharesUpdateShareTypeFormTests(base.APITestCase): mock_horizon_exceptions_handle.assert_called_once_with( self.request, mock.ANY) + @ddt.data( + ('name_updated_1', 'description_updated', True), + ('name_updated_2', 'description_updated', False), + ('name_updated_3', None, True), + ) + @ddt.unpack + @mock.patch('horizon.messages.success') + def test_update_share_type_name_description_public( + self, name, description, is_public, + mock_horizon_messages_success): + initial = { + 'id': 'fake_id', + 'name': 'fake_name', + 'description': 'fake_description', + 'is_public': True, + 'extra_specs': {} + } + form = self._get_form(initial) + data = { + 'name': name, + 'description': description, + 'is_public': is_public, + 'extra_specs': '', + } + result = form.handle(self.request, data) + + self.assertIs(True, result) + mock_horizon_messages_success.assert_called_once_with( + mock.ANY, mock.ANY) + self.manilaclient.share_types.get.assert_called_once_with( + initial['id']) + self.manilaclient.share_types.get.return_value.update.\ + assert_called_once_with(name=data['name'], + description=data['description'], + is_public=data['is_public']) + @ddt.ddt class ManilaDashboardsAdminSharesCreateShareTypeFormTests(base.APITestCase): diff --git a/manila_ui/tests/dashboards/admin/share_types/tests.py b/manila_ui/tests/dashboards/admin/share_types/tests.py index 779ea00b..2320c2a3 100644 --- a/manila_ui/tests/dashboards/admin/share_types/tests.py +++ b/manila_ui/tests/dashboards/admin/share_types/tests.py @@ -76,15 +76,25 @@ class ShareTypeTests(test.BaseAdminViewTests): def test_update_share_type_post(self): data = { - 'extra_specs': 'driver_handles_share_servers=True' + 'name': 'share_type_update', + 'description': 'share_type_description', + 'is_public': True, + 'extra_specs': 'fork_key=fork_val' } form_data = { - 'extra_specs': {'driver_handles_share_servers': 'True'}, + 'name': 'share_type_update', + 'description': 'share_type_description', + 'is_public': True, + 'extra_specs': {'fork_key': 'fork_val'}, } self.mock_object(api_manila, "share_type_set_extra_specs") + self.mock_object(api_manila, "share_type_update") res = self.client.post(self.url, data) api_manila.share_type_set_extra_specs.assert_called_once_with( mock.ANY, self.share_type.id, form_data['extra_specs']) + api_manila.share_type_update.assert_called_once_with( + mock.ANY, self.share_type.id, data['name'], data['description'], + data['is_public']) self.assertRedirectsNoFollow(res, INDEX_URL) diff --git a/releasenotes/notes/bp-update-share-type-name-or-description-cc5357feee2a3b61.yaml b/releasenotes/notes/bp-update-share-type-name-or-description-cc5357feee2a3b61.yaml new file mode 100644 index 00000000..294cafce --- /dev/null +++ b/releasenotes/notes/bp-update-share-type-name-or-description-cc5357feee2a3b61.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + The ``name``, ``description`` and/or ``share_type_access:is_public`` + attributes of share types can be updated with API version ``2.50`` + and beyond.