-
Notifications
You must be signed in to change notification settings - Fork 494
Add [S3Event] annotation attribute and source generator support #2321
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
Draft
GarrettBeatty
wants to merge
5
commits into
dev
Choose a base branch
from
feature/s3-annotations
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,11 @@ | ||
| { | ||
| "Projects": [ | ||
| { | ||
| "Name": "Amazon.Lambda.Annotations", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "Added [S3Event] annotation attribute for declaratively configuring S3 event-triggered Lambda functions with support for bucket reference, event types, key prefix/suffix filters, and enabled state." | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
...rc/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/S3EventAttributeBuilder.cs
This file contains hidden or 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,34 @@ | ||
| using Amazon.Lambda.Annotations.S3; | ||
| using Microsoft.CodeAnalysis; | ||
| using System; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes | ||
| { | ||
| public class S3EventAttributeBuilder | ||
| { | ||
| public static S3EventAttribute Build(AttributeData att) | ||
| { | ||
| if (att.ConstructorArguments.Length != 1) | ||
| throw new NotSupportedException($"{TypeFullNames.S3EventAttribute} must have constructor with 1 argument."); | ||
|
|
||
| var bucket = att.ConstructorArguments[0].Value as string; | ||
| var data = new S3EventAttribute(bucket); | ||
|
|
||
| foreach (var pair in att.NamedArguments) | ||
| { | ||
| if (pair.Key == nameof(data.ResourceName) && pair.Value.Value is string resourceName) | ||
| data.ResourceName = resourceName; | ||
| else if (pair.Key == nameof(data.Events) && pair.Value.Value is string events) | ||
| data.Events = events; | ||
| else if (pair.Key == nameof(data.FilterPrefix) && pair.Value.Value is string filterPrefix) | ||
| data.FilterPrefix = filterPrefix; | ||
| else if (pair.Key == nameof(data.FilterSuffix) && pair.Value.Value is string filterSuffix) | ||
| data.FilterSuffix = filterSuffix; | ||
| else if (pair.Key == nameof(data.Enabled) && pair.Value.Value is bool enabled) | ||
| data.Enabled = enabled; | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
113 changes: 113 additions & 0 deletions
113
Libraries/src/Amazon.Lambda.Annotations/S3/S3EventAttribute.cs
This file contains hidden or 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,113 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.S3 | ||
| { | ||
| /// <summary> | ||
| /// This attribute defines the S3 event source configuration for a Lambda function. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
| public class S3EventAttribute : Attribute | ||
| { | ||
| private static readonly Regex _resourceNameRegex = new Regex("^[a-zA-Z0-9]+$"); | ||
|
|
||
| /// <summary> | ||
| /// The S3 bucket that will act as the event trigger for the Lambda function. | ||
| /// This must be a reference to an S3 bucket resource defined in the serverless template, prefixed with "@". | ||
| /// </summary> | ||
| public string Bucket { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The CloudFormation resource name for the S3 event. By default this is derived from the Bucket reference without the "@" prefix. | ||
| /// </summary> | ||
| public string ResourceName | ||
| { | ||
| get | ||
| { | ||
| if (IsResourceNameSet) | ||
| return resourceName; | ||
| if (Bucket.StartsWith("@")) | ||
| return Bucket.Substring(1); | ||
| return Bucket; | ||
| } | ||
| set => resourceName = value; | ||
| } | ||
| private string resourceName = null; | ||
| internal bool IsResourceNameSet => resourceName != null; | ||
|
|
||
| /// <summary> | ||
| /// Semicolon-separated list of S3 event types. Default is 's3:ObjectCreated:*'. | ||
| /// </summary> | ||
| public string Events { get; set; } = "s3:ObjectCreated:*"; | ||
| internal bool IsEventsSet => Events != null; | ||
|
|
||
| /// <summary> | ||
| /// S3 key prefix filter for the event notification. | ||
| /// </summary> | ||
| public string FilterPrefix | ||
| { | ||
| get => filterPrefix; | ||
| set => filterPrefix = value; | ||
| } | ||
| private string filterPrefix = null; | ||
| internal bool IsFilterPrefixSet => filterPrefix != null; | ||
|
|
||
| /// <summary> | ||
| /// S3 key suffix filter for the event notification. | ||
| /// </summary> | ||
| public string FilterSuffix | ||
| { | ||
| get => filterSuffix; | ||
| set => filterSuffix = value; | ||
| } | ||
| private string filterSuffix = null; | ||
| internal bool IsFilterSuffixSet => filterSuffix != null; | ||
|
|
||
| /// <summary> | ||
| /// If set to false, the event source will be disabled. Default value is true. | ||
| /// </summary> | ||
| public bool Enabled | ||
| { | ||
| get => enabled.GetValueOrDefault(true); | ||
| set => enabled = value; | ||
| } | ||
| private bool? enabled; | ||
| internal bool IsEnabledSet => enabled.HasValue; | ||
|
|
||
| /// <summary> | ||
| /// Creates an instance of the <see cref="S3EventAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="bucket"><see cref="Bucket"/> property</param> | ||
| public S3EventAttribute(string bucket) | ||
| { | ||
| Bucket = bucket; | ||
| } | ||
|
|
||
| internal List<string> Validate() | ||
| { | ||
| var validationErrors = new List<string>(); | ||
|
|
||
| if (string.IsNullOrEmpty(Bucket)) | ||
| { | ||
| validationErrors.Add($"{nameof(S3EventAttribute.Bucket)} is required and must not be empty"); | ||
| } | ||
| else if (!Bucket.StartsWith("@")) | ||
| { | ||
| validationErrors.Add($"{nameof(S3EventAttribute.Bucket)} = {Bucket}. S3 event sources require a reference to an S3 bucket resource in the serverless template. Prefix the resource name with '@'"); | ||
| } | ||
|
|
||
| if (IsResourceNameSet && !_resourceNameRegex.IsMatch(ResourceName)) | ||
| { | ||
| validationErrors.Add($"{nameof(S3EventAttribute.ResourceName)} = {ResourceName}. It must only contain alphanumeric characters and must not be an empty string"); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(Events)) | ||
| { | ||
| validationErrors.Add($"{nameof(S3EventAttribute.Events)} must not be empty"); | ||
| } | ||
|
|
||
| return validationErrors; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.