-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule E3695 to validate cache cluster engines (#3824)
- Loading branch information
Showing
4 changed files
with
332 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/cfnlint/data/schemas/extensions/aws_elasticache_cachecluster/engine_version.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
{ | ||
"_description": [ | ||
"Automatically updated using aws api" | ||
], | ||
"allOf": [ | ||
{ | ||
"if": { | ||
"properties": { | ||
"Engine": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"Engine" | ||
] | ||
}, | ||
"then": { | ||
"properties": { | ||
"Engine": { | ||
"enum": [ | ||
"memcached", | ||
"redis", | ||
"valkey" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"if": { | ||
"properties": { | ||
"Engine": { | ||
"const": "memcached" | ||
}, | ||
"EngineVersion": { | ||
"type": [ | ||
"string", | ||
"number" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"Engine", | ||
"EngineVersion" | ||
] | ||
}, | ||
"then": { | ||
"properties": { | ||
"EngineVersion": { | ||
"enum": [ | ||
"1.4.14", | ||
"1.4.24", | ||
"1.4.33", | ||
"1.4.34", | ||
"1.4.5", | ||
"1.5.10", | ||
"1.5.16", | ||
"1.6.12", | ||
"1.6.17", | ||
"1.6.22", | ||
"1.6.6" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"if": { | ||
"properties": { | ||
"Engine": { | ||
"const": "redis" | ||
}, | ||
"EngineVersion": { | ||
"type": [ | ||
"string", | ||
"number" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"Engine", | ||
"EngineVersion" | ||
] | ||
}, | ||
"then": { | ||
"properties": { | ||
"EngineVersion": { | ||
"enum": [ | ||
"4.0.10", | ||
"5.0.6", | ||
"6.0", | ||
"6.2", | ||
"7.0", | ||
"7.1" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"if": { | ||
"properties": { | ||
"Engine": { | ||
"const": "valkey" | ||
}, | ||
"EngineVersion": { | ||
"type": [ | ||
"string", | ||
"number" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"Engine", | ||
"EngineVersion" | ||
] | ||
}, | ||
"then": { | ||
"properties": { | ||
"EngineVersion": { | ||
"enum": [ | ||
"7.2" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |
29 changes: 29 additions & 0 deletions
29
src/cfnlint/rules/resources/elasticache/CacheClusterEngine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import cfnlint.data.schemas.extensions.aws_elasticache_cachecluster | ||
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails | ||
|
||
|
||
class CacheClusterEngine(CfnLintJsonSchema): | ||
id = "E3695" | ||
shortdesc = "Validate Elasticache Cluster Engine and Engine Version" | ||
description = ( | ||
"Validate the Elasticache cluster engine along with the engine version" | ||
) | ||
tags = ["resources"] | ||
source_url = "https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/supported-engine-versions.html" | ||
|
||
def __init__(self) -> None: | ||
super().__init__( | ||
keywords=["Resources/AWS::ElastiCache::CacheCluster/Properties"], | ||
schema_details=SchemaDetails( | ||
module=cfnlint.data.schemas.extensions.aws_elasticache_cachecluster, | ||
filename="engine_version.json", | ||
), | ||
all_matches=True, | ||
) |
90 changes: 90 additions & 0 deletions
90
test/unit/rules/resources/elasticache/test_cache_cluster_engine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from collections import deque | ||
|
||
import pytest | ||
|
||
from cfnlint.jsonschema import ValidationError | ||
from cfnlint.rules.resources.elasticache.CacheClusterEngine import CacheClusterEngine | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rule(): | ||
rule = CacheClusterEngine() | ||
yield rule | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"instance,expected", | ||
[ | ||
( | ||
{"Engine": "redis", "EngineVersion": 7.1}, | ||
[], | ||
), | ||
( | ||
{ | ||
"Engine": "redis", | ||
}, | ||
[], | ||
), | ||
( | ||
{}, | ||
[], | ||
), | ||
( | ||
{ | ||
"Engine": "redis", | ||
"EngineVersion": "7.1.0", | ||
}, | ||
[ | ||
ValidationError( | ||
( | ||
"'7.1.0' is not one of " | ||
"['4.0.10', '5.0.6', " | ||
"'6.0', '6.2', '7.0', '7.1']" | ||
), | ||
validator="enum", | ||
path=deque(["EngineVersion"]), | ||
schema_path=[ | ||
"allOf", | ||
2, | ||
"then", | ||
"properties", | ||
"EngineVersion", | ||
"enum", | ||
], | ||
rule=CacheClusterEngine(), | ||
) | ||
], | ||
), | ||
( | ||
{ | ||
"Engine": "oss-redis", | ||
"EngineVersion": "7.1", | ||
}, | ||
[ | ||
ValidationError( | ||
("'oss-redis' is not one of " "['memcached', 'redis', 'valkey']"), | ||
validator="enum", | ||
path=deque(["Engine"]), | ||
schema_path=["allOf", 0, "then", "properties", "Engine", "enum"], | ||
rule=CacheClusterEngine(), | ||
) | ||
], | ||
), | ||
( | ||
{ | ||
"Engine": "redis", | ||
"EngineVersion": {"Ref": "AWS::AccountId"}, | ||
}, | ||
[], | ||
), | ||
], | ||
) | ||
def test_validate(instance, expected, rule, validator): | ||
errs = list(rule.validate(validator, "", instance, {})) | ||
|
||
assert errs == expected, f"Expected {expected} got {errs}" |