From 16159b75d96197d857c46b57ebef34038e37a8e1 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Mon, 12 Nov 2012 17:42:36 +0100 Subject: [PATCH] Add identifiers for resources Change-Id: I5d0477ba8ee681f14b0f54cf8fa4d8569d541f88 Signed-off-by: Zane Bitter --- heat/engine/identifier.py | 16 ++++++++++++++++ heat/engine/resources/resource.py | 6 ++++++ heat/tests/test_identifier.py | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/heat/engine/identifier.py b/heat/engine/identifier.py index 9fbc25b0..8cf71c1c 100644 --- a/heat/engine/identifier.py +++ b/heat/engine/identifier.py @@ -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) diff --git a/heat/engine/resources/resource.py b/heat/engine/resources/resource.py index 54d6ac24..ad3fcb3f 100644 --- a/heat/engine/resources/resource.py +++ b/heat/engine/resources/resource.py @@ -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 diff --git a/heat/tests/test_identifier.py b/heat/tests/test_identifier.py index 4fcc73df..92eefb3f 100644 --- a/heat/tests/test_identifier.py +++ b/heat/tests/test_identifier.py @@ -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__) -- 2.45.2