-
Notifications
You must be signed in to change notification settings - Fork 982
S3 transfer manager client constructor and method recipe #6151
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb17e7a
recipe added
Fred1155 4bef344
Merge branch 'master' into bole/tm_client_method_migration
Fred1155 d41acfe
delete upload recipe
Fred1155 5c0ae7a
comments addressed
Fred1155 c24387c
Merge branch 'master' into bole/tm_client_method_migration
Fred1155 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,17 +16,20 @@ | |
| package software.amazon.awssdk.v2migration; | ||
|
|
||
| import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG; | ||
| import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_TM_CLIENT; | ||
| import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_TM_MODEL_PKG; | ||
| import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2TmMethodMatcher; | ||
|
|
||
| import java.util.regex.Pattern; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.AddImport; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
|
||
| @SdkInternalApi | ||
|
|
@@ -47,6 +50,19 @@ public class TransferManagerMethodsToV2 extends Recipe { | |
| private static final MethodMatcher COPY_BUCKET_KEY = | ||
| v2TmMethodMatcher("copy(String, String, String, String"); | ||
|
|
||
| private static final MethodMatcher DOWNLOAD_DIR = v2TmMethodMatcher("downloadDirectory(String, String, java.io.File)"); | ||
|
|
||
| private static final MethodMatcher RESUME_DOWNLOAD = v2TmMethodMatcher("resumeDownload(..)"); | ||
| private static final MethodMatcher RESUME_UPLOAD = v2TmMethodMatcher("resumeUpload(..)"); | ||
| private static final MethodMatcher SHUT_DOWN_NOW = v2TmMethodMatcher("shutdownNow()"); | ||
|
|
||
|
|
||
|
|
||
| private static final Pattern S3_TM_CREDENTIAL = Pattern.compile(V2_TM_CLIENT); | ||
| private static final Pattern V2_AWSCREDENTAIL = Pattern.compile("software.amazon.awssdk.auth.credentials.AwsCredentials"); | ||
| private static final Pattern V2_CREDENTIAL_PROVIDER = Pattern.compile("software.amazon.awssdk.auth.credentials" | ||
| + ".AwsCredentialsProvider"); | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Transfer Manager Methods to V2"; | ||
|
|
@@ -62,10 +78,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() { | |
| return new Visitor(); | ||
| } | ||
|
|
||
| private static final class Visitor extends JavaIsoVisitor<ExecutionContext> { | ||
| private static final class Visitor extends JavaVisitor<ExecutionContext> { | ||
|
|
||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { | ||
| public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { | ||
|
|
||
| if (DOWNLOAD_BUCKET_KEY_FILE.matches(method, false)) { | ||
| method = transformDownloadWithBucketKeyFile(method); | ||
|
|
@@ -95,10 +111,100 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu | |
| method = transformUploadWithBucketKeyFile(method); | ||
| return super.visitMethodInvocation(method, executionContext); | ||
| } | ||
| if (DOWNLOAD_DIR.matches(method, false)) { | ||
| method = transformDownloadDirectory(method); | ||
| return super.visitMethodInvocation(method, executionContext); | ||
| } | ||
| if (RESUME_DOWNLOAD.matches(method, false)) { | ||
| method = transformResumeDownload(method); | ||
| return super.visitMethodInvocation(method, executionContext); | ||
| } | ||
| if (RESUME_UPLOAD.matches(method, false)) { | ||
| method = transformResumeUpload(method); | ||
| return super.visitMethodInvocation(method, executionContext); | ||
| } | ||
| if (SHUT_DOWN_NOW.matches(method, false)) { | ||
| method = transformShutDownNow(method); | ||
| return super.visitMethodInvocation(method, executionContext); | ||
| } | ||
|
|
||
| return super.visitMethodInvocation(method, executionContext); | ||
| } | ||
|
|
||
| @Override | ||
| public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) { | ||
| JavaType type = newClass.getType(); | ||
| if (!(type instanceof JavaType.FullyQualified)) { | ||
| return newClass; | ||
| } | ||
| if (type.isAssignableFrom(S3_TM_CREDENTIAL) && | ||
L-Applin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| newClass.getArguments().size() == 1 && | ||
| newClass.getArguments().get(0).getType() != null) { | ||
| if (newClass.getArguments().get(0).getType().isAssignableFrom(V2_AWSCREDENTAIL)) { | ||
Fred1155 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| addS3AsyncClientImport(); | ||
| addStaticCredentialsProviderImport(); | ||
|
|
||
| return JavaTemplate | ||
| .builder("S3TransferManager.builder()" + | ||
| ".s3Client(S3AsyncClient.builder()" + | ||
| ".credentialsProvider(StaticCredentialsProvider.create(#{any()}))" + | ||
| ".build())" + | ||
| ".build()") | ||
| .build() | ||
| .apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0)); | ||
| } | ||
| if (newClass.getArguments().get(0).getType().isAssignableFrom(V2_CREDENTIAL_PROVIDER)) { | ||
Fred1155 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| addS3AsyncClientImport(); | ||
|
|
||
| return JavaTemplate | ||
| .builder("S3TransferManager.builder()" + | ||
| ".s3Client(S3AsyncClient.builder()" + | ||
| ".credentialsProvider(#{any()})" + | ||
| ".build())" + | ||
| ".build()") | ||
| .build() | ||
| .apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0)); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| return super.visitNewClass(newClass, executionContext); | ||
| } | ||
|
|
||
| private J.MethodInvocation transformResumeDownload(J.MethodInvocation method) { | ||
| String v2Method = "#{any()}.resumeDownloadFile(#{any()})"; | ||
|
|
||
| method = JavaTemplate.builder(v2Method).build() | ||
| .apply(getCursor(), method.getCoordinates().replace(), method.getSelect(), | ||
| method.getArguments().get(0)); | ||
| return method; | ||
| } | ||
|
|
||
| private J.MethodInvocation transformResumeUpload(J.MethodInvocation method) { | ||
| String v2Method = "#{any()}.resumeUploadFile(#{any()})"; | ||
|
|
||
| method = JavaTemplate.builder(v2Method).build() | ||
| .apply(getCursor(), method.getCoordinates().replace(), method.getSelect(), | ||
| method.getArguments().get(0)); | ||
| return method; | ||
| } | ||
|
||
|
|
||
|
|
||
| private J.MethodInvocation transformDownloadDirectory(J.MethodInvocation method) { | ||
| String v2Method = "#{any()}.downloadDirectory(DownloadDirectoryRequest.builder()" | ||
| + ".bucket(#{any()}).listObjectsV2RequestTransformer(builder -> builder.prefix(#{any()}))" | ||
| + ".destination(#{any()}.toPath()).build())"; | ||
|
|
||
| method = JavaTemplate.builder(v2Method).build() | ||
| .apply(getCursor(), method.getCoordinates().replace(), method.getSelect(), | ||
| method.getArguments().get(0), method.getArguments().get(1), | ||
| method.getArguments().get(2)); | ||
|
|
||
| addTmImport("DirectoryDownload"); | ||
| addTmImport("DownloadDirectoryRequest"); | ||
| return method; | ||
| } | ||
|
|
||
| private J.MethodInvocation transformUploadWithBucketKeyFile(J.MethodInvocation method) { | ||
| String v2Method = "#{any()}.uploadFile(UploadFileRequest.builder()" | ||
| + ".putObjectRequest(PutObjectRequest.builder().bucket(#{any()}).key(#{any()}).build())" | ||
|
|
@@ -203,6 +309,13 @@ private J.MethodInvocation transformDownloadWithBucketKeyFileTimeout(J.MethodInv | |
| return method; | ||
| } | ||
|
|
||
| private J.MethodInvocation transformShutDownNow(J.MethodInvocation method) { | ||
| String v2Method = "#{any()}.close()"; | ||
| method = JavaTemplate.builder(v2Method).build() | ||
| .apply(getCursor(), method.getCoordinates().replace(), method.getSelect()); | ||
| return method; | ||
| } | ||
|
|
||
| private void addTmImport(String pojoName) { | ||
| String fqcn = V2_TM_MODEL_PKG + pojoName; | ||
| doAfterVisit(new AddImport<>(fqcn, null, false)); | ||
|
|
@@ -220,5 +333,13 @@ private void addDurationImport() { | |
| private void addRequestOverrideConfigImport() { | ||
| doAfterVisit(new AddImport<>("software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration", null, false)); | ||
| } | ||
|
|
||
| private void addS3AsyncClientImport() { | ||
| doAfterVisit(new AddImport<>("software.amazon.awssdk.services.s3.S3AsyncClient", null, false)); | ||
| } | ||
|
|
||
| private void addStaticCredentialsProviderImport() { | ||
| doAfterVisit(new AddImport<>("software.amazon.awssdk.auth.credentials.StaticCredentialsProvider", null, false)); | ||
| } | ||
| } | ||
| } | ||
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.
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.