From 3a3cb681c912863bca586f4354b9a0205f9b25f4 Mon Sep 17 00:00:00 2001 From: Ernest Millan Date: Wed, 16 Nov 2016 16:00:17 -0800 Subject: [PATCH] Ensures proper network name in subnet error message We need to pass network object to _create_subnet() to show proper network name in subnet error message. The network itself has been retrieved in the view code, so what we need to do is to make it available in the workflow code. This commit makes 'network' information available in the workflow context_seed dict and CreateSubnet handle() method can use it. Also removes the unnecessary network_get in the admin subnet create form. we can retrieve a network object from context_seed['network'] as we do in the project side code now. Change-Id: Ib2162814cbc7263fc03a9da0319c0b2b38732cc5 Closes-bug: #1505414 Co-Authored-By: Akihiro Motoki --- .../admin/networks/subnets/tests.py | 10 +++---- .../admin/networks/subnets/workflows.py | 29 +++++++------------ .../project/networks/subnets/tests.py | 2 +- .../project/networks/subnets/views.py | 3 +- .../project/networks/subnets/workflows.py | 10 ++++--- 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/openstack_dashboard/dashboards/admin/networks/subnets/tests.py b/openstack_dashboard/dashboards/admin/networks/subnets/tests.py index 0e71f725ca..8b889f7228 100644 --- a/openstack_dashboard/dashboards/admin/networks/subnets/tests.py +++ b/openstack_dashboard/dashboards/admin/networks/subnets/tests.py @@ -126,9 +126,8 @@ class NetworkSubnetTests(test.BaseAdminViewTests): redir_url = reverse(NETWORKS_DETAIL_URL, args=[subnet.network_id]) self.assertRedirectsNoFollow(res, redir_url) - self.assert_mock_multiple_calls_with_same_arguments( - self.mock_network_get, 2, - mock.call(test.IsHttpRequest(), network.id)) + self.mock_network_get.assert_called_once_with(test.IsHttpRequest(), + network.id) self._check_is_extension_supported({'subnet_allocation': 1}) self.mock_subnetpool_list.assert_called_once_with(test.IsHttpRequest()) self.mock_subnet_create.assert_called_once_with( @@ -184,9 +183,8 @@ class NetworkSubnetTests(test.BaseAdminViewTests): redir_url = reverse(NETWORKS_DETAIL_URL, args=[subnet.network_id]) self.assertRedirectsNoFollow(res, redir_url) - self.assert_mock_multiple_calls_with_same_arguments( - self.mock_network_get, 2, - mock.call(test.IsHttpRequest(), network.id)) + self.mock_network_get.assert_called_once_with(test.IsHttpRequest(), + network.id) self._check_is_extension_supported({'subnet_allocation': 1}) self.mock_subnetpool_list.assert_called_once_with(test.IsHttpRequest()) self.mock_subnet_create.assert_called_once_with( diff --git a/openstack_dashboard/dashboards/admin/networks/subnets/workflows.py b/openstack_dashboard/dashboards/admin/networks/subnets/workflows.py index 72e2521058..cec55876a8 100644 --- a/openstack_dashboard/dashboards/admin/networks/subnets/workflows.py +++ b/openstack_dashboard/dashboards/admin/networks/subnets/workflows.py @@ -17,9 +17,6 @@ import logging from django.urls import reverse from django.utils.translation import ugettext_lazy as _ -from horizon import exceptions - -from openstack_dashboard import api from openstack_dashboard.dashboards.project.networks.subnets \ import workflows as project_workflows from openstack_dashboard.dashboards.project.networks import workflows \ @@ -52,27 +49,21 @@ class CreateSubnet(project_workflows.CreateSubnet): def get_success_url(self): return reverse("horizon:admin:networks:detail", - args=(self.context.get('network_id'),)) + args=(self.context['network'].id,)) def get_failure_url(self): return reverse("horizon:admin:networks:detail", - args=(self.context.get('network_id'),)) + args=(self.context['network'].id,)) def handle(self, request, data): - try: - # We must specify tenant_id of the network which a subnet is - # created for if admin user does not belong to the tenant. - network = api.neutron.network_get(request, - self.context['network_id']) - except Exception as e: - LOG.info('Failed to retrieve network %(id)s for a subnet: %(exc)s', - {'id': data['network_id'], 'exc': e}) - msg = (_('Failed to retrieve network %s for a subnet') % - data['network_id']) - redirect = self.get_failure_url() - exceptions.handle(request, msg, redirect=redirect) - subnet = self._create_subnet(request, data, - tenant_id=network.tenant_id) + network = self.context_seed['network'] + # NOTE: network argument is required to show error message correctly. + # NOTE: We must specify tenant_id of the network which a subnet is + # created for if admin user does not belong to the tenant. + subnet = self._create_subnet( + request, data, + network=network, + tenant_id=network.tenant_id) return True if subnet else False diff --git a/openstack_dashboard/dashboards/project/networks/subnets/tests.py b/openstack_dashboard/dashboards/project/networks/subnets/tests.py index 57f3055a1a..4300c8e499 100644 --- a/openstack_dashboard/dashboards/project/networks/subnets/tests.py +++ b/openstack_dashboard/dashboards/project/networks/subnets/tests.py @@ -185,7 +185,7 @@ class NetworkSubnetTests(test.TestCase): def test_subnet_create_post_with_additional_attributes(self): network = self.networks.list()[1] subnet = self.subnets.list()[2] - self.mock_network_get.return_value = self.networks.first() + self.mock_network_get.return_value = network self.mock_is_extension_supported.return_value = True self.mock_subnetpool_list.return_value = self.subnetpools.list() self.mock_subnet_create.return_value = subnet diff --git a/openstack_dashboard/dashboards/project/networks/subnets/views.py b/openstack_dashboard/dashboards/project/networks/subnets/views.py index 0ab085da38..13e54c57eb 100644 --- a/openstack_dashboard/dashboards/project/networks/subnets/views.py +++ b/openstack_dashboard/dashboards/project/networks/subnets/views.py @@ -52,8 +52,7 @@ class CreateView(DefaultSubnetWorkflowMixin, workflows.WorkflowView): def get_initial(self): network = self.get_object() - return {"network_id": self.kwargs['network_id'], - "network_name": network.name_or_id, + return {"network": network, "dns_nameservers": self.get_default_dns_servers()} diff --git a/openstack_dashboard/dashboards/project/networks/subnets/workflows.py b/openstack_dashboard/dashboards/project/networks/subnets/workflows.py index ae7dd9cd5a..9e1ab9b3b4 100644 --- a/openstack_dashboard/dashboards/project/networks/subnets/workflows.py +++ b/openstack_dashboard/dashboards/project/networks/subnets/workflows.py @@ -50,7 +50,7 @@ class CreateSubnetInfoAction(network_workflows.CreateSubnetInfoAction): class CreateSubnetInfo(network_workflows.CreateSubnetInfo): action_class = CreateSubnetInfoAction - depends_on = ("network_id",) + depends_on = ("network",) class CreateSubnet(network_workflows.CreateNetwork): @@ -68,14 +68,16 @@ class CreateSubnet(network_workflows.CreateNetwork): def get_success_url(self): return reverse("horizon:project:networks:detail", - args=(self.context.get('network_id'),)) + args=(self.context['network'].id,)) def get_failure_url(self): return reverse("horizon:project:networks:detail", - args=(self.context.get('network_id'),)) + args=(self.context['network'].id,)) def handle(self, request, data): - subnet = self._create_subnet(request, data) + network = self.context_seed['network'] + # network argument is required to show error message correctly. + subnet = self._create_subnet(request, data, network=network) return True if subnet else False