Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/cfnlint/schema/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,24 @@ def get_resource_schemas_by_regions(
Iterator[tuple[list[str], Schema]]: the unique schemas with
their associated regions
"""
resource_type = self._normalize_resource_type(resource_type)
# Match get_resource_schema: registry schemas are looked up before
# the type is normalized, so a registry "::MODULE" type keeps its
# own schema.
is_registry_type = resource_type in self._registry_schemas
if not is_registry_type:
resource_type = self._normalize_resource_type(resource_type)

hash_to_regions: dict[str, list[str]] = {}
hash_to_schema: dict[str, Schema] = {}

for region in regions:
provider_types = self._load_provider_module(region)
schema_hash = provider_types.get(resource_type)
if is_registry_type:
# Registry schemas are not in any region's provider mapping,
# and one schema covers every region.
schema_hash: str | None = resource_type
else:
provider_types = self._load_provider_module(region)
schema_hash = provider_types.get(resource_type)

if not schema_hash:
continue
Expand Down
48 changes: 48 additions & 0 deletions test/unit/module/schema/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from cfnlint.helpers import get_cache_dir
from cfnlint.schema._patch import SchemaPatch
from cfnlint.schema._schema import Schema
from cfnlint.schema.manager import ProviderSchemaManager, ResourceNotFoundError

_fixtures_dir = Path(__file__).parent.parent.parent.parent / "fixtures" / "schemas"
Expand Down Expand Up @@ -404,6 +405,53 @@ def test_type_normalization(self):
assert schema is True


class TestManagerGetResourceSchemasByRegions(BaseTestCase):
"""Test getting the unique schemas for a type across regions"""

def setUp(self) -> None:
super().setUp()

self.manager = _make_manager()

def test_registry_schema(self):
rt = "Example::Module::AlbTarget"
schema = Schema({"typeName": rt, "properties": {"Name": {"type": "string"}}})
self.manager._registry_schemas[rt] = schema

results = list(
self.manager.get_resource_schemas_by_regions(rt, ["us-east-1", "us-west-2"])
)

self.assertEqual(results, [(["us-east-1", "us-west-2"], schema)])

def test_registry_module_schema(self):
# A registry "::MODULE" type keeps its own schema and is not
# normalized to the generic Module schema
rt = "Foo::Bar::MODULE"
schema = Schema({"typeName": rt, "properties": {"Foo": {"type": "string"}}})
self.manager._registry_schemas[rt] = schema

results = list(self.manager.get_resource_schemas_by_regions(rt, ["us-east-1"]))

self.assertEqual(results, [(["us-east-1"], schema)])

def test_registry_schema_removed_type(self):
rt = "Example::Module::AlbTarget"
self.manager._registry_schemas[rt] = Schema({"typeName": rt})
self.manager.patch(SchemaPatch([], [rt], {}), "us-east-1")

results = list(self.manager.get_resource_schemas_by_regions(rt, ["us-east-1"]))

self.assertEqual(results, [])

def test_unknown_type(self):
results = list(
self.manager.get_resource_schemas_by_regions("Foo::Bar::Baz", ["us-east-1"])
)

self.assertEqual(results, [])


class TestManagerPatch(BaseTestCase):
"""Test patching schemas"""

Expand Down
63 changes: 63 additions & 0 deletions test/unit/module/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from cfnlint.core import get_rules
from cfnlint.helpers import REGIONS
from cfnlint.rules.errors import ConfigError
from cfnlint.schema import PROVIDER_SCHEMA_MANAGER, reset


class TestLint(TestCase):
Expand Down Expand Up @@ -418,3 +419,65 @@ def test_graph_invalid_template(self):

result = graph("not: [valid: yaml: {")
self.assertIsNone(result)


class TestLintRegistrySchemas(TestCase):
"""Templates using types loaded with --registry-schemas"""

def setUp(self) -> None:
# Earlier tests can leave the resource type caches populated
# without the registry types
reset()

def tearDown(self) -> None:
PROVIDER_SCHEMA_MANAGER._registry_schemas = {}
reset()

def test_good_template(self):
filename = "test/fixtures/templates/good/schema_resource.yaml"
with open(filename, "r") as f:
matches = lint(
f.read(),
config=ManualArgs(
registry_schemas=["test/fixtures/registry/custom/"],
regions=["us-east-1", "us-west-2"],
),
)

self.assertListEqual([], matches, f"Got matches: {matches!r}")

def test_properties_and_getatts_are_validated(self):
template = """
Resources:
MyReport:
Type: Initech::TPS::Report
Properties:
TestCode: NOT_STARTED
Title: My report has to be longer
Unknown: true
MissingTitle:
Type: Initech::TPS::Report
Properties:
TestCode: CANCELLED
Parameter:
Type: AWS::SSM::Parameter
Properties:
Type: String
Value: !GetAtt MyReport.Nope
Outputs:
TpsCode:
Value: !GetAtt MyReport.TPSCode
"""
matches = lint(
template,
config=ManualArgs(
registry_schemas=["test/fixtures/registry/custom/"],
regions=["us-east-1", "us-west-2"],
),
)

self.assertListEqual(
["E1010", "E3002", "E3003"],
sorted(match.rule.id for match in matches),
f"Got matches: {matches!r}",
)
Loading