Skip to content

Commit

Permalink
Move network constraint to neutron plugin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
steveb committed Jul 21, 2014
1 parent 09b31c1 commit ec35f07
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
13 changes: 13 additions & 0 deletions heat/engine/clients/os/neutron.py
Expand Up @@ -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):
Expand All @@ -35,3 +38,13 @@ def _create(self):
}

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)
12 changes: 0 additions & 12 deletions heat/engine/resources/neutron/net.py
Expand Up @@ -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):
Expand Down Expand Up @@ -182,16 +180,6 @@ def _replace_dhcp_agents(self, dhcp_agent_ids):
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,
Expand Down
9 changes: 5 additions & 4 deletions heat/tests/test_neutron.py
Expand Up @@ -2397,16 +2397,17 @@ def test_validate(self):
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))
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -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
Expand Down

0 comments on commit ec35f07

Please sign in to comment.