Skip to content

Commit d4da845

Browse files
authored
Added support for s3-event-notifications in migration tool (#6059)
* initial recipe added for s3 event notification * WIP * WIP * test case added and fixed * getEventNameAsEnum test added * delete unnecessary method * comments for DateTime added * added recipe for getEventTime * one more method covered * checkstyle fixed * updated recipe with tm * test module merged * test case fixed
1 parent 218af5d commit d4da845

File tree

16 files changed

+446
-4
lines changed

16 files changed

+446
-4
lines changed

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
<artifactId>s3</artifactId>
4747
<version>V2_VERSION</version>
4848
</dependency>
49+
<dependency>
50+
<groupId>software.amazon.awssdk</groupId>
51+
<artifactId>s3-event-notifications</artifactId>
52+
<version>V2_VERSION</version>
53+
</dependency>
4954
<dependency>
5055
<groupId>software.amazon.awssdk</groupId>
5156
<artifactId>s3-transfer-manager</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 foo.bar;
17+
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification;
18+
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotificationRecord;
19+
import software.amazon.awssdk.eventnotifications.s3.model.RestoreEventData;
20+
import software.amazon.awssdk.eventnotifications.s3.model.GlacierEventData;
21+
import org.joda.time.DateTime;
22+
23+
public class S3EnDateTime {
24+
25+
public void parseEvent(String jsonInput) {
26+
S3EventNotification notification = S3EventNotification.fromJson(jsonInput);
27+
28+
for (S3EventNotification.S3EventNotificationRecord record : notification.getRecords()) {
29+
DateTime eventTime = /*AWS SDK for Java v2 migration: getEventTime returns Instant instead of DateTime in v2. AWS SDK v2 does not include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to manually add "org.joda.time:joda-time" dependency to your project after migration.*/new DateTime(record.getEventTime().toEpochMilli());
30+
31+
GlacierEventData glacierEventData = record.getGlacierEventData();
32+
33+
RestoreEventData restoreEventData = glacierEventData.getRestoreEventData();
34+
35+
DateTime expireTime = /*AWS SDK for Java v2 migration: getLifecycleRestorationExpiryTime returns Instant instead of DateTime in v2. AWS SDK v2 does not include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to manually add "org.joda.time:joda-time" dependency to your project after migration.*/new DateTime(restoreEventData.getLifecycleRestorationExpiryTime().toEpochMilli());
36+
37+
}
38+
}
39+
40+
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/src/main/java/foo/bar/S3Transforms.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
import com.amazonaws.HttpMethod;
1919
import com.amazonaws.services.s3.model.SSEAwsKeyManagementParams;
2020
import com.amazonaws.services.s3.model.SSECustomerKey;
21-
import java.io.ByteArrayInputStream;
22-
import java.io.InputStream;
23-
import java.net.URL;
24-
import java.util.Date;
2521
import software.amazon.awssdk.core.async.AsyncRequestBody;
2622
import software.amazon.awssdk.services.s3.S3Client;
2723
import software.amazon.awssdk.services.s3.model.AccessControlPolicy;
@@ -34,6 +30,11 @@
3430
import software.amazon.awssdk.transfer.s3.S3TransferManager;
3531
import software.amazon.awssdk.transfer.s3.model.UploadRequest;
3632

33+
import java.io.ByteArrayInputStream;
34+
import java.io.InputStream;
35+
import java.net.URL;
36+
import java.util.Date;
37+
3738
public class S3Transforms {
3839

3940
S3Client s3;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 foo.bar;
17+
18+
import com.amazonaws.services.s3.event.S3EventNotification;
19+
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
20+
import com.amazonaws.services.s3.event.S3EventNotification.RestoreEventDataEntity;
21+
import com.amazonaws.services.s3.event.S3EventNotification.GlacierEventDataEntity;
22+
import com.amazonaws.services.s3.model.S3Event;
23+
import org.joda.time.DateTime;
24+
25+
public class S3EnDateTime {
26+
27+
public void parseEvent(String jsonInput) {
28+
S3EventNotification notification = S3EventNotification.parseJson(jsonInput);
29+
30+
for (S3EventNotification.S3EventNotificationRecord record : notification.getRecords()) {
31+
DateTime eventTime = record.getEventTime();
32+
33+
GlacierEventDataEntity glacierEventData = record.getGlacierEventData();
34+
35+
RestoreEventDataEntity restoreEventData = glacierEventData.getRestoreEventData();
36+
37+
DateTime expireTime = restoreEventData.getLifecycleRestorationExpiryTime();
38+
39+
}
40+
}
41+
42+
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
<artifactId>netty-nio-client</artifactId>
5858
<version>V2_VERSION</version>
5959
</dependency>
60+
<dependency>
61+
<groupId>software.amazon.awssdk</groupId>
62+
<artifactId>s3-event-notifications</artifactId>
63+
<version>V2_VERSION</version>
64+
</dependency>
6065
<dependency>
6166
<groupId>software.amazon.awssdk</groupId>
6267
<artifactId>s3</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 foo.bar;
17+
18+
import software.amazon.awssdk.eventnotifications.s3.model.S3Bucket;
19+
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification;
20+
import software.amazon.awssdk.eventnotifications.s3.model.S3;
21+
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotificationRecord;
22+
import software.amazon.awssdk.eventnotifications.s3.model.S3Object;
23+
import software.amazon.awssdk.eventnotifications.s3.model.RequestParameters;
24+
import software.amazon.awssdk.eventnotifications.s3.model.ResponseElements;
25+
import software.amazon.awssdk.eventnotifications.s3.model.RestoreEventData;
26+
import software.amazon.awssdk.eventnotifications.s3.model.UserIdentity;
27+
import software.amazon.awssdk.eventnotifications.s3.model.GlacierEventData;
28+
import software.amazon.awssdk.eventnotifications.s3.model.LifecycleEventData;
29+
import software.amazon.awssdk.eventnotifications.s3.model.IntelligentTieringEventData;
30+
import software.amazon.awssdk.eventnotifications.s3.model.ReplicationEventData;
31+
32+
public class S3EventNotificationTest {
33+
public void parseEvent(String jsonInput) {
34+
S3EventNotification notification = S3EventNotification.fromJson(jsonInput);
35+
36+
for (S3EventNotificationRecord record : notification.getRecords()) {
37+
S3 s3 = record.getS3();
38+
39+
S3Bucket bucket = s3.getBucket();
40+
41+
S3Object object = s3.getObject();
42+
43+
String eventName = record.getEventName();
44+
45+
String eventNameEnum = record.getEventName();
46+
47+
RequestParameters requestParams = record.getRequestParameters();
48+
49+
ResponseElements responseElements = record.getResponseElements();
50+
51+
UserIdentity userIdentity = record.getUserIdentity();
52+
53+
GlacierEventData glacierEventData = record.getGlacierEventData();
54+
55+
RestoreEventData restoreEventData = glacierEventData.getRestoreEventData();
56+
57+
LifecycleEventData lifecycleEventData = record.getLifecycleEventData();
58+
59+
IntelligentTieringEventData intelligentTieringEventData = record.getIntelligentTieringEventData();
60+
61+
ReplicationEventData replicationEventData = record.getReplicationEventData();
62+
}
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 foo.bar;
17+
18+
import com.amazonaws.services.s3.event.S3EventNotification;
19+
import com.amazonaws.services.s3.event.S3EventNotification.S3BucketEntity;
20+
import com.amazonaws.services.s3.event.S3EventNotification.S3Entity;
21+
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
22+
import com.amazonaws.services.s3.event.S3EventNotification.S3ObjectEntity;
23+
import com.amazonaws.services.s3.event.S3EventNotification.RequestParametersEntity;
24+
import com.amazonaws.services.s3.event.S3EventNotification.ResponseElementsEntity;
25+
import com.amazonaws.services.s3.event.S3EventNotification.RestoreEventDataEntity;
26+
import com.amazonaws.services.s3.event.S3EventNotification.UserIdentityEntity;
27+
import com.amazonaws.services.s3.event.S3EventNotification.GlacierEventDataEntity;
28+
import com.amazonaws.services.s3.event.S3EventNotification.LifecycleEventDataEntity;
29+
import com.amazonaws.services.s3.event.S3EventNotification.IntelligentTieringEventDataEntity;
30+
import com.amazonaws.services.s3.event.S3EventNotification.ReplicationEventDataEntity;
31+
import com.amazonaws.services.s3.model.S3Event;
32+
33+
public class S3EventNotificationTest {
34+
public void parseEvent(String jsonInput) {
35+
S3EventNotification notification = S3EventNotification.parseJson(jsonInput);
36+
37+
for (S3EventNotificationRecord record : notification.getRecords()) {
38+
S3Entity s3 = record.getS3();
39+
40+
S3BucketEntity bucket = s3.getBucket();
41+
42+
S3ObjectEntity object = s3.getObject();
43+
44+
String eventName = record.getEventName();
45+
46+
S3Event eventNameEnum = record.getEventNameAsEnum();
47+
48+
RequestParametersEntity requestParams = record.getRequestParameters();
49+
50+
ResponseElementsEntity responseElements = record.getResponseElements();
51+
52+
UserIdentityEntity userIdentity = record.getUserIdentity();
53+
54+
GlacierEventDataEntity glacierEventData = record.getGlacierEventData();
55+
56+
RestoreEventDataEntity restoreEventData = glacierEventData.getRestoreEventData();
57+
58+
LifecycleEventDataEntity lifecycleEventData = record.getLifecycleEventData();
59+
60+
IntelligentTieringEventDataEntity intelligentTieringEventData = record.getIntelligentTieringEventData();
61+
62+
ReplicationEventDataEntity replicationEventData = record.getReplicationEventDataEntity();
63+
}
64+
}
65+
66+
}

v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3AddImportsAndComments.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_MODEL_PKG;
1919
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG;
2020
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments;
21+
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1EnMethodMatcher;
2122
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1S3MethodMatcher;
2223

2324
import java.util.List;
@@ -52,6 +53,10 @@ public class S3AddImportsAndComments extends Recipe {
5253
private static final MethodMatcher SELECT_OBJECT_CONTENT = v1S3MethodMatcher("selectObjectContent(..)");
5354
private static final MethodMatcher SET_LIFECYCLE_CONFIGURATION = v1S3MethodMatcher("setBucketLifecycleConfiguration(..)");
5455
private static final MethodMatcher SET_TAGGING_CONFIGURATION = v1S3MethodMatcher("setBucketTaggingConfiguration(..)");
56+
private static final MethodMatcher GET_EVENT_TIME = v1EnMethodMatcher("S3EventNotification.S3EventNotificationRecord "
57+
+ "getEventTime(..)");
58+
private static final MethodMatcher GET_EXPIRY_TIME = v1EnMethodMatcher("S3EventNotification.RestoreEventDataEntity "
59+
+ "getLifecycleRestorationExpiryTime(..)");
5560

5661

5762
private static final Pattern CANNED_ACL = Pattern.compile(V1_S3_MODEL_PKG + "CannedAccessControlList");
@@ -191,6 +196,13 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
191196
return method.withComments(createComments(comment));
192197
}
193198

199+
if (GET_EVENT_TIME.matches(method) || GET_EXPIRY_TIME.matches(method)) {
200+
String comment = method.getSimpleName() + " returns Instant instead of DateTime in v2. AWS SDK v2 does not "
201+
+ "include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to "
202+
+ "manually add \"org.joda.time:joda-time\" dependency to your"
203+
+ " project after migration.";
204+
return method.withComments(createComments(comment));
205+
}
194206
return method;
195207
}
196208

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.v1EnMethodMatcher;
19+
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.java.JavaTemplate;
24+
import org.openrewrite.java.JavaVisitor;
25+
import org.openrewrite.java.MethodMatcher;
26+
import org.openrewrite.java.tree.J;
27+
import software.amazon.awssdk.annotations.SdkInternalApi;
28+
29+
@SdkInternalApi
30+
public class S3EventNotificationMethodToV2 extends Recipe {
31+
32+
private static final MethodMatcher GET_EVENT_TIME = v1EnMethodMatcher("S3EventNotification.S3EventNotificationRecord "
33+
+ "getEventTime(..)");
34+
35+
private static final MethodMatcher GET_EXPIRY_TIME = v1EnMethodMatcher("S3EventNotification.RestoreEventDataEntity "
36+
+ "getLifecycleRestorationExpiryTime(..)");
37+
38+
@Override
39+
public String getDisplayName() {
40+
return "S3 Event Notification method to v2";
41+
}
42+
43+
@Override
44+
public String getDescription() {
45+
return "S3 Event Notification method to v2";
46+
}
47+
48+
@Override
49+
public TreeVisitor<?, ExecutionContext> getVisitor() {
50+
return new Visitor();
51+
}
52+
53+
private static class Visitor extends JavaVisitor<ExecutionContext> {
54+
@Override
55+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
56+
if (GET_EVENT_TIME.matches(method) || GET_EXPIRY_TIME.matches(method)) {
57+
JavaTemplate template = JavaTemplate.builder("new DateTime(#{any(java.time.Instant)}.toEpochMilli())")
58+
.build();
59+
J m = super.visitMethodInvocation(method, ctx);
60+
m = template.apply(getCursor(), ((J.MethodInvocation) m).getCoordinates().replace(), m);
61+
return m;
62+
}
63+
return method;
64+
}
65+
66+
}
67+
68+
}

v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class S3TransformUtils {
3737
public static final String V1_S3_CLIENT = "com.amazonaws.services.s3.AmazonS3";
3838
public static final String V1_S3_MODEL_PKG = "com.amazonaws.services.s3.model.";
3939
public static final String V1_S3_PKG = "com.amazonaws.services.s3.";
40+
public static final String V1_EN_PKG = "com.amazonaws.services.s3.event.";
4041

4142
public static final String V2_S3_CLIENT = "software.amazon.awssdk.services.s3.S3Client";
4243
public static final String V2_S3_MODEL_PKG = "software.amazon.awssdk.services.s3.model.";
@@ -77,6 +78,10 @@ public static MethodMatcher v1S3MethodMatcher(String methodSignature) {
7778
return new MethodMatcher(V1_S3_CLIENT + " " + methodSignature, true);
7879
}
7980

81+
public static MethodMatcher v1EnMethodMatcher(String methodSignature) {
82+
return new MethodMatcher(V1_EN_PKG + methodSignature, true);
83+
}
84+
8085
public static MethodMatcher v2S3MethodMatcher(String methodSignature) {
8186
return new MethodMatcher(V2_S3_CLIENT + " " + methodSignature, true);
8287
}

v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/SdkTypeUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public final class SdkTypeUtils {
174174
CLASSES_TO_SKIP.add(V1_S3_MODEL_PKG + "S3ObjectId");
175175
CLASSES_TO_SKIP.add(V1_S3_MODEL_PKG + "S3ObjectIdBuilder");
176176
CLASSES_TO_SKIP.add(V1_S3_MODEL_PKG + "TagSet");
177+
CLASSES_TO_SKIP.add(V1_S3_MODEL_PKG + "SelectObjectContentEvent");
177178

178179
// S3 Enums with no v2 equivalent
179180
CLASSES_TO_SKIP.add(V1_S3_MODEL_PKG + "GroupGrantee");

0 commit comments

Comments
 (0)