|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.v2migration; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_MODEL_PKG; |
| 19 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG; |
| 20 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments; |
| 21 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1S3MethodMatcher; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.regex.Pattern; |
| 25 | +import org.openrewrite.ExecutionContext; |
| 26 | +import org.openrewrite.Recipe; |
| 27 | +import org.openrewrite.TreeVisitor; |
| 28 | +import org.openrewrite.java.AddImport; |
| 29 | +import org.openrewrite.java.JavaIsoVisitor; |
| 30 | +import org.openrewrite.java.MethodMatcher; |
| 31 | +import org.openrewrite.java.RemoveImport; |
| 32 | +import org.openrewrite.java.tree.Expression; |
| 33 | +import org.openrewrite.java.tree.J; |
| 34 | +import org.openrewrite.java.tree.JavaType; |
| 35 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 36 | + |
| 37 | +@SdkInternalApi |
| 38 | +public class S3AddImportsAndComments extends Recipe { |
| 39 | + |
| 40 | + private static final MethodMatcher CREATE_BUCKET = v1S3MethodMatcher("createBucket(String, " |
| 41 | + + V1_S3_MODEL_PKG + "Region"); |
| 42 | + private static final MethodMatcher LIST_NEXT_BATCH_OBJECTS = v1S3MethodMatcher("listNextBatchOfObjects(..)"); |
| 43 | + private static final MethodMatcher LIST_NEXT_BATCH_VERSIONS = v1S3MethodMatcher("listNextBatchOfVersions(..)"); |
| 44 | + private static final MethodMatcher GET_METADATA = v1S3MethodMatcher("getCachedResponseMetadata(..)"); |
| 45 | + private static final MethodMatcher SET_BUCKET_ACL = v1S3MethodMatcher("setBucketAcl(..)"); |
| 46 | + private static final MethodMatcher SET_ENDPOINT = v1S3MethodMatcher("setEndpoint(..)"); |
| 47 | + private static final MethodMatcher SET_OBJECT_ACL = v1S3MethodMatcher("setObjectAcl(..)"); |
| 48 | + private static final MethodMatcher SET_REGION = v1S3MethodMatcher("setRegion(..)"); |
| 49 | + private static final MethodMatcher SET_S3CLIENT_OPTIONS = v1S3MethodMatcher("setS3ClientOptions(..)"); |
| 50 | + private static final MethodMatcher SELECT_OBJECT_CONTENT = v1S3MethodMatcher("selectObjectContent(..)"); |
| 51 | + |
| 52 | + private static final Pattern CANNED_ACL = Pattern.compile(V1_S3_MODEL_PKG + "CannedAccessControlList"); |
| 53 | + private static final Pattern GET_OBJECT_REQUEST = Pattern.compile(V1_S3_MODEL_PKG + "GetObjectRequest"); |
| 54 | + private static final Pattern INITIATE_MPU = Pattern.compile(V1_S3_MODEL_PKG + "InitiateMultipartUpload"); |
| 55 | + private static final Pattern MULTI_FACTOR_AUTH = Pattern.compile(V1_S3_MODEL_PKG + "MultiFactorAuthentication"); |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getDisplayName() { |
| 59 | + return "Add imports and comments to unsupported S3 transforms."; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getDescription() { |
| 64 | + return "Add imports and comments to unsupported S3 transforms."; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 69 | + return new Visitor(); |
| 70 | + } |
| 71 | + |
| 72 | + private static class Visitor extends JavaIsoVisitor<ExecutionContext> { |
| 73 | + |
| 74 | + @Override |
| 75 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 76 | + boolean isSetObjectAcl = SET_OBJECT_ACL.matches(method); |
| 77 | + boolean isSetBucketAcl = SET_BUCKET_ACL.matches(method); |
| 78 | + |
| 79 | + if (isSetObjectAcl || isSetBucketAcl) { |
| 80 | + removeV1S3ModelImport("CannedAccessControlList"); |
| 81 | + maybeAddV2CannedAclImport(method.getArguments(), isSetObjectAcl, isSetBucketAcl); |
| 82 | + |
| 83 | + // TODO: add the developer guide link in the comments once the doc is published. |
| 84 | + String comment = "Transform for AccessControlList and CannedAccessControlList not supported. " |
| 85 | + + "In v2, CannedAccessControlList is replaced by BucketCannedACL for buckets and " |
| 86 | + + "ObjectCannedACL for objects."; |
| 87 | + return method.withComments(createComments(comment)); |
| 88 | + } |
| 89 | + if (LIST_NEXT_BATCH_OBJECTS.matches(method)) { |
| 90 | + // TODO: add the developer guide link in the comments once the doc is published. |
| 91 | + String comment = "Transform for listNextBatchOfObjects method not supported. " |
| 92 | + + "listNextBatchOfObjects() only exists in SDK v1, for SDK v2 use either " |
| 93 | + + "listObjectsV2Paginator().stream() for automatic pagination" |
| 94 | + + " or manually handle pagination with listObjectsV2() and nextToken in the response for more " |
| 95 | + + "control"; |
| 96 | + return method.withComments(createComments(comment)); |
| 97 | + } |
| 98 | + if (LIST_NEXT_BATCH_VERSIONS.matches(method)) { |
| 99 | + // TODO: add the developer guide link in the comments once the doc is published. |
| 100 | + String comment = "Transform for listNextBatchOfVersions method not supported." |
| 101 | + + "listNextBatchOfVersions() only exists in SDK v1, for SDK v2 use either " |
| 102 | + + "listObjectVersionsPaginator().stream for automatic pagination" |
| 103 | + + " or manually handle pagination with listObjectVersions() and VersionIdMarker/KeyMarker. "; |
| 104 | + return method.withComments(createComments(comment)); |
| 105 | + } |
| 106 | + if (SET_REGION.matches(method)) { |
| 107 | + String comment = "Transform for setRegion method not supported. Please manually " |
| 108 | + + "migrate your code by configuring the region in the s3 client builder"; |
| 109 | + return method.withComments(createComments(comment)); |
| 110 | + } |
| 111 | + if (SET_S3CLIENT_OPTIONS.matches(method)) { |
| 112 | + String comment = "Transform for setS3ClientOptions method not supported. Please manually " |
| 113 | + + "migrate setS3ClientOptions by configuring the equivalent settings in " |
| 114 | + + "S3Configuration.builder() when building your S3Client."; |
| 115 | + return method.withComments(createComments(comment)); |
| 116 | + } |
| 117 | + if (SELECT_OBJECT_CONTENT.matches(method)) { |
| 118 | + String comment = "Note: selectObjectContent is only supported in AWS SDK v2 with S3AsyncClient. " |
| 119 | + + "Please manually migrate to event-based response handling using " |
| 120 | + + "SelectObjectContentEventStream"; |
| 121 | + return method.withComments(createComments(comment)); |
| 122 | + } |
| 123 | + |
| 124 | + if (CREATE_BUCKET.matches(method)) { |
| 125 | + String comment = "Transform for createBucket(String bucketName, Region region) method not supported. Please " |
| 126 | + + "manually migrate your code by using the following pattern: " |
| 127 | + + "createBucket(builder -> builder.bucket(bucketName)" |
| 128 | + + ".createBucketConfiguration(cfg -> cfg.locationConstraint(region)))"; |
| 129 | + return method.withComments(createComments(comment)); |
| 130 | + } |
| 131 | + |
| 132 | + if (SET_ENDPOINT.matches(method)) { |
| 133 | + String comment = "Transform for setEndpoint method not supported. setEndpoint() method is removed in SDK v2. " |
| 134 | + + "Please manually migrate your code by using endpointOverride(URI.create(endpoint)) in " |
| 135 | + + "S3ClientBuilder"; |
| 136 | + return method.withComments(createComments(comment)); |
| 137 | + } |
| 138 | + |
| 139 | + if (GET_METADATA.matches(method)) { |
| 140 | + String comment = "Transform for getCachedResponseMetadata method not " |
| 141 | + + "supported. getCachedResponseMetadata() is removed in SDK v2. Please manually migrate your " |
| 142 | + + "code by accessing metadata directly from specific response objects instead of cached " |
| 143 | + + "metadata"; |
| 144 | + return method.withComments(createComments(comment)); |
| 145 | + } |
| 146 | + |
| 147 | + return method; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { |
| 152 | + JavaType type = newClass.getType(); |
| 153 | + if (!(type instanceof JavaType.FullyQualified)) { |
| 154 | + return newClass; |
| 155 | + } |
| 156 | + |
| 157 | + if (type.isAssignableFrom(MULTI_FACTOR_AUTH)) { |
| 158 | + removeV1S3ModelImport("MultiFactorAuthentication"); |
| 159 | + String comment = "v2 does not have a MultiFactorAuthentication POJO. Please manually set the String value on " |
| 160 | + + "the request POJO."; |
| 161 | + return newClass.withComments(createComments(comment)); |
| 162 | + } |
| 163 | + |
| 164 | + if (type.isAssignableFrom(GET_OBJECT_REQUEST) && newClass.getArguments().size() == 1) { |
| 165 | + removeV1S3ModelImport("S3ObjectId"); |
| 166 | + String comment = "v2 does not have S3ObjectId class. Please manually migrate the code by setting the configs " |
| 167 | + + "directly into the request builder pattern."; |
| 168 | + return newClass.withComments(createComments(comment)); |
| 169 | + } |
| 170 | + |
| 171 | + if (type.isAssignableFrom(INITIATE_MPU) && newClass.getArguments().size() == 3) { |
| 172 | + String comment = "Transform for ObjectMetadata in initiateMultipartUpload() method is not supported. Please " |
| 173 | + + "manually migrate your code by replacing ObjectMetadata with individual setter methods " |
| 174 | + + "or metadata map in the request builder."; |
| 175 | + return newClass.withComments(createComments(comment)); |
| 176 | + } |
| 177 | + |
| 178 | + return newClass; |
| 179 | + } |
| 180 | + |
| 181 | + private void maybeAddV2CannedAclImport(List<Expression> args, boolean isSetObjectAcl, boolean isSetBucketAcl) { |
| 182 | + for (Expression expr : args) { |
| 183 | + JavaType type = expr.getType(); |
| 184 | + if (type == null || !type.isAssignableFrom(CANNED_ACL)) { |
| 185 | + continue; |
| 186 | + } |
| 187 | + removeV1S3ModelImport("CannedAccessControlList"); |
| 188 | + if (isSetBucketAcl) { |
| 189 | + addV2S3ModelImport("BucketCannedACL"); |
| 190 | + } |
| 191 | + if (isSetObjectAcl) { |
| 192 | + addV2S3ModelImport("ObjectCannedACL"); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private void removeV1S3ModelImport(String className) { |
| 198 | + doAfterVisit(new RemoveImport<>(V1_S3_MODEL_PKG + className, true)); |
| 199 | + } |
| 200 | + |
| 201 | + private void addV2S3ModelImport(String className) { |
| 202 | + doAfterVisit(new AddImport<>(V2_S3_MODEL_PKG + className, null, false)); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments