]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add identifiers for resources
authorZane Bitter <zbitter@redhat.com>
Mon, 12 Nov 2012 16:42:36 +0000 (17:42 +0100)
committerZane Bitter <zbitter@redhat.com>
Thu, 15 Nov 2012 20:14:29 +0000 (21:14 +0100)
Change-Id: I5d0477ba8ee681f14b0f54cf8fa4d8569d541f88
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/engine/identifier.py
heat/engine/resources/resource.py
heat/tests/test_identifier.py

index 9fbc25b091826eeb1a632508f306d813d83c863d..8cf71c1c01644c2ed75424530a3e2678eef08ab3 100644 (file)
@@ -72,6 +72,12 @@ class HeatIdentifier(collections.Mapping):
                    urllib.unquote(path.group(2)),
                    urllib.unquote(path.group(3)))
 
+    def stack(self):
+        '''
+        Return a HeatIdentifier for the top-level stack
+        '''
+        return HeatIdentifier(self.tenant, self.stack_name, self.stack_id)
+
     def arn(self):
         '''
         Return an ARN of the form:
@@ -126,3 +132,13 @@ class HeatIdentifier(collections.Mapping):
 
     def __repr__(self):
         return repr(dict(self))
+
+
+class ResourceIdentifier(HeatIdentifier):
+    def __init__(self, stack_identifier, resource_id):
+        path = (stack_identifier.path.rstrip('/') +
+                '/resources/%s' % resource_id)
+        super(ResourceIdentifier, self).__init__(stack_identifier.tenant,
+                                                 stack_identifier.stack_name,
+                                                 stack_identifier.stack_id,
+                                                 path)
index 54d6ac2425d5242850d94e285d60c244e63c04c4..ad3fcb3f685e397affe3b738449faf81e02f17c9 100644 (file)
@@ -19,6 +19,7 @@ from datetime import datetime
 from heat.common import exception
 from heat.common import config
 from heat.db import api as db_api
+from heat.engine import identifier
 from heat.engine import timestamp
 from heat.engine.resources.properties import Properties
 
@@ -133,6 +134,11 @@ class Resource(object):
             return result
         return not result
 
+    def identifier(self):
+        '''Return an identifier for this resource'''
+        return identifier.ResourceIdentifier(self.stack.identifier(),
+                                             self.name)
+
     def parsed_template(self, section=None, default={}):
         '''
         Return the parsed template data for the resource. May be limited to
index 4fcc73df0c68a8b18bca62a34d0ffa6945747062..92eefb3ff46d27ebb4470a645e87e4e7b4e2f87c 100644 (file)
@@ -234,6 +234,21 @@ class IdentifierTest(unittest.TestCase):
                 utils.generate_uuid()))
 
 
+@attr(tag=['unit', 'identifier'])
+@attr(speed='fast')
+class ResourceIdentifierTest(unittest.TestCase):
+    def test_resource_init_no_path(self):
+        si = identifier.HeatIdentifier('t', 's', 'i')
+        ri = identifier.ResourceIdentifier(si, 'r')
+        self.assertEqual(ri.path, '/resources/r')
+
+    def test_resource_init_path(self):
+        si = identifier.HeatIdentifier('t', 's', 'i')
+        pi = identifier.ResourceIdentifier(si, 'p')
+        ri = identifier.ResourceIdentifier(pi, 'r')
+        self.assertEqual(ri.path, '/resources/p/resources/r')
+
+
 # allows testing of the test directly, shown below
 if __name__ == '__main__':
     sys.argv.append(__file__)