-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
fix: apply resource conditions to DeploymentPreference resources #1578
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1c2e550
fix code deploy conditions bug
keetonian 7aacc65
Cover all condition cases, update tests
keetonian 2ec132f
black reformat
hawflau b2736f4
Update tests to use .get_codedeploy_iam_role() instead of .codedeploy…
hawflau 38fcc48
Fixing unit tests after merging Condition fix commit
hawflau 2a56189
Use feature toggle to gate deployment preference condition fix
hawflau f14ef77
Add tests
hawflau cdcde39
Revert using feature toggle
hawflau 7b672e7
Add property to opt-in deployment preference condition passthrough
hawflau 8585bf8
Add tests for PassthroughCondition
hawflau c7cbf79
Fix passthrough condition logic and add tests
hawflau 8405853
Update PassthroughCondition to support intrinsic
hawflau 82ffbd9
intrinscs support + tests
torresxb1 d39ef47
update invalid intrinsics end-to-end test
torresxb1 4597514
uncomment and update invalid value unit test
torresxb1 bdaded1
black
torresxb1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
""" SAM macro definitions """ | ||
import copy | ||
from typing import Union | ||
from samtranslator.intrinsics.resolver import IntrinsicsResolver | ||
|
||
import samtranslator.model.eventsources | ||
import samtranslator.model.eventsources.pull | ||
|
@@ -853,11 +855,12 @@ def _validate_deployment_preference_and_add_update_policy( | |
self.DeploymentPreference["Type"] = preference_type | ||
|
||
if "PassthroughCondition" in self.DeploymentPreference: | ||
# resolve intrinsics and mappings for PassthroughCondition | ||
passthrough_condition = self.DeploymentPreference["PassthroughCondition"] | ||
passthrough_condition = intrinsics_resolver.resolve_parameter_refs(passthrough_condition) | ||
passthrough_condition = mappings_resolver.resolve_parameter_refs(passthrough_condition) | ||
self.DeploymentPreference["PassthroughCondition"] = passthrough_condition | ||
self.DeploymentPreference["PassthroughCondition"] = self._resolve_property_to_boolean( | ||
self.DeploymentPreference["PassthroughCondition"], | ||
"PassthroughCondition", | ||
intrinsics_resolver, | ||
mappings_resolver, | ||
) | ||
elif feature_toggle: | ||
self.DeploymentPreference["PassthroughCondition"] = feature_toggle.is_enabled( | ||
"deployment_preference_condition_fix" | ||
|
@@ -886,6 +889,40 @@ def _validate_deployment_preference_and_add_update_policy( | |
"UpdatePolicy", deployment_preference_collection.update_policy(self.logical_id).to_dict() | ||
) | ||
|
||
def _resolve_property_to_boolean( | ||
self, | ||
property_value: Union[bool, str, dict], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can it be anything else? like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, can't they only be string, boolean or intrinsic? |
||
property_name: str, | ||
intrinsics_resolver: IntrinsicsResolver, | ||
mappings_resolver: IntrinsicsResolver, | ||
) -> bool: | ||
""" | ||
Resolves intrinsics, if any, and/or converts string in a given property to boolean. | ||
Raises InvalidResourceException if can't resolve intrinsic or can't resolve string to boolean | ||
|
||
:param property_value: property value to resolve | ||
:param property_name: name/key of property to resolve | ||
:param intrinsics_resolver: resolves intrinsics | ||
:param mappings_resolver: resolves FindInMap | ||
:return bool: resolved boolean value | ||
""" | ||
processed_property_value = intrinsics_resolver.resolve_parameter_refs(property_value) | ||
processed_property_value = mappings_resolver.resolve_parameter_refs(processed_property_value) | ||
|
||
# FIXME: We should support not only true/false, but also yes/no, on/off? See https://yaml.org/type/bool.html | ||
if processed_property_value in [True, "true", "True"]: | ||
return True | ||
elif processed_property_value in [False, "false", "False"]: | ||
return False | ||
elif is_intrinsic(processed_property_value): # couldn't resolve intrinsic | ||
raise InvalidResourceException( | ||
self.logical_id, | ||
f"Unsupported intrinsic: the only intrinsic functions supported for " | ||
f"property {property_name} are FindInMap and parameter Refs.", | ||
) | ||
else: | ||
raise InvalidResourceException(self.logical_id, f"Invalid value for property {property_name}.") | ||
|
||
def _construct_function_url(self, lambda_function, lambda_alias): | ||
""" | ||
This method is used to construct a lambda url resource | ||
|
72 changes: 72 additions & 0 deletions
72
.../error_function_with_deployment_preference_passthrough_condition_with_invalid_values.yaml
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,72 @@ | ||
# Tests unsupported intrinsic and invalid type in the PassthroughCondition property | ||
|
||
Mappings: | ||
HelloWorldMap: | ||
hello: | ||
key1: true | ||
key2: false | ||
world: | ||
key1: false | ||
key2: true | ||
|
||
Parameters: | ||
FnName: | ||
Type: String | ||
ProvisionedConcurrency: | ||
Type: String | ||
Default: 10 | ||
EnableAliasProvisionedConcurrency: | ||
Type: String | ||
AllowedValues: | ||
- true | ||
- false | ||
Default: true | ||
DefaultTrueParam: | ||
Type: String | ||
Default: "true" | ||
DefaultFalseParam: | ||
Type: String | ||
Default: "false" | ||
HelloWorldParam: | ||
Type: String | ||
Default: "hello" | ||
|
||
Conditions: | ||
AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true] | ||
FunctionCondition: !Equals [true, true] | ||
|
||
Resources: | ||
InvalidType: | ||
Type: 'AWS::Serverless::Function' | ||
Condition: FunctionCondition | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/hello.zip | ||
Handler: hello.handler | ||
Runtime: python2.7 | ||
AutoPublishAlias: live | ||
DeploymentPreference: | ||
Type: Linear10PercentEvery3Minutes | ||
PassthroughCondition: ["hello"] | ||
ProvisionedConcurrencyConfig: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency | ||
- !Ref 'AWS::NoValue' | ||
|
||
UnsupportedIntrinsic: | ||
Type: 'AWS::Serverless::Function' | ||
Condition: FunctionCondition | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/hello.zip | ||
Handler: hello.handler | ||
Runtime: python2.7 | ||
AutoPublishAlias: live | ||
DeploymentPreference: | ||
Type: Linear10PercentEvery3Minutes | ||
PassthroughCondition: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- true | ||
- false | ||
ProvisionedConcurrencyConfig: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency | ||
- !Ref 'AWS::NoValue' |
110 changes: 110 additions & 0 deletions
110
.../function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.yaml
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,110 @@ | ||
# Tests supported intrinsics in the PassthroughCondition property | ||
|
||
Mappings: | ||
HelloWorldMap: | ||
hello: | ||
key1: true | ||
key2: false | ||
world: | ||
key1: false | ||
key2: true | ||
|
||
Parameters: | ||
FnName: | ||
Type: String | ||
ProvisionedConcurrency: | ||
Type: String | ||
Default: 10 | ||
EnableAliasProvisionedConcurrency: | ||
Type: String | ||
AllowedValues: | ||
- true | ||
- false | ||
Default: true | ||
DefaultTrueParam: | ||
Type: String | ||
Default: "true" | ||
DefaultFalseParam: | ||
Type: String | ||
Default: "false" | ||
HelloParam: | ||
Type: String | ||
Default: "hello" | ||
WorldParam: | ||
Type: String | ||
Default: "world" | ||
|
||
Conditions: | ||
AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true] | ||
FunctionCondition: !Equals [true, true] | ||
|
||
Resources: | ||
TrueRef: | ||
Type: 'AWS::Serverless::Function' | ||
Condition: FunctionCondition | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/hello.zip | ||
Handler: hello.handler | ||
Runtime: python2.7 | ||
AutoPublishAlias: live | ||
DeploymentPreference: | ||
Type: Linear10PercentEvery3Minutes | ||
PassthroughCondition: !Ref DefaultTrueParam | ||
ProvisionedConcurrencyConfig: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency | ||
- !Ref 'AWS::NoValue' | ||
|
||
FalseRef: | ||
Type: 'AWS::Serverless::Function' | ||
Condition: FunctionCondition | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/hello.zip | ||
Handler: hello.handler | ||
Runtime: python2.7 | ||
AutoPublishAlias: live | ||
DeploymentPreference: | ||
Type: Linear10PercentEvery3Minutes | ||
PassthroughCondition: !Ref DefaultFalseParam | ||
ProvisionedConcurrencyConfig: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency | ||
- !Ref 'AWS::NoValue' | ||
|
||
TrueFindInMap: | ||
Type: 'AWS::Serverless::Function' | ||
Condition: FunctionCondition | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/hello.zip | ||
Handler: hello.handler | ||
Runtime: python2.7 | ||
AutoPublishAlias: live | ||
DeploymentPreference: | ||
Type: Linear10PercentEvery3Minutes | ||
PassthroughCondition: !FindInMap | ||
- HelloWorldMap | ||
- !Ref HelloParam | ||
- key1 | ||
ProvisionedConcurrencyConfig: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency | ||
- !Ref 'AWS::NoValue' | ||
|
||
FalseFindInMap: | ||
Type: 'AWS::Serverless::Function' | ||
Condition: FunctionCondition | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/hello.zip | ||
Handler: hello.handler | ||
Runtime: python2.7 | ||
AutoPublishAlias: live | ||
DeploymentPreference: | ||
Type: Linear10PercentEvery3Minutes | ||
PassthroughCondition: !FindInMap | ||
- HelloWorldMap | ||
- !Ref WorldParam | ||
- key1 | ||
ProvisionedConcurrencyConfig: !If | ||
- AliasProvisionedConcurrencyEnabled | ||
- ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency | ||
- !Ref 'AWS::NoValue' |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to
sam_resources