From f9705f0f0376fc2eccf6b075e5a1a5c00e19fbd7 Mon Sep 17 00:00:00 2001 From: Ying Zuo Date: Tue, 15 Aug 2017 19:19:17 -0700 Subject: [PATCH] Remove hardcoded check for subnet actions of a shared network Currently create subnet, update subnet, and delete subnet actions for a shared network are restricted to admin users only. This patches removed this requirement so that the actions are enabled based on the neutron policy. Closes-bug: #1710700 Change-Id: Ia5b903c3678e26a3270bbe30402ae348942b9f50 --- .../project/networks/subnets/tables.py | 35 ++++++++----------- .../project/networks/subnets/tests.py | 6 ---- .../dashboards/project/networks/tables.py | 4 ++- .../dashboards/project/networks/tests.py | 4 --- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/openstack_dashboard/dashboards/project/networks/subnets/tables.py b/openstack_dashboard/dashboards/project/networks/subnets/tables.py index ebd0d8210f..e975976022 100644 --- a/openstack_dashboard/dashboards/project/networks/subnets/tables.py +++ b/openstack_dashboard/dashboards/project/networks/subnets/tables.py @@ -31,33 +31,30 @@ from openstack_dashboard.usage import quotas LOG = logging.getLogger(__name__) -class CheckNetworkEditable(object): - """Mixin class to determine the specified network is editable.""" - - def allowed(self, request, datum=None): - # Only administrator is allowed to create and manage subnets - # on shared networks. - network = self.table._get_network() - - if network.shared: - return False - return True - - class SubnetPolicyTargetMixin(policy.PolicyTargetMixin): def get_policy_target(self, request, datum=None): policy_target = super(SubnetPolicyTargetMixin, self)\ .get_policy_target(request, datum) - network = self.table._get_network() + # Use the network information if it is passed in with datum. + if datum and "tenant_id" in datum: + network = datum + else: + # This is called by the table actions of the subnets table on the + # network details panel and some information is not available. + # 1. Network information is not passed in so need to make a neutron + # API call to get it. + # 2. tenant_id and project_id are missing from policy_target. + network = self.table._get_network() + policy_target["tenant_id"] = network.tenant_id + policy_target["project_id"] = network.tenant_id # neutron switched policy target values, we'll support both policy_target["network:tenant_id"] = network.tenant_id policy_target["network:project_id"] = network.tenant_id return policy_target -class DeleteSubnet(SubnetPolicyTargetMixin, CheckNetworkEditable, - tables.DeleteAction): +class DeleteSubnet(SubnetPolicyTargetMixin, tables.DeleteAction): @staticmethod def action_present(count): return ungettext_lazy( @@ -89,8 +86,7 @@ class DeleteSubnet(SubnetPolicyTargetMixin, CheckNetworkEditable, exceptions.handle(request, msg, redirect=redirect) -class CreateSubnet(SubnetPolicyTargetMixin, CheckNetworkEditable, - tables.LinkAction): +class CreateSubnet(SubnetPolicyTargetMixin, tables.LinkAction): name = "create" verbose_name = _("Create Subnet") url = "horizon:project:networks:createsubnet" @@ -118,8 +114,7 @@ class CreateSubnet(SubnetPolicyTargetMixin, CheckNetworkEditable, return True -class UpdateSubnet(SubnetPolicyTargetMixin, CheckNetworkEditable, - tables.LinkAction): +class UpdateSubnet(SubnetPolicyTargetMixin, tables.LinkAction): name = "update" verbose_name = _("Edit Subnet") url = "horizon:project:networks:editsubnet" diff --git a/openstack_dashboard/dashboards/project/networks/subnets/tests.py b/openstack_dashboard/dashboards/project/networks/subnets/tests.py index dfe4812022..d0a6c7febf 100644 --- a/openstack_dashboard/dashboards/project/networks/subnets/tests.py +++ b/openstack_dashboard/dashboards/project/networks/subnets/tests.py @@ -1110,7 +1110,6 @@ class NetworkSubnetTests(test.TestCase): @test.create_stubs({api.neutron: ('subnet_delete', 'subnet_list', - 'network_get', 'port_list', 'is_extension_supported',)}) def test_subnet_delete_with_mac_learning(self): @@ -1122,8 +1121,6 @@ class NetworkSubnetTests(test.TestCase): api.neutron.subnet_delete(IsA(http.HttpRequest), subnet.id) api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network_id)\ .AndReturn([self.subnets.first()]) - api.neutron.network_get(IsA(http.HttpRequest), network_id)\ - .AndReturn(self.networks.first()) api.neutron.is_extension_supported(IsA(http.HttpRequest), 'mac-learning')\ .AndReturn(mac_learning) @@ -1145,7 +1142,6 @@ class NetworkSubnetTests(test.TestCase): @test.create_stubs({api.neutron: ('subnet_delete', 'subnet_list', - 'network_get', 'port_list', 'is_extension_supported',)}) def test_subnet_delete_exception_with_mac_learning(self): @@ -1158,8 +1154,6 @@ class NetworkSubnetTests(test.TestCase): .AndRaise(self.exceptions.neutron) api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network_id)\ .AndReturn([self.subnets.first()]) - api.neutron.network_get(IsA(http.HttpRequest), network_id)\ - .AndReturn(self.networks.first()) api.neutron.is_extension_supported(IsA(http.HttpRequest), 'mac-learning')\ .AndReturn(mac_learning) diff --git a/openstack_dashboard/dashboards/project/networks/tables.py b/openstack_dashboard/dashboards/project/networks/tables.py index 6376650562..b65926c997 100644 --- a/openstack_dashboard/dashboards/project/networks/tables.py +++ b/openstack_dashboard/dashboards/project/networks/tables.py @@ -24,6 +24,8 @@ from horizon import exceptions from horizon import tables from openstack_dashboard import api +from openstack_dashboard.dashboards.project.networks.subnets import tables \ + as subnet_tables from openstack_dashboard import policy from openstack_dashboard.usage import quotas @@ -104,7 +106,7 @@ class EditNetwork(policy.PolicyTargetMixin, tables.LinkAction): policy_rules = (("network", "update_network"),) -class CreateSubnet(policy.PolicyTargetMixin, tables.LinkAction): +class CreateSubnet(subnet_tables.SubnetPolicyTargetMixin, tables.LinkAction): name = "subnet" verbose_name = _("Create Subnet") url = "horizon:project:networks:createsubnet" diff --git a/openstack_dashboard/dashboards/project/networks/tests.py b/openstack_dashboard/dashboards/project/networks/tests.py index 17a94ff87c..441c3b8cbb 100644 --- a/openstack_dashboard/dashboards/project/networks/tests.py +++ b/openstack_dashboard/dashboards/project/networks/tests.py @@ -214,8 +214,6 @@ class NetworkTests(test.TestCase, NetworkStubMixin): .AndReturn(self.networks.first()) api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network_id)\ .AndReturn([self.subnets.first()]) - api.neutron.network_get(IsA(http.HttpRequest), network_id)\ - .AndReturn(self.networks.first()) api.neutron.is_extension_supported(IsA(http.HttpRequest), 'mac-learning')\ .AndReturn(mac_learning) @@ -327,8 +325,6 @@ class NetworkTests(test.TestCase, NetworkStubMixin): AndReturn(self.networks.first()) api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network_id).\ AndReturn([self.subnets.first()]) - api.neutron.network_get(IsA(http.HttpRequest), network_id).\ - AndReturn(self.networks.first()) api.neutron.is_extension_supported(IsA(http.HttpRequest), 'mac-learning')\ .AndReturn(mac_learning)