Skip to content

Add mappping for non-HTTP config settings #5181

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +45 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the other settings included in the scope for the preview version, or will they come later? Is there an approximate estimate for the size?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will not be included in the preview version. It's not trivial to implement them, probably x-large since we don't have anything similar implemented at the moment. It may become easier as we go. We can prioritize them based on feedback when we get to it.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

- 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 @@ -25,6 +25,7 @@ recipeList:
- software.amazon.awssdk.UpgradeSdkDependencies
- 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