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 <amotoki@gmail.com>
This commit is contained in:
Ernest Millan 2016-11-16 16:00:17 -08:00 committed by Akihiro Motoki
parent e09c11ac84
commit 3a3cb681c9
5 changed files with 22 additions and 32 deletions

View File

@ -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(

View File

@ -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

View File

@ -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

View File

@ -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()}

View File

@ -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