Skip to content

Commit 1b5f27e

Browse files
authored
Add mappping for non-HTTP config settings and add comments for settings (#5181)
1 parent 0734f68 commit 1b5f27e

File tree

12 files changed

+391
-45
lines changed

12 files changed

+391
-45
lines changed

migration-tool/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
<scope>compile</scope>
8585
</dependency>
8686

87+
<dependency>
88+
<groupId>org.openrewrite.recipe</groupId>
89+
<artifactId>rewrite-migrate-java</artifactId>
90+
</dependency>
91+
8792
<dependency>
8893
<groupId>org.openrewrite</groupId>
8994
<artifactId>rewrite-test</artifactId>
@@ -112,6 +117,21 @@
112117
<artifactId>utils</artifactId>
113118
<version>${project.version}</version>
114119
</dependency>
120+
<dependency>
121+
<groupId>software.amazon.awssdk</groupId>
122+
<artifactId>sdk-core</artifactId>
123+
<version>${project.version}</version>
124+
</dependency>
125+
<dependency>
126+
<groupId>software.amazon.awssdk</groupId>
127+
<artifactId>auth</artifactId>
128+
<version>${project.version}</version>
129+
</dependency>
130+
<dependency>
131+
<groupId>software.amazon.awssdk</groupId>
132+
<artifactId>sts</artifactId>
133+
<version>${project.version}</version>
134+
</dependency>
115135
<!-- Used in UpgradeSdkDependenciesTest -->
116136
<dependency>
117137
<groupId>software.amazon.awssdk</groupId>

migration-tool/src/main/java/software/amazon/awssdk/migration/internal/recipe/AddCommentToMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {
7272

7373
Visitor(String methodPattern, String comment) {
7474
this.methodMatcher = new MethodMatcher(methodPattern, false);
75-
this.comment = COMMENT_PREFIX + comment;
75+
this.comment = COMMENT_PREFIX + comment + "\n";
7676
}
7777

7878
@Override

migration-tool/src/main/java/software/amazon/awssdk/migration/internal/recipe/V1SetterToV2.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
package software.amazon.awssdk.migration.internal.recipe;
1717

18+
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isEligibleToConvertToBuilder;
1819
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ClientClass;
19-
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ModelClass;
2020

2121
import java.util.Map;
2222
import org.openrewrite.ExecutionContext;
@@ -37,10 +37,13 @@
3737
* for generated model classes and client classes.
3838
*
3939
* @see NewClassToBuilderPattern
40+
* TODO: separate model classes and client classes
4041
*/
4142
@SdkInternalApi
4243
public class V1SetterToV2 extends Recipe {
4344
private static final Map<String, String> CLIENT_CONFIG_NAMING_MAPPING =
45+
// TODO: handle other settings on the builder such as withEndpointConfiguration,
46+
// withMonitoringListener and withMetricsCollector
4447
ImmutableMap.<String, String>builder()
4548
.put("credentials", "credentialsProvider")
4649
.put("clientConfiguration", "overrideConfiguration")
@@ -75,11 +78,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
7578
selectType = select.getType();
7679
}
7780

78-
if (selectType == null || !shouldChangeSetter(selectType)) {
81+
if (selectType == null) {
7982
return method;
8083
}
8184

8285
String methodName = method.getSimpleName();
86+
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType);
87+
88+
if (!shouldChangeSetter(fullyQualified)) {
89+
return method;
90+
}
8391

8492
if (NamingUtils.isWither(methodName)) {
8593
methodName = NamingUtils.removeWith(methodName);
@@ -97,8 +105,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
97105
mt = mt.withName(methodName)
98106
.withReturnType(selectType);
99107

100-
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType);
101-
102108
if (fullyQualified != null) {
103109
mt = mt.withDeclaringType(fullyQualified);
104110
}
@@ -112,8 +118,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
112118
return method;
113119
}
114120

115-
private static boolean shouldChangeSetter(JavaType selectType) {
116-
return isV2ModelClass(selectType) || isV2ClientClass(selectType);
121+
private static boolean shouldChangeSetter(JavaType.FullyQualified selectType) {
122+
return isEligibleToConvertToBuilder(selectType);
117123
}
118124
}
119125
}

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@
2323
import org.openrewrite.java.tree.JavaType;
2424
import org.openrewrite.java.tree.TypeUtils;
2525
import software.amazon.awssdk.annotations.SdkInternalApi;
26+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
27+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
28+
import software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider;
29+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
30+
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
31+
import software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider;
32+
import software.amazon.awssdk.auth.credentials.ProcessCredentialsProvider;
33+
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
34+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
35+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
36+
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
37+
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider;
38+
import software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider;
2639
import software.amazon.awssdk.utils.ImmutableMap;
2740

2841
/**
@@ -35,11 +48,11 @@ public final class SdkTypeUtils {
3548
*/
3649
public static final Map<String, Integer> V2_CORE_CLASSES_WITH_STATIC_FACTORY =
3750
ImmutableMap.<String, Integer>builder()
38-
.put("software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider", 0)
39-
.put("software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider", 0)
40-
.put("software.amazon.awssdk.auth.credentials.AwsBasicCredentials", 2)
41-
.put("software.amazon.awssdk.auth.credentials.AwsSessionCredentials", 3)
42-
.put("software.amazon.awssdk.auth.credentials.StaticCredentialsProvider", 1)
51+
.put(EnvironmentVariableCredentialsProvider.class.getCanonicalName(), 0)
52+
.put(InstanceProfileCredentialsProvider.class.getCanonicalName(), 0)
53+
.put(AwsBasicCredentials.class.getCanonicalName(), 2)
54+
.put(AwsSessionCredentials.class.getCanonicalName(), 3)
55+
.put(StaticCredentialsProvider.class.getCanonicalName(), 1)
4356
.build();
4457

4558
private static final Pattern V1_SERVICE_CLASS_PATTERN =
@@ -66,15 +79,15 @@ public final class SdkTypeUtils {
6679
* V2 core classes with a builder
6780
*/
6881
private static final Set<String> V2_CORE_CLASSES_WITH_BUILDER =
69-
new HashSet<>(Arrays.asList("software.amazon.awssdk.core.client.ClientOverrideConfiguration",
70-
"software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider",
71-
"software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider",
72-
"software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider",
73-
"software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider",
74-
"software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider",
75-
"software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider",
76-
"software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider",
77-
"software.amazon.awssdk.auth.credentials.ProcessCredentialsProvider"));
82+
new HashSet<>(Arrays.asList(ClientOverrideConfiguration.class.getCanonicalName(),
83+
DefaultCredentialsProvider.class.getCanonicalName(),
84+
ProfileCredentialsProvider.class.getCanonicalName(),
85+
ContainerCredentialsProvider.class.getCanonicalName(),
86+
InstanceProfileCredentialsProvider.class.getCanonicalName(),
87+
StsAssumeRoleCredentialsProvider.class.getCanonicalName(),
88+
StsGetSessionTokenCredentialsProvider.class.getCanonicalName(),
89+
StsAssumeRoleWithWebIdentityCredentialsProvider.class.getCanonicalName(),
90+
ProcessCredentialsProvider.class.getCanonicalName()));
7891

7992
private static final Pattern V2_CLIENT_BUILDER_PATTERN = Pattern.compile(
8093
"software\\.amazon\\.awssdk\\.services\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+Builder");
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
## TODO: support retry policy, signer and throttledRetries
16+
---
17+
type: specs.openrewrite.org/v1beta/recipe
18+
name: software.amazon.awssdk.ChangeConfigTypes
19+
displayName: Change region related classes
20+
recipeList:
21+
- org.openrewrite.java.ChangeMethodName:
22+
methodPattern: com.amazonaws.ClientConfiguration withRequestTimeout(int)
23+
newMethodName: withApiCallAttemptTimeout
24+
- org.openrewrite.java.ChangeMethodName:
25+
methodPattern: com.amazonaws.ClientConfiguration setRequestTimeout(int)
26+
newMethodName: withApiCallAttemptTimeout
27+
- software.amazon.awssdk.migration.internal.recipe.NumberToDuration:
28+
methodPattern: com.amazonaws.ClientConfiguration withApiCallAttemptTimeout(int)
29+
30+
- org.openrewrite.java.ChangeMethodName:
31+
methodPattern: com.amazonaws.ClientConfiguration withClientExecutionTimeout(int)
32+
newMethodName: withApiCallTimeout
33+
- org.openrewrite.java.ChangeMethodName:
34+
methodPattern: com.amazonaws.ClientConfiguration setClientExecutionTimeout(int)
35+
newMethodName: withApiCallTimeout
36+
- software.amazon.awssdk.migration.internal.recipe.NumberToDuration:
37+
methodPattern: com.amazonaws.ClientConfiguration withApiCallTimeout(int)
38+
39+
- org.openrewrite.java.ChangeMethodName:
40+
methodPattern: com.amazonaws.ClientConfiguration withRetryMode(..)
41+
newMethodName: withRetryPolicy
42+
- org.openrewrite.java.ChangeMethodName:
43+
methodPattern: com.amazonaws.ClientConfiguration setRetryMode(..)
44+
newMethodName: withRetryPolicy
45+
- org.openrewrite.java.ChangeMethodName:
46+
methodPattern: com.amazonaws.ClientConfiguration withHeader(String, String)
47+
newMethodName: withPutHeader
48+
- org.openrewrite.java.ChangeMethodName:
49+
methodPattern: com.amazonaws.ClientConfiguration setHeader(String, String)
50+
newMethodName: withPutHeader
51+
52+
## Add comment to unsupported options
53+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
54+
methodPattern: com.amazonaws.ClientConfiguration setMaxConsecutiveRetriesBeforeThrottling(int)
55+
comment: maxConsecutiveRetriesBeforeThrottling is deprecated and not supported in v2. Consider removing it or using a custom RetryPolicy.
56+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
57+
methodPattern: com.amazonaws.ClientConfiguration withMaxConsecutiveRetriesBeforeThrottling(int)
58+
comment: maxConsecutiveRetriesBeforeThrottling is deprecated and not supported in v2. Consider removing it or using a custom RetryPolicy.
59+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
60+
methodPattern: com.amazonaws.ClientConfiguration setCacheResponseMetadata(boolean)
61+
comment: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.
62+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
63+
methodPattern: com.amazonaws.ClientConfiguration withCacheResponseMetadata(boolean)
64+
comment: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.
65+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
66+
methodPattern: com.amazonaws.ClientConfiguration withDisableHostPrefixInjection(boolean)
67+
comment: disableHostPrefixInjection is deprecated and not supported removed in v2. Consider removing it.
68+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
69+
methodPattern: com.amazonaws.ClientConfiguration setDisableHostPrefixInjection(boolean)
70+
comment: disableHostPrefixInjection is deprecated and not supported in v2. Consider removing it.
71+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
72+
methodPattern: com.amazonaws.ClientConfiguration setDnsResolver(..)
73+
comment: dnsResolver is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
74+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
75+
methodPattern: com.amazonaws.ClientConfiguration withDnsResolver(..)
76+
comment: dnsResolver is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
77+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
78+
methodPattern: com.amazonaws.ClientConfiguration setGzip(boolean)
79+
comment: gzip is not supported in v2 tracking in https://github.com/aws/aws-sdk-java-v2/issues/866. Consider removing it.
80+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
81+
methodPattern: com.amazonaws.ClientConfiguration withGzip(boolean)
82+
comment: gzip is not supported in v2 tracking in https://github.com/aws/aws-sdk-java-v2/issues/866. Consider removing it.
83+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
84+
methodPattern: com.amazonaws.ClientConfiguration setLocalAddress(..)
85+
comment: localAddress is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
86+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
87+
methodPattern: com.amazonaws.ClientConfiguration withLocalAddress(..)
88+
comment: localAddress is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
89+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
90+
methodPattern: com.amazonaws.ClientConfiguration setSecureRandom(.*)
91+
comment: secureRandom is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
92+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
93+
methodPattern: com.amazonaws.ClientConfiguration withSecureRandom(.*)
94+
comment: secureRandom is supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
95+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
96+
methodPattern: com.amazonaws.ClientConfiguration setUseExpectContinue(boolean)
97+
comment: useExpectContinue is removed in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
98+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
99+
methodPattern: com.amazonaws.ClientConfiguration withUseExpectContinue(boolean)
100+
comment: useExpectContinue is removed in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
101+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
102+
methodPattern: com.amazonaws.ClientConfiguration withProtocol(.*)
103+
comment: protocol is deprecated and not supported in v2. Consider using endpointOverride to specify HTTP scheme.
104+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
105+
methodPattern: com.amazonaws.ClientConfiguration setProtocol(.*)
106+
comment: protocol is deprecated and not supported in v2. Consider using endpointOverride to specify HTTP scheme.
107+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
108+
methodPattern: com.amazonaws.ClientConfiguration withUserAgent(String)
109+
comment: userAgent override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).
110+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
111+
methodPattern: com.amazonaws.ClientConfiguration setUserAgent(String)
112+
comment: userAgent override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).
113+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
114+
methodPattern: com.amazonaws.ClientConfiguration withUserAgentPrefix(String)
115+
comment: userAgentPrefix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).
116+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
117+
methodPattern: com.amazonaws.ClientConfiguration setUserAgentPrefix(String)
118+
comment: userAgentPrefix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).
119+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
120+
methodPattern: com.amazonaws.ClientConfiguration withUserAgentSuffix(String)
121+
comment: userAgentSuffix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).
122+
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
123+
methodPattern: com.amazonaws.ClientConfiguration setUserAgentSuffix(String)
124+
comment: userAgentSuffix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).
125+
126+
## The following change needs to be the last step
127+
- org.openrewrite.java.ChangeType:
128+
oldFullyQualifiedTypeName: com.amazonaws.ClientConfiguration
129+
newFullyQualifiedTypeName: software.amazon.awssdk.core.client.config.ClientOverrideConfiguration
130+
- org.openrewrite.java.ChangeType:
131+
oldFullyQualifiedTypeName: com.amazonaws.retry.RetryMode
132+
newFullyQualifiedTypeName: software.amazon.awssdk.core.retry.RetryMode

migration-tool/src/main/resources/META-INF/rewrite/change-sdk-core-types.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ name: software.amazon.awssdk.ChangeSdkCoreTypes
1818
displayName: Change Maven dependency groupId, artifactId and/or the version example
1919
recipeList:
2020
- software.amazon.awssdk.ChangeRegionTypes
21-
- software.amazon.awssdk.ChangeAuthTypes
21+
- software.amazon.awssdk.ChangeAuthTypes
22+
- software.amazon.awssdk.ChangeConfigTypes

migration-tool/src/main/resources/META-INF/rewrite/java-sdk-v1-to-v2.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ recipeList:
2727
- software.amazon.awssdk.migration.internal.recipe.S3StreamingResponseToV2
2828
- software.amazon.awssdk.migration.recipe.ChangeSdkType
2929
- software.amazon.awssdk.ChangeSdkCoreTypes
30+
# At this point, all classes should be changed to v2 equivalents
3031
- software.amazon.awssdk.migration.recipe.V1BuilderVariationsToV2Builder
3132
- software.amazon.awssdk.migration.recipe.NewClassToBuilderPattern
3233
- software.amazon.awssdk.migration.recipe.NewClassToStaticFactory

0 commit comments

Comments
 (0)