Skip to content

Commit 04e2118

Browse files
authored
update logic in rule E3686 (#3367)
1 parent 4244af2 commit 04e2118

File tree

3 files changed

+137
-24
lines changed

3 files changed

+137
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,56 @@
11
{
22
"additionalProperties": true,
3-
"description": "When creating a serverless 'EngineMode' don't specify 'ScalingConfiguration'",
4-
"else": {},
5-
"if": {
6-
"properties": {
7-
"EngineMode": {
8-
"const": "serverless"
3+
"allOf": [
4+
{
5+
"if": {
6+
"properties": {
7+
"EngineMode": {
8+
"const": "provisioned"
9+
}
10+
},
11+
"required": [
12+
"EngineMode"
13+
],
14+
"type": "object"
15+
},
16+
"then": {
17+
"properties": {
18+
"ScalingConfiguration": false
19+
}
920
}
1021
},
11-
"required": [
12-
"EngineMode"
13-
],
14-
"type": "object"
15-
},
16-
"then": {
17-
"properties": {
18-
"ScalingConfiguration": false
22+
{
23+
"if": {
24+
"properties": {
25+
"EngineMode": {
26+
"const": "serverless"
27+
}
28+
},
29+
"required": [
30+
"EngineMode"
31+
],
32+
"type": "object"
33+
},
34+
"then": {
35+
"properties": {
36+
"ServerlessV2ScalingConfiguration": false
37+
}
38+
}
39+
},
40+
{
41+
"if": {
42+
"properties": {
43+
"EngineMode": false
44+
},
45+
"type": "object"
46+
},
47+
"then": {
48+
"properties": {
49+
"ScalingConfiguration": false,
50+
"ServerlessV2ScalingConfiguration": false
51+
}
52+
}
1953
}
20-
}
54+
],
55+
"description": "When creating a serverless 'EngineMode' don't specify 'ServerlessV2ScalingConfiguration'"
2156
}

Diff for: src/cfnlint/rules/resources/rds/DbClusterServerlessExclusive.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
class DbClusterServerlessExclusive(CfnLintJsonSchema):
1616
id = "E3686"
17-
shortdesc = (
18-
"Validate when using a serverless RDS DB certain properties aren't needed"
19-
)
17+
shortdesc = "Validate allowed properties when using a serverless RDS DB cluster"
2018
description = (
21-
"When creating a serverless 'EngineMode' don't specify 'ScalingConfiguration'"
19+
"Validate that when EngineMode is 'serverless' or 'provisioned' that the "
20+
"appropriate allowed properties are provided. If 'EngineMode' is not provided "
21+
"make sure serverless properties don't exist at all."
2222
)
2323
tags = ["resources"]
2424

@@ -32,4 +32,15 @@ def __init__(self) -> None:
3232
)
3333

3434
def message(self, instance: Any, err: ValidationError) -> str:
35-
return "Additional properties are not allowed ('ScalingConfiguration')"
35+
36+
# validator None means the schema is falsy
37+
if err.validator is None:
38+
if instance.get("EngineMode") in ["serverless", "provisioned"]:
39+
return (
40+
f"EngineMode {instance.get('EngineMode')!r} "
41+
f" doesn't allow additional properties {err.path[0]!r}"
42+
)
43+
else:
44+
return "Additional properties are not allowed " f"({err.path[0]!r})"
45+
46+
return err.message

Diff for: test/unit/rules/resources/rds/test_db_cluster_serverless_exclusive.py

+71-4
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,87 @@ def rule():
3535
[],
3636
),
3737
(
38-
{"EngineMode": "serverless", "ScalingConfiguration": "foo"},
38+
{"EngineMode": "serverless", "ServerlessV2ScalingConfiguration": "foo"},
3939
[
4040
ValidationError(
41-
"Additional properties are not allowed ('ScalingConfiguration')",
41+
(
42+
"EngineMode 'serverless' doesn't allow additional "
43+
"properties 'ServerlessV2ScalingConfiguration'"
44+
),
45+
rule=DbClusterServerlessExclusive(),
46+
path=deque(["ServerlessV2ScalingConfiguration"]),
47+
validator=None,
48+
schema_path=deque(
49+
[
50+
"allOf",
51+
1,
52+
"then",
53+
"properties",
54+
"ServerlessV2ScalingConfiguration",
55+
]
56+
),
57+
)
58+
],
59+
),
60+
(
61+
{"EngineMode": "provisioned", "ScalingConfiguration": "foo"},
62+
[
63+
ValidationError(
64+
(
65+
"EngineMode 'provisioned' doesn't allow "
66+
"additional properties 'ScalingConfiguration'"
67+
),
68+
rule=DbClusterServerlessExclusive(),
69+
path=deque(["ScalingConfiguration"]),
70+
validator=None,
71+
schema_path=deque(
72+
["allOf", 0, "then", "properties", "ScalingConfiguration"]
73+
),
74+
)
75+
],
76+
),
77+
(
78+
{"ServerlessV2ScalingConfiguration": "foo"},
79+
[
80+
ValidationError(
81+
(
82+
"Additional properties are not allowed "
83+
"('ServerlessV2ScalingConfiguration')"
84+
),
85+
rule=DbClusterServerlessExclusive(),
86+
path=deque(["ServerlessV2ScalingConfiguration"]),
87+
validator=None,
88+
schema_path=deque(
89+
[
90+
"allOf",
91+
2,
92+
"then",
93+
"properties",
94+
"ServerlessV2ScalingConfiguration",
95+
]
96+
),
97+
)
98+
],
99+
),
100+
(
101+
{"ScalingConfiguration": "foo"},
102+
[
103+
ValidationError(
104+
(
105+
"Additional properties are not allowed "
106+
"('ScalingConfiguration')"
107+
),
42108
rule=DbClusterServerlessExclusive(),
43109
path=deque(["ScalingConfiguration"]),
44110
validator=None,
45-
schema_path=deque(["then", "properties", "ScalingConfiguration"]),
111+
schema_path=deque(
112+
["allOf", 2, "then", "properties", "ScalingConfiguration"]
113+
),
46114
)
47115
],
48116
),
49117
],
50118
)
51119
def test_validate(instance, expected, rule, validator):
52120
errs = list(rule.validate(validator, "", instance, {}))
53-
54121
assert errs == expected, f"Expected {expected} got {errs}"

0 commit comments

Comments
 (0)