From e842561394bfa1a36d6f8ccb8e80968806c1ec39 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam Date: Fri, 14 Aug 2015 16:47:02 +0530 Subject: [PATCH] Search based on the resource type name Adds the filtering support to resource type based on the type name pattern. implements blueprint heat-resource-type-search Change-Id: Id492d260b87464af385d58db7c017ba648fde411 --- heat/engine/environment.py | 13 ++++++++++++- heat/tests/test_environment.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/heat/engine/environment.py b/heat/engine/environment.py index 38519fdd44..bafa674be5 100644 --- a/heat/engine/environment.py +++ b/heat/engine/environment.py @@ -16,6 +16,7 @@ import fnmatch import glob import itertools import os.path +import re import warnings from oslo_config import cfg @@ -438,7 +439,10 @@ class ResourceRegistry(object): return _as_dict(self._registry) - def get_types(self, cnxt=None, support_status=None): + def get_types(self, + cnxt=None, + support_status=None, + type_name=None): '''Return a list of valid resource types.''' # validate the support status @@ -478,8 +482,15 @@ class ResourceRegistry(object): enforcer = policy.ResourceEnforcer() + def name_matches(name): + try: + return type_name is None or re.match(type_name, name) + except: # noqa + return False + return [name for name, cls in six.iteritems(self._registry) if (is_resource(name) and + name_matches(name) and status_matches(cls) and is_available(cls) and is_allowed(enforcer, name) and diff --git a/heat/tests/test_environment.py b/heat/tests/test_environment.py index 93b0c697cd..7c620b1b06 100644 --- a/heat/tests/test_environment.py +++ b/heat/tests/test_environment.py @@ -762,6 +762,23 @@ class ResourceRegistryTest(common.HeatTestCase): self.assertIn('ResourceTypeUnSupportedLiberty', types) self.assertIn('GenericResourceType', types) + def test_list_type_with_name(self): + registry = resources.global_env().registry + types = registry.get_types(type_name='ResourceType*') + self.assertIn('ResourceTypeUnSupportedLiberty', types) + self.assertNotIn('GenericResourceType', types) + + def test_list_type_with_name_none(self): + registry = resources.global_env().registry + types = registry.get_types(type_name=None) + self.assertIn('ResourceTypeUnSupportedLiberty', types) + self.assertIn('GenericResourceType', types) + + def test_list_type_with_invalid_type_name(self): + registry = resources.global_env().registry + types = registry.get_types(type_name="r'[^\+]'") + self.assertEqual([], types) + class HookMatchTest(common.HeatTestCase):