From ec35f074ccc9c11d8bad3059eaacb5cfe14c4653 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Tue, 17 Jun 2014 16:06:59 +1200 Subject: [PATCH] Move network constraint to neutron plugin This allows exception imports to be encapsulated inside the neutron plugin, and plugins are likely the most appropriate place for custom constraints to live. Change-Id: I5766edb305b7cfffdcd8943fb6edce7f3e9ff26d --- heat/engine/clients/os/neutron.py | 13 +++++++++++++ heat/engine/resources/neutron/net.py | 12 ------------ heat/tests/test_neutron.py | 9 +++++---- setup.cfg | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/heat/engine/clients/os/neutron.py b/heat/engine/clients/os/neutron.py index b5b6509aa..3ef22b596 100644 --- a/heat/engine/clients/os/neutron.py +++ b/heat/engine/clients/os/neutron.py @@ -11,9 +11,12 @@ # License for the specific language governing permissions and limitations # under the License. +from neutronclient.common import exceptions +from neutronclient.neutron import v2_0 as neutronV20 from neutronclient.v2_0 import client as nc from heat.engine.clients import client_plugin +from heat.engine import constraints class NeutronClientPlugin(client_plugin.ClientPlugin): @@ -35,3 +38,13 @@ class NeutronClientPlugin(client_plugin.ClientPlugin): } return nc.Client(**args) + + +class NetworkConstraint(constraints.BaseCustomConstraint): + + expected_exceptions = (exceptions.NeutronClientException,) + + def validate_with_client(self, client, value): + neutron_client = client.client('neutron') + neutronV20.find_resourceid_by_name_or_id( + neutron_client, 'network', value) diff --git a/heat/engine/resources/neutron/net.py b/heat/engine/resources/neutron/net.py index d8ad78410..47d85a2a0 100644 --- a/heat/engine/resources/neutron/net.py +++ b/heat/engine/resources/neutron/net.py @@ -12,12 +12,10 @@ # under the License. from heat.engine import attributes -from heat.engine import constraints from heat.engine import properties from heat.engine.resources.neutron import neutron import neutronclient.common.exceptions as neutron_exp -from neutronclient.neutron import v2_0 as neutronV20 class Net(neutron.NeutronResource): @@ -182,16 +180,6 @@ class Net(neutron.NeutronResource): raise ex -class NetworkConstraint(constraints.BaseCustomConstraint): - - expected_exceptions = (neutron_exp.NeutronClientException,) - - def validate_with_client(self, client, value): - neutron_client = client.client('neutron') - neutronV20.find_resourceid_by_name_or_id( - neutron_client, 'network', value) - - def resource_mapping(): return { 'OS::Neutron::Net': Net, diff --git a/heat/tests/test_neutron.py b/heat/tests/test_neutron.py index cced8eb75..b89d890c9 100644 --- a/heat/tests/test_neutron.py +++ b/heat/tests/test_neutron.py @@ -2397,16 +2397,17 @@ class NetworkConstraintTest(HeatTestCase): nc = self.m.CreateMockAnything() self.m.StubOutWithMock(neutron.NeutronClientPlugin, '_create') neutron.NeutronClientPlugin._create().AndReturn(nc) - self.m.StubOutWithMock(net.neutronV20, 'find_resourceid_by_name_or_id') - net.neutronV20.find_resourceid_by_name_or_id( + self.m.StubOutWithMock(neutron.neutronV20, + 'find_resourceid_by_name_or_id') + neutron.neutronV20.find_resourceid_by_name_or_id( nc, 'network', 'foo' ).AndReturn('foo') - net.neutronV20.find_resourceid_by_name_or_id( + neutron.neutronV20.find_resourceid_by_name_or_id( nc, 'network', 'bar' ).AndRaise(qe.NeutronClientException(status_code=404)) self.m.ReplayAll() - constraint = net.NetworkConstraint() + constraint = neutron.NetworkConstraint() ctx = utils.dummy_context() self.assertTrue(constraint.validate("foo", ctx)) self.assertFalse(constraint.validate("bar", ctx)) diff --git a/setup.cfg b/setup.cfg index 56019ad3c..a40c2e691 100644 --- a/setup.cfg +++ b/setup.cfg @@ -50,7 +50,7 @@ heat.clients = heat.constraints = nova.flavor = heat.engine.resources.server:FlavorConstraint - neutron.network = heat.engine.resources.neutron.net:NetworkConstraint + neutron.network = heat.engine.clients.os.neutron:NetworkConstraint glance.image = heat.engine.resources.image:ImageConstraint iso_8601 = heat.engine.resources.iso_8601:ISO8601Constraint nova.keypair = heat.engine.resources.nova_keypair:KeypairConstraint