Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule W0510 to validate transform order #3151

Merged
merged 2 commits into from
Apr 18, 2024
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
39 changes: 39 additions & 0 deletions src/cfnlint/rules/transforms/LanguageExtensionsBeforeSAM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from cfnlint.rules import CloudFormationLintRule, RuleMatch


class LanguageExtensionsBeforeSAM(CloudFormationLintRule):
"""Check if Serverless Resources exist without the Serverless Transform"""

id = "W0510"
shortdesc = "Put LanguageExtensions before SAM transform"
description = (
"If using language extensions with SAM, you should add AWS::LanguageExtensions "
"before the serverless transform (that is, before AWS::Serverless-2016-10-31)"
)
source_url = "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html"
tags = ["transforms"]

def match(self, cfn):
matches = []

if isinstance(cfn.transform_pre["Transform"], list):
flag = False
for i, transform in enumerate(cfn.transform_pre["Transform"]):
if transform == "AWS::Serverless-2016-10-31":
flag = True
if transform == "AWS::LanguageExtensions" and flag:
matches.append(
RuleMatch(
["Transform", i],
(
"Place 'AWS::LanguageExtensions' before "
"'AWS::Serverless-2016-10-31'"
),
)
)
return matches
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Transform:
- AWS::Serverless-2016-10-31
- AWS::LanguageExtensions
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs20.x
CodeUri: 's3://testBucket/mySourceCode.zip'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Transform:
- MyOtherTransform
- AWS::LanguageExtensions
Resources:
Bucket:
Type: AWS::S3::Bucket
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs20.x
CodeUri: 's3://testBucket/mySourceCode.zip'
8 changes: 8 additions & 0 deletions test/fixtures/templates/good/transforms/string_transform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs20.x
CodeUri: 's3://testBucket/mySourceCode.zip'
Empty file.
35 changes: 35 additions & 0 deletions test/unit/rules/transforms/test_language_extensions_before_sam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from test.unit.rules import BaseRuleTestCase

from cfnlint.rules.transforms.LanguageExtensionsBeforeSAM import (
LanguageExtensionsBeforeSAM, # pylint: disable=E0401
)


class TestLanguageExtensionsBeforeSAM(BaseRuleTestCase):
"""Test template limit size"""

def setUp(self):
"""Setup"""
super(TestLanguageExtensionsBeforeSAM, self).setUp()
self.collection.register(LanguageExtensionsBeforeSAM())
self.success_templates = [
"test/fixtures/templates/good/transforms/language_extensions_with_sam.yaml",
"test/fixtures/templates/good/transforms/string_transform.yaml",
"test/fixtures/templates/good/transforms/language_extensions_with_other.yaml",
]

def test_file_positive(self):
"""Test Positive"""
self.helper_file_positive()

def test_file_negative(self):
"""Test failure"""
self.helper_file_negative(
"test/fixtures/templates/bad/transforms/language_extensions_with_sam.yaml",
1,
)