[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix cfn-lint breaking changes (#7784)
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni committed Jun 21, 2024
1 parent 3a8edcb commit a5488db
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
67 changes: 45 additions & 22 deletions moto/cloudformation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,58 @@ def _f(loader: Any, tag: Any, node: Any) -> Any:

def validate_template_cfn_lint(template: str) -> List[Any]:
# Importing cfnlint adds a significant overhead, so we keep it local
from cfnlint import core, decode

# Save the template to a temporary file -- cfn-lint requires a file
filename = "file.tmp"
with open(filename, "w") as file:
file.write(template)
abs_filename = os.path.abspath(filename)

# decode handles both yaml and json
try:
template, matches = decode.decode(abs_filename, False)
except TypeError:
# As of cfn-lint 0.39.0, the second argument (ignore_bad_template) was dropped
# https://github.com/aws-cloudformation/cfn-python-lint/pull/1580
template, matches = decode.decode(abs_filename)
# Compatibility for cfn-lint 0.x
# Fail fast with `cfnlint.core.configure_logging` which is removed in cfn-lint 1.x

from cfnlint.core import configure_logging, get_rules, run_checks
from cfnlint.decode import decode

# Save the template to a temporary file -- cfn-lint requires a file
filename = "file.tmp"
with open(filename, "w") as file:
file.write(template)
abs_filename = os.path.abspath(filename)

# decode handles both yaml and json
try:
template, matches = decode(abs_filename, False)
except TypeError:
# As of cfn-lint 0.39.0, the second argument (ignore_bad_template) was dropped
# https://github.com/aws-cloudformation/cfn-python-lint/pull/1580
template, matches = decode(abs_filename)

# Set cfn-lint to info
configure_logging(None)

# Initialize the ruleset to be applied (no overrules, no excludes)
rules = get_rules([], [], [])

# Use us-east-1 region (spec file) for validation
regions = ["us-east-1"]

# Process all the rules and gather the errors
return run_checks(abs_filename, template, rules, regions)

except ImportError:
# Compatibility for cfn-lint 1.x

# Set cfn-lint to info
core.configure_logging(None)
from cfnlint.api import lint
from cfnlint.config import configure_logging
from cfnlint.core import get_rules

# Initialize the ruleset to be applied (no overrules, no excludes)
rules = core.get_rules([], [], [])
# Set cfn-lint to info
configure_logging(None, False)

# Use us-east-1 region (spec file) for validation
regions = ["us-east-1"]
# Initialize the ruleset to be applied (no overrules, no excludes)
rules = get_rules([], [], [])

# Process all the rules and gather the errors
matches = core.run_checks(abs_filename, template, rules, regions)
# Use us-east-1 region (spec file) for validation
regions = ["us-east-1"]

return matches
# Process all the rules and gather the errors
return lint(template, rules, regions)


def get_stack_from_s3_url(template_url: str, account_id: str, partition: str) -> str:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_cloudformation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def test_boto3_json_invalid_missing_resource():
err = exc.value.response["Error"]
assert (
err["Message"]
== "Stack with id Missing top level template section Resources does not exist"
in (
"Stack with id Missing top level template section Resources does not exist", # cfn-lint 0.x
"Stack with id 'Resources' is a required property does not exist", # cfn-lint 1.x
)
)


Expand Down Expand Up @@ -124,5 +127,8 @@ def test_boto3_yaml_invalid_missing_resource():
err = exc.value.response["Error"]
assert (
err["Message"]
== "Stack with id Missing top level template section Resources does not exist"
in (
"Stack with id Missing top level template section Resources does not exist", # cfn-lint 0.x
"Stack with id 'Resources' is a required property does not exist", # cfn-lint 1.x
)
)

0 comments on commit a5488db

Please sign in to comment.