Skip to content

Commit

Permalink
Add mappping for non-HTTP config settings and add comments for settin…
Browse files Browse the repository at this point in the history
…gs (#5181)
  • Loading branch information
zoewangg committed May 7, 2024
1 parent 0734f68 commit 1b5f27e
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 45 deletions.
20 changes: 20 additions & 0 deletions migration-tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
</dependency>

<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-test</artifactId>
Expand Down Expand Up @@ -112,6 +117,21 @@
<artifactId>utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sdk-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>auth</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Used in UpgradeSdkDependenciesTest -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {

Visitor(String methodPattern, String comment) {
this.methodMatcher = new MethodMatcher(methodPattern, false);
this.comment = COMMENT_PREFIX + comment;
this.comment = COMMENT_PREFIX + comment + "\n";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

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

import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isEligibleToConvertToBuilder;
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ClientClass;
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ModelClass;

import java.util.Map;
import org.openrewrite.ExecutionContext;
Expand All @@ -37,10 +37,13 @@
* for generated model classes and client classes.
*
* @see NewClassToBuilderPattern
* TODO: separate model classes and client classes
*/
@SdkInternalApi
public class V1SetterToV2 extends Recipe {
private static final Map<String, String> CLIENT_CONFIG_NAMING_MAPPING =
// TODO: handle other settings on the builder such as withEndpointConfiguration,
// withMonitoringListener and withMetricsCollector
ImmutableMap.<String, String>builder()
.put("credentials", "credentialsProvider")
.put("clientConfiguration", "overrideConfiguration")
Expand Down Expand Up @@ -75,11 +78,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
selectType = select.getType();
}

if (selectType == null || !shouldChangeSetter(selectType)) {
if (selectType == null) {
return method;
}

String methodName = method.getSimpleName();
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType);

if (!shouldChangeSetter(fullyQualified)) {
return method;
}

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

JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType);

if (fullyQualified != null) {
mt = mt.withDeclaringType(fullyQualified);
}
Expand All @@ -112,8 +118,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return method;
}

private static boolean shouldChangeSetter(JavaType selectType) {
return isV2ModelClass(selectType) || isV2ClientClass(selectType);
private static boolean shouldChangeSetter(JavaType.FullyQualified selectType) {
return isEligibleToConvertToBuilder(selectType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProcessCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider;
import software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider;
import software.amazon.awssdk.utils.ImmutableMap;

/**
Expand All @@ -35,11 +48,11 @@ public final class SdkTypeUtils {
*/
public static final Map<String, Integer> V2_CORE_CLASSES_WITH_STATIC_FACTORY =
ImmutableMap.<String, Integer>builder()
.put("software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider", 0)
.put("software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider", 0)
.put("software.amazon.awssdk.auth.credentials.AwsBasicCredentials", 2)
.put("software.amazon.awssdk.auth.credentials.AwsSessionCredentials", 3)
.put("software.amazon.awssdk.auth.credentials.StaticCredentialsProvider", 1)
.put(EnvironmentVariableCredentialsProvider.class.getCanonicalName(), 0)
.put(InstanceProfileCredentialsProvider.class.getCanonicalName(), 0)
.put(AwsBasicCredentials.class.getCanonicalName(), 2)
.put(AwsSessionCredentials.class.getCanonicalName(), 3)
.put(StaticCredentialsProvider.class.getCanonicalName(), 1)
.build();

private static final Pattern V1_SERVICE_CLASS_PATTERN =
Expand All @@ -66,15 +79,15 @@ public final class SdkTypeUtils {
* V2 core classes with a builder
*/
private static final Set<String> V2_CORE_CLASSES_WITH_BUILDER =
new HashSet<>(Arrays.asList("software.amazon.awssdk.core.client.ClientOverrideConfiguration",
"software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider",
"software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider",
"software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider",
"software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider",
"software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider",
"software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider",
"software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider",
"software.amazon.awssdk.auth.credentials.ProcessCredentialsProvider"));
new HashSet<>(Arrays.asList(ClientOverrideConfiguration.class.getCanonicalName(),
DefaultCredentialsProvider.class.getCanonicalName(),
ProfileCredentialsProvider.class.getCanonicalName(),
ContainerCredentialsProvider.class.getCanonicalName(),
InstanceProfileCredentialsProvider.class.getCanonicalName(),
StsAssumeRoleCredentialsProvider.class.getCanonicalName(),
StsGetSessionTokenCredentialsProvider.class.getCanonicalName(),
StsAssumeRoleWithWebIdentityCredentialsProvider.class.getCanonicalName(),
ProcessCredentialsProvider.class.getCanonicalName()));

private static final Pattern V2_CLIENT_BUILDER_PATTERN = Pattern.compile(
"software\\.amazon\\.awssdk\\.services\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+Builder");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
## TODO: support retry policy, signer and throttledRetries
---
type: specs.openrewrite.org/v1beta/recipe
name: software.amazon.awssdk.ChangeConfigTypes
displayName: Change region related classes
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration withRequestTimeout(int)
newMethodName: withApiCallAttemptTimeout
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration setRequestTimeout(int)
newMethodName: withApiCallAttemptTimeout
- software.amazon.awssdk.migration.internal.recipe.NumberToDuration:
methodPattern: com.amazonaws.ClientConfiguration withApiCallAttemptTimeout(int)

- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration withClientExecutionTimeout(int)
newMethodName: withApiCallTimeout
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration setClientExecutionTimeout(int)
newMethodName: withApiCallTimeout
- software.amazon.awssdk.migration.internal.recipe.NumberToDuration:
methodPattern: com.amazonaws.ClientConfiguration withApiCallTimeout(int)

- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration withRetryMode(..)
newMethodName: withRetryPolicy
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration setRetryMode(..)
newMethodName: withRetryPolicy
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration withHeader(String, String)
newMethodName: withPutHeader
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.amazonaws.ClientConfiguration setHeader(String, String)
newMethodName: withPutHeader

## Add comment to unsupported options
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setMaxConsecutiveRetriesBeforeThrottling(int)
comment: maxConsecutiveRetriesBeforeThrottling is deprecated and not supported in v2. Consider removing it or using a custom RetryPolicy.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withMaxConsecutiveRetriesBeforeThrottling(int)
comment: maxConsecutiveRetriesBeforeThrottling is deprecated and not supported in v2. Consider removing it or using a custom RetryPolicy.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setCacheResponseMetadata(boolean)
comment: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withCacheResponseMetadata(boolean)
comment: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withDisableHostPrefixInjection(boolean)
comment: disableHostPrefixInjection is deprecated and not supported removed in v2. Consider removing it.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setDisableHostPrefixInjection(boolean)
comment: disableHostPrefixInjection is deprecated and not supported in v2. Consider removing it.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setDnsResolver(..)
comment: dnsResolver is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withDnsResolver(..)
comment: dnsResolver is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setGzip(boolean)
comment: gzip is not supported in v2 tracking in https://github.com/aws/aws-sdk-java-v2/issues/866. Consider removing it.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withGzip(boolean)
comment: gzip is not supported in v2 tracking in https://github.com/aws/aws-sdk-java-v2/issues/866. Consider removing it.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setLocalAddress(..)
comment: localAddress is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withLocalAddress(..)
comment: localAddress is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setSecureRandom(.*)
comment: secureRandom is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withSecureRandom(.*)
comment: secureRandom is supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setUseExpectContinue(boolean)
comment: useExpectContinue is removed in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withUseExpectContinue(boolean)
comment: useExpectContinue is removed in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withProtocol(.*)
comment: protocol is deprecated and not supported in v2. Consider using endpointOverride to specify HTTP scheme.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setProtocol(.*)
comment: protocol is deprecated and not supported in v2. Consider using endpointOverride to specify HTTP scheme.
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withUserAgent(String)
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).
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setUserAgent(String)
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).
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withUserAgentPrefix(String)
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).
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setUserAgentPrefix(String)
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).
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration withUserAgentSuffix(String)
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).
- software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod:
methodPattern: com.amazonaws.ClientConfiguration setUserAgentSuffix(String)
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).

## The following change needs to be the last step
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: com.amazonaws.ClientConfiguration
newFullyQualifiedTypeName: software.amazon.awssdk.core.client.config.ClientOverrideConfiguration
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: com.amazonaws.retry.RetryMode
newFullyQualifiedTypeName: software.amazon.awssdk.core.retry.RetryMode
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ name: software.amazon.awssdk.ChangeSdkCoreTypes
displayName: Change Maven dependency groupId, artifactId and/or the version example
recipeList:
- software.amazon.awssdk.ChangeRegionTypes
- software.amazon.awssdk.ChangeAuthTypes
- software.amazon.awssdk.ChangeAuthTypes
- software.amazon.awssdk.ChangeConfigTypes
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ recipeList:
- software.amazon.awssdk.migration.internal.recipe.S3StreamingResponseToV2
- software.amazon.awssdk.migration.recipe.ChangeSdkType
- software.amazon.awssdk.ChangeSdkCoreTypes
# At this point, all classes should be changed to v2 equivalents
- software.amazon.awssdk.migration.recipe.V1BuilderVariationsToV2Builder
- software.amazon.awssdk.migration.recipe.NewClassToBuilderPattern
- software.amazon.awssdk.migration.recipe.NewClassToStaticFactory
Loading

0 comments on commit 1b5f27e

Please sign in to comment.