From: Angus Salkeld Date: Tue, 20 Aug 2013 05:37:26 +0000 (+1000) Subject: Add a has_interface() method to the resource class X-Git-Tag: 2014.1~151^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=ee8b29ee7ecf558a6e19822f5e6b6bbffd28e773;p=openstack-build%2Fheat-build.git Add a has_interface() method to the resource class This is to better indentify resource types, given that they can be mapped in the environment. Change-Id: I6dd552c87b80eb34baec306acd663657ee929b30 --- diff --git a/heat/engine/resource.py b/heat/engine/resource.py index 331f89c8..e242840b 100644 --- a/heat/engine/resource.py +++ b/heat/engine/resource.py @@ -184,6 +184,16 @@ class Resource(object): def type(self): return self.t['Type'] + def has_interface(self, resource_type): + """Check to see if this resource is either mapped to resource_type + or is a "resource_type". + """ + if self.type() == resource_type: + return True + ri = self.stack.env.get_resource_info(self.type(), + self.name) + return ri.name == resource_type + def identifier(self): '''Return an identifier for this resource.''' return identifier.ResourceIdentifier(resource_name=self.name, diff --git a/heat/engine/resources/internet_gateway.py b/heat/engine/resources/internet_gateway.py index 67576f23..622708ed 100644 --- a/heat/engine/resources/internet_gateway.py +++ b/heat/engine/resources/internet_gateway.py @@ -68,7 +68,7 @@ class VPCGatewayAttachment(resource.Resource): def _vpc_route_tables(self): for resource in self.stack.resources.itervalues(): - if (resource.type() == 'AWS::EC2::RouteTable' and + if (resource.has_interface('AWS::EC2::RouteTable') and resource.properties.get('VpcId') == self.properties.get('VpcId')): yield resource diff --git a/heat/engine/resources/neutron/floatingip.py b/heat/engine/resources/neutron/floatingip.py index ddff82e7..34b0f65c 100644 --- a/heat/engine/resources/neutron/floatingip.py +++ b/heat/engine/resources/neutron/floatingip.py @@ -36,8 +36,7 @@ class FloatingIP(neutron.NeutronResource): # depend on any RouterGateway in this template with the same # network_id as this floating_network_id for resource in self.stack.resources.itervalues(): - if ((resource.type() == 'OS::Neutron::RouterGateway' or - resource.type() == 'OS::Quantum::RouterGateway') and + if (resource.has_interface('OS::Neutron::RouterGateway') and resource.properties.get('network_id') == self.properties.get('floating_network_id')): deps += (self, resource) diff --git a/heat/engine/resources/neutron/port.py b/heat/engine/resources/neutron/port.py index a0eccb9e..fa8a1280 100644 --- a/heat/engine/resources/neutron/port.py +++ b/heat/engine/resources/neutron/port.py @@ -65,8 +65,7 @@ class Port(neutron.NeutronResource): # to so all subnets in a network should be created before # the ports in that network. for resource in self.stack.resources.itervalues(): - if ((resource.type() == 'OS::Neutron::Subnet' or - resource.type() == 'OS::Quantum::Subnet') and + if (resource.has_interface('OS::Neutron::Subnet') and resource.properties.get('network_id') == self.properties.get('network_id')): deps += (self, resource) diff --git a/heat/engine/resources/neutron/router.py b/heat/engine/resources/neutron/router.py index 91073368..c81be307 100644 --- a/heat/engine/resources/neutron/router.py +++ b/heat/engine/resources/neutron/router.py @@ -103,16 +103,14 @@ class RouterGateway(neutron.NeutronResource): for resource in self.stack.resources.itervalues(): # depend on any RouterInterface in this template with the same # router_id as this router_id - if ((resource.type() == 'OS::Neutron::RouterInterface' or - resource.type() == 'OS::Quantum::RouterInterface') and + if (resource.has_interface('OS::Neutron::RouterInterface') and resource.properties.get('router_id') == self.properties.get('router_id')): deps += (self, resource) # depend on any subnet in this template with the same network_id # as this network_id, as the gateway implicitly creates a port # on that subnet - elif ((resource.type() == 'OS::Neutron::Subnet' or - resource.type() == 'OS::Quantum::Subnet') and + elif (resource.has_interface('OS::Neutron::Subnet') and resource.properties.get('network_id') == self.properties.get('network_id')): deps += (self, resource) diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index 6517dbec..3b905817 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -18,6 +18,7 @@ from heat.common import exception from heat.engine import parser from heat.engine import resource from heat.engine import scheduler +from heat.engine import environment from heat.openstack.common import uuidutils import heat.db.api as db_api @@ -30,13 +31,18 @@ class ResourceTest(HeatTestCase): def setUp(self): super(ResourceTest, self).setUp() utils.setup_dummy_db() - self.stack = parser.Stack(utils.dummy_context(), 'test_stack', - parser.Template({}), - stack_id=uuidutils.generate_uuid()) resource._register_class('GenericResourceType', generic_rsrc.GenericResource) + env = environment.Environment() + env.load({u'resource_registry': + {u'OS::Test::GenericResource': u'GenericResourceType'}}) + + self.stack = parser.Stack(utils.dummy_context(), 'test_stack', + parser.Template({}), env=env, + stack_id=uuidutils.generate_uuid()) + def test_get_class_ok(self): cls = resource.get_class('GenericResourceType') self.assertEqual(cls, generic_rsrc.GenericResource) @@ -89,6 +95,21 @@ class ResourceTest(HeatTestCase): res = generic_rsrc.GenericResource('test_resource', tmpl, self.stack) self.assertEqual(res.type(), 'Foo') + def test_has_interface_direct_match(self): + tmpl = {'Type': 'GenericResourceType'} + res = generic_rsrc.GenericResource('test_resource', tmpl, self.stack) + self.assertTrue(res.has_interface('GenericResourceType')) + + def test_has_interface_no_match(self): + tmpl = {'Type': 'GenericResourceType'} + res = generic_rsrc.GenericResource('test_resource', tmpl, self.stack) + self.assertFalse(res.has_interface('LookingForAnotherType')) + + def test_has_interface_mapping(self): + tmpl = {'Type': 'OS::Test::GenericResource'} + res = generic_rsrc.GenericResource('test_resource', tmpl, self.stack) + self.assertTrue(res.has_interface('GenericResourceType')) + def test_created_time(self): tmpl = {'Type': 'Foo'} res = generic_rsrc.GenericResource('test_res_new', tmpl, self.stack)