]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add a has_interface() method to the resource class
authorAngus Salkeld <asalkeld@redhat.com>
Tue, 20 Aug 2013 05:37:26 +0000 (15:37 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Fri, 23 Aug 2013 13:01:26 +0000 (23:01 +1000)
This is to better indentify resource types, given that they
can be mapped in the environment.

Change-Id: I6dd552c87b80eb34baec306acd663657ee929b30

heat/engine/resource.py
heat/engine/resources/internet_gateway.py
heat/engine/resources/neutron/floatingip.py
heat/engine/resources/neutron/port.py
heat/engine/resources/neutron/router.py
heat/tests/test_resource.py

index 331f89c8f6293e890db245b77019c161fe1cc917..e242840b7985b2a36adaf524085007cd80dd54d4 100644 (file)
@@ -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,
index 67576f23b003419491078dd370225ee691ef54c2..622708ed167b46a321de73318cde7dfa35ca4b7e 100644 (file)
@@ -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
index ddff82e73eb3e8fb8d11b55eefc9f9de1f665352..34b0f65c7ce6469cc70f237212ea40d4137f0d5f 100644 (file)
@@ -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)
index a0eccb9e6fe0e6119208b9b97b80294327a8d6c5..fa8a12806a3b70fa83302631b19b56b87bde796b 100644 (file)
@@ -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)
index 910733681a144bc3908de9871c177be646d631a2..c81be307440354505ea4b8739757bfd415236e41 100644 (file)
@@ -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)
index 6517dbec1b378aca012622be8bb22a9a0b9ef880..3b9058177b4c7477051a322e9dac7630250a8501 100644 (file)
@@ -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)