8000 Add support for fn::select in conditional by pinzon · Pull Request #9311 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

Add support for fn::select in conditional #9311

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

Merged
merged 1 commit into from
Oct 9, 2023
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
15 changes: 15 additions & 0 deletions localstack/services/cloudformation/engine/template_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,21 @@ def resolve_condition(
]
)
return result
case "Fn::Select":
index = v[0]
options = v[1]
for i, option in enumerate(options):
if isinstance(option, dict):
options[i] = resolve_condition(
account_id,
region_name,
option,
conditions,
parameters,
mappings,
stack_name,
)
return options[index]
case "Fn::Sub":
# we can assume anything in there is a ref
if isinstance(v, str):
Expand Down
12 changes: 12 additions & 0 deletions tests/aws/services/cloudformation/engine/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,15 @@ def test_conditional_in_conditional(self, env, region, deploy_cfn_template, aws_
assert stack.outputs["Result"] == "true"
else:
assert stack.outputs["Result"] == "false"

@markers.aws.validated
def test_conditional_with_select(self, deploy_cfn_template, aws_client):
stack = deploy_cfn_template(
template_path=os.path.join(
os.path.dirname(__file__),
"../../../templates/conditions/conditional-with-select.yml",
),
)

managed_policy_arn = stack.outputs["PolicyArn"]
assert aws_client.iam.get_policy(PolicyArn=managed_policy_arn)
26 changes: 26 additions & 0 deletions tests/aws/templates/conditions/conditional-with-select.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
AWSTemplateFormatVersion: '2010-09-09'

Conditions:
IsGrapes: !Equals [!Select [ 1, ['apples', 'grapes', 'bananas']], 'grapes']

Resources:
StreamWriterPolicy2:
Type: 'AWS::IAM::ManagedPolicy'
Condition: IsGrapes
Properties:
ManagedPolicyName: Test2
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "kinesis:PutRecord"
Resource: !Join
- ':'
- - arn:aws:kinesis
- !Ref AWS::Region
- !Ref AWS::AccountId
- !Sub stream/${AWS::StackName}-*
Outputs:
PolicyArn:
Description: StreamWriterPolicy2
Value: !Ref StreamWriterPolicy2
0