From 7f0c6ad276d42289c61761e9cdcaa3720f7be764 Mon Sep 17 00:00:00 2001 From: Gloria Gu Date: Tue, 3 Sep 2019 21:10:56 -0700 Subject: [PATCH] Updated to get quotas data for Modify Quotas dialog Share tab Current Modfiy Quotas action of a project in Identity->Projects for manila ui does not get quotas data for the project. If user updated quotas for manila ui, the saved data will not be shown in the Modify Quotas dialog Share tab. This commit includes: 1) Added a call to get quotas data and also maps the data fields retuned to the UI data fields. 2) Added unit tests. 3) Added a release note. Change-Id: I5fb9cef577b109530fde9a4bafe930ea05f3fed8 Closes-bug: #1842119 Depends-on: https://review.opendev.org/#/c/679513/ (cherry picked from commit c763ebe6cd89657aa820ea14774fa3c95e4d0a6b) --- manila_ui/api/manila.py | 10 +++ .../dashboards/identity/projects/workflows.py | 19 +++++ manila_ui/tests/api/test_manila.py | 28 +++++++ .../tests/dashboards/identity/__init__.py | 0 .../dashboards/identity/projects/__init__.py | 0 .../dashboards/identity/projects/tests.py | 74 +++++++++++++++++++ ...-update-quotas-share-7f229e4e011004cd.yaml | 6 ++ 7 files changed, 137 insertions(+) create mode 100644 manila_ui/tests/dashboards/identity/__init__.py create mode 100644 manila_ui/tests/dashboards/identity/projects/__init__.py create mode 100644 manila_ui/tests/dashboards/identity/projects/tests.py create mode 100644 releasenotes/notes/bug-1842119-fix-get-quotas-for-update-quotas-share-7f229e4e011004cd.yaml diff --git a/manila_ui/api/manila.py b/manila_ui/api/manila.py index 93231576..a24d0e68 100644 --- a/manila_ui/api/manila.py +++ b/manila_ui/api/manila.py @@ -47,6 +47,16 @@ MANILA_QUOTA_FIELDS = { "share_networks", } +# UI field names do not match data field names returned, have +# a map to convert them. +MANILA_QUOTA_FIELDS_DATA_MAP = { + "shares": "shares", + "share_gigabytes": "gigabytes", + "share_snapshots": "snapshots", + "share_snapshot_gigabytes": "snapshot_gigabytes", + "share_networks": "share_networks" +} + def manilaclient(request): insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) diff --git a/manila_ui/dashboards/identity/projects/workflows.py b/manila_ui/dashboards/identity/projects/workflows.py index 82450026..83d2a449 100644 --- a/manila_ui/dashboards/identity/projects/workflows.py +++ b/manila_ui/dashboards/identity/projects/workflows.py @@ -10,8 +10,11 @@ # License for the specific language governing permissions and limitations # under the License. +import logging + from django.utils.translation import ugettext_lazy as _ +from horizon import exceptions from horizon import forms from horizon import workflows @@ -21,6 +24,8 @@ from openstack_dashboard.dashboards.identity.projects \ from manila_ui.api import manila as api_manila +LOG = logging.getLogger(__name__) + class ShareQuotaAction(project_workflows.CommonQuotaAction): shares = forms.IntegerField(min_value=-1, label=_("Shares")) @@ -50,5 +55,19 @@ class UpdateShareQuota(workflows.Step): depends_on = ("project_id", "disabled_quotas") contributes = api_manila.MANILA_QUOTA_FIELDS + def prepare_action_context(self, request, context): + try: + quotas = api_manila.tenant_quota_get( + request, context['project_id']) + for field in api_manila.MANILA_QUOTA_FIELDS: + # Resolve mismatch UI field names and data field names. + data_field = api_manila.MANILA_QUOTA_FIELDS_DATA_MAP[field] + context[field] = quotas.get(data_field).limit + except Exception as ex: + LOG.exception(ex) + exceptions.handle(request, + _('Unable to retrieve share quotas.')) + return context + def allowed(self, request): return base.is_service_enabled(request, 'share') diff --git a/manila_ui/tests/api/test_manila.py b/manila_ui/tests/api/test_manila.py index a5cef1d0..f8839f9d 100644 --- a/manila_ui/tests/api/test_manila.py +++ b/manila_ui/tests/api/test_manila.py @@ -15,6 +15,8 @@ import ddt +from openstack_dashboard.api import base as horizon_api + from manila_ui.api import manila as api from manila_ui.tests import helpers as base @@ -25,6 +27,7 @@ class ManilaApiTests(base.APITestCase): def setUp(self): super(self.__class__, self).setUp() self.id = "fake_id" + self.mock_object(horizon_api, "QuotaSet") @ddt.data((None, True), ("some_fake_sg_id", False)) @ddt.unpack @@ -353,6 +356,31 @@ class ManilaApiTests(base.APITestCase): self.manilaclient.quota_classes.update.assert_called_once_with( api.DEFAULT_QUOTA_NAME, **expected_kwargs) + def test_tenant_quota_get(self): + tenant_id = 'fake_tenant_id' + result = api.tenant_quota_get(self.request, tenant_id) + + self.assertIsNotNone(result) + self.manilaclient.quotas.get.assert_called_once_with(tenant_id) + + @ddt.data({ + 'shares': 24, 'gigabytes': 333, 'snapshots': 14, + 'snapshot_gigabytes': 444, 'share_networks': 14 + }) + @ddt.unpack + def test_ui_data_map(self, **kwargs): + expected_result = { + 'shares': 24, 'share_gigabytes': 333, 'share_snapshots': 14, + 'share_snapshot_gigabytes': 444, 'share_networks': 14 + } + + converted_result_for_ui = {} + for field in api.MANILA_QUOTA_FIELDS: + converted_result_for_ui[field] = ( + kwargs[api.MANILA_QUOTA_FIELDS_DATA_MAP[field]]) + + self.assertEqual(expected_result, converted_result_for_ui) + @ddt.data( {}, {"name": "foo_name"}, diff --git a/manila_ui/tests/dashboards/identity/__init__.py b/manila_ui/tests/dashboards/identity/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/manila_ui/tests/dashboards/identity/projects/__init__.py b/manila_ui/tests/dashboards/identity/projects/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/manila_ui/tests/dashboards/identity/projects/tests.py b/manila_ui/tests/dashboards/identity/projects/tests.py new file mode 100644 index 00000000..ed951f75 --- /dev/null +++ b/manila_ui/tests/dashboards/identity/projects/tests.py @@ -0,0 +1,74 @@ +# (c) Copyright 2019 SUSE LLC +# All rights reserved. + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from django.test.utils import override_settings +from django.urls import reverse + +from horizon.workflows import views +from openstack_dashboard.api import base +from openstack_dashboard.usage import quotas + +from manila_ui.api import manila as manila_api +from manila_ui.tests import helpers as manila_base_test + + +class UpdateShareQuotaWorkflowTests(manila_base_test.BaseAdminViewTests): + + def setUp(self): + super(UpdateShareQuotaWorkflowTests, self).setUp() + self.id = "fake_id" + + # mock functions in manila.py + self.mock_tenant_quota_get = ( + self.mock_object(manila_api, "tenant_quota_get")) + self.mock_tenant_quota_get.return_value = ( + self._get_mock_share_quota_data()) + + # mock functions in quotas.py + self.mock_object(quotas, "get_tenant_quota_data") + self.mock_object(quotas, "get_disabled_quotas").return_value = set() + + def _get_mock_share_quota_data(self): + quota_data = { + 'shares': 24, 'gigabytes': 333, 'snapshots': 14, + 'snapshot_gigabytes': 444, 'share_networks': 14 + } + + return base.QuotaSet(quota_data) + + @override_settings(EXTRA_STEPS={ + 'openstack_dashboard.dashboards.identity.projects.\ + workflows.UpdateQuota': ( + 'manila_ui.dashboards.identity.projects.workflows.UpdateShareQuota' + )} + ) + def test_tenant_quota_get(self): + tenant_id = 'fake_tenant_id' + url = reverse('horizon:identity:projects:update_quotas', + args=[tenant_id]) + res = self.client.get(url) + + workflow = res.context['workflow'] + self.assertTemplateUsed(res, views.WorkflowView.template_name) + step = workflow.get_step("update_share_quotas") + + # Check step UI fields got the fields converted and data correct + self.assertEqual(step.action.initial['project_id'], tenant_id) + self.assertEqual(step.action.initial['shares'], 24) + self.assertEqual(step.action.initial['share_gigabytes'], 333) + self.assertEqual(step.action.initial['share_snapshot_gigabytes'], 444) + self.assertEqual(step.action.initial['share_snapshots'], 14) + self.assertEqual(step.action.initial['share_networks'], 14) diff --git a/releasenotes/notes/bug-1842119-fix-get-quotas-for-update-quotas-share-7f229e4e011004cd.yaml b/releasenotes/notes/bug-1842119-fix-get-quotas-for-update-quotas-share-7f229e4e011004cd.yaml new file mode 100644 index 00000000..778e6ddd --- /dev/null +++ b/releasenotes/notes/bug-1842119-fix-get-quotas-for-update-quotas-share-7f229e4e011004cd.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed issue that the saved Share quota data is not shown in Modify + Quotas modal dialog. +