Skip to content

Commit

Permalink
Remove ConfigurationUpdater abstraction (#4552)
Browse files Browse the repository at this point in the history
  • Loading branch information
sugmanue authored Oct 6, 2023
1 parent 195bf79 commit e064ef0
Show file tree
Hide file tree
Showing 44 changed files with 3,751 additions and 3,770 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@
import software.amazon.awssdk.codegen.poet.model.ServiceClientConfigurationUtils;
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
import software.amazon.awssdk.codegen.utils.AuthUtils;
import software.amazon.awssdk.core.SdkServiceClientConfiguration;
import software.amazon.awssdk.core.SdkPlugin;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.config.internal.ConfigurationUpdater;
import software.amazon.awssdk.core.endpointdiscovery.providers.DefaultEndpointDiscoveryProviderChain;
import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
Expand Down Expand Up @@ -177,10 +176,9 @@ public TypeSpec poetSpec() {
builder.addMethod(defaultTokenAuthSignerMethod());
}
}
builder.addMethod(defaultConfigUpdaterMethod());
builder.addMethod(setOverridesMethod());
addServiceHttpConfigIfNeeded(builder, model);

builder.addMethod(invokePluginsMethod());
builder.addMethod(validateClientOptionsMethod());


Expand Down Expand Up @@ -733,19 +731,23 @@ private MethodSpec authSchemesMethod() {
return builder.build();
}

private MethodSpec defaultConfigUpdaterMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("defaultConfigurationUpdater")
private MethodSpec invokePluginsMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("invokePlugins")
.addAnnotation(Override.class)
.addModifiers(PROTECTED)
.returns(ParameterizedTypeName.get(ConfigurationUpdater.class,
SdkServiceClientConfiguration.Builder.class));

builder.addCode("return (consumer, configBuilder) -> {\n")
.addCode("$>")
.addStatement("$1T.BuilderInternal serviceConfigBuilder = $1T.builder(configBuilder)",
configurationUtils.serviceClientConfigurationBuilderClassName())
.addStatement("consumer.accept(serviceConfigBuilder)")
.addStatement("return serviceConfigBuilder.buildSdkClientConfiguration()")
.addCode("$<};\n");
.addParameter(SdkClientConfiguration.class, "config")
.returns(SdkClientConfiguration.class);
builder.addStatement("$T plugins = plugins()",
ParameterizedTypeName.get(List.class, SdkPlugin.class))
.beginControlFlow("if (plugins.isEmpty())")
.addStatement("return config")
.endControlFlow();
builder.addStatement("$1T.BuilderInternal serviceConfigBuilder = $1T.builder(config.toBuilder())",
configurationUtils.serviceClientConfigurationBuilderClassName());
builder.beginControlFlow("for ($T plugin : plugins)", SdkPlugin.class)
.addStatement("plugin.configureClient(serviceConfigBuilder)")
.endControlFlow();
builder.addStatement("return serviceConfigBuilder.buildSdkClientConfiguration()");
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.SdkPlugin;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.SdkServiceClientConfiguration;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.async.AsyncResponseTransformerUtils;
import software.amazon.awssdk.core.async.SdkPublisher;
import software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.config.internal.ConfigurationUpdater;
import software.amazon.awssdk.core.client.handler.AsyncClientHandler;
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRefreshCache;
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest;
Expand All @@ -101,7 +99,6 @@ public final class AsyncClientClass extends AsyncClientInterface {
private final ClassName className;
private final ProtocolSpec protocolSpec;
private final ClassName serviceClientConfigurationClassName;
private final ClassName sdkClientConfigurationUtilClassName;
private final ServiceClientConfigurationUtils configurationUtils;
private final boolean useSraAuth;

Expand All @@ -112,8 +109,6 @@ public AsyncClientClass(GeneratorTaskParams dependencies) {
this.className = poetExtensions.getClientClass(model.getMetadata().getAsyncClient());
this.protocolSpec = getProtocolSpecs(poetExtensions, model);
this.serviceClientConfigurationClassName = new PoetExtension(model).getServiceConfigClass();
this.sdkClientConfigurationUtilClassName = ClassName.get(model.getMetadata().getFullClientInternalPackageName(),
"SdkClientConfigurationUtil");
this.useSraAuth = new AuthSchemeSpecUtils(model).useSraAuth();
this.configurationUtils = new ServiceClientConfigurationUtils(model);
}
Expand Down Expand Up @@ -175,9 +170,7 @@ protected void addAdditionalMethods(TypeSpec.Builder type) {
type.addMethod(isSignerOverriddenOnClientMethod());
}
}
type.addMethod(updateSdkClientConfigurationMethod(
sdkClientConfigurationUtilClassName,
configurationUtils.serviceClientConfigurationBuilderClassName()));
type.addMethod(updateSdkClientConfigurationMethod(configurationUtils.serviceClientConfigurationBuilderClassName()));
protocolSpec.createErrorResponseHandler().ifPresent(type::addMethod);
}

Expand Down Expand Up @@ -291,26 +284,26 @@ protected MethodSpec serviceClientConfigMethod() {
}

protected static MethodSpec updateSdkClientConfigurationMethod(
ClassName sdkClientConfigurationUtilClassName,
TypeName serviceClientConfigurationBuilderClassName) {
MethodSpec.Builder builder = MethodSpec.methodBuilder("updateSdkClientConfiguration")
.addModifiers(PROTECTED)
.addModifiers(PRIVATE)
.addParameter(SdkRequest.class, "request")
.addParameter(SdkClientConfiguration.class, "clientConfiguration")
.returns(SdkClientConfiguration.class);
builder.addCode("$T configurationUpdater = ",
ParameterizedTypeName.get(ConfigurationUpdater.class, SdkServiceClientConfiguration.Builder.class));
builder.addCode("(consumer, configBuilder) -> {\n$>")
.addStatement("$1T.BuilderInternal serviceConfigBuilder = $1T.builder(configBuilder)",
serviceClientConfigurationBuilderClassName)
.addStatement("consumer.accept(serviceConfigBuilder)")
.addStatement("return serviceConfigBuilder.buildSdkClientConfiguration()")
.addCode("$<};\n");


builder.addStatement("$T plugins = request.overrideConfiguration()\n"
+ ".map(c -> c.plugins()).orElse(Collections.emptyList())",
ParameterizedTypeName.get(List.class, SdkPlugin.class));
builder.addStatement("return $T.invokePlugins(clientConfiguration, plugins, configurationUpdater)",
sdkClientConfigurationUtilClassName);
ParameterizedTypeName.get(List.class, SdkPlugin.class))
.beginControlFlow("if (plugins.isEmpty())")
.addStatement("return clientConfiguration")
.endControlFlow();
builder.addStatement("$1T.BuilderInternal serviceConfigBuilder = $1T.builder(clientConfiguration.toBuilder())",
serviceClientConfigurationBuilderClassName);
builder.beginControlFlow("for ($T plugin : plugins)", SdkPlugin.class)
.addStatement("plugin.configureClient(serviceConfigBuilder)")
.endControlFlow();
builder.addStatement("return serviceConfigBuilder.buildSdkClientConfiguration()");
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public class SyncClientClass extends SyncClientInterface {
private final ClassName className;
private final ProtocolSpec protocolSpec;
private final ClassName serviceClientConfigurationClassName;
private final ClassName sdkClientConfigurationUtilClassName;
private final ServiceClientConfigurationUtils configurationUtils;
private final boolean useSraAuth;

Expand All @@ -86,8 +85,6 @@ public SyncClientClass(GeneratorTaskParams taskParams) {
this.className = poetExtensions.getClientClass(model.getMetadata().getSyncClient());
this.protocolSpec = getProtocolSpecs(poetExtensions, model);
this.serviceClientConfigurationClassName = new PoetExtension(model).getServiceConfigClass();
this.sdkClientConfigurationUtilClassName = ClassName.get(model.getMetadata().getFullClientInternalPackageName(),
"SdkClientConfigurationUtil");
this.configurationUtils = new ServiceClientConfigurationUtils(model);
this.useSraAuth = new AuthSchemeSpecUtils(model).useSraAuth();
}
Expand Down Expand Up @@ -140,8 +137,7 @@ protected void addAdditionalMethods(TypeSpec.Builder type) {
.addMethod(resolveMetricPublishersMethod());

protocolSpec.createErrorResponseHandler().ifPresent(type::addMethod);
type.addMethod(updateSdkClientConfigurationMethod(sdkClientConfigurationUtilClassName,
configurationUtils.serviceClientConfigurationBuilderClassName()));
type.addMethod(updateSdkClientConfigurationMethod(configurationUtils.serviceClientConfigurationBuilderClassName()));
type.addMethod(protocolSpec.initProtocolFactory(model));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.config.internal.ConfigurationUpdater;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.internal.SdkInternalTestAdvancedClientOption;
Expand Down Expand Up @@ -130,21 +129,4 @@ public final class SdkClientConfigurationUtil {

return clientOverrides;
}

/**
* Executes the given list of plugins and returns the updated configuration.
*/
public static SdkClientConfiguration invokePlugins(SdkClientConfiguration clientConfiguration,
List<SdkPlugin> plugins,
ConfigurationUpdater<SdkServiceClientConfiguration.Builder> updater
) {
if (plugins.isEmpty()) {
return clientConfiguration;
}
return updater.update(builder -> {
for (SdkPlugin plugin : plugins) {
plugin.configureClient(builder);
}
}, clientConfiguration.toBuilder());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import software.amazon.awssdk.auth.token.credentials.aws.DefaultAwsTokenProvider;
import software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.core.SdkServiceClientConfiguration;
import software.amazon.awssdk.core.SdkPlugin;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.config.internal.ConfigurationUpdater;
import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.http.auth.scheme.BearerAuthScheme;
Expand Down Expand Up @@ -54,10 +53,10 @@ protected final String serviceName() {
@Override
protected final SdkClientConfiguration mergeServiceDefaults(SdkClientConfiguration config) {
return config.merge(c -> c.option(SdkClientOption.ENDPOINT_PROVIDER, defaultEndpointProvider())
.option(SdkClientOption.AUTH_SCHEME_PROVIDER, defaultAuthSchemeProvider())
.option(SdkClientOption.AUTH_SCHEMES, authSchemes())
.option(SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, false)
.option(AwsClientOption.TOKEN_IDENTITY_PROVIDER, defaultTokenProvider()));
.option(SdkClientOption.AUTH_SCHEME_PROVIDER, defaultAuthSchemeProvider())
.option(SdkClientOption.AUTH_SCHEMES, authSchemes())
.option(SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, false)
.option(AwsClientOption.TOKEN_IDENTITY_PROVIDER, defaultTokenProvider()));
}

@Override
Expand All @@ -78,7 +77,7 @@ protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientCon
if (identityProvider != null) {
IdentityProviders identityProviders = config.option(SdkClientOption.IDENTITY_PROVIDERS);
builder.option(SdkClientOption.IDENTITY_PROVIDERS, identityProviders.toBuilder()
.putIdentityProvider(identityProvider).build());
.putIdentityProvider(identityProvider).build());
}
builder.option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors);
return builder.build();
Expand Down Expand Up @@ -122,15 +121,6 @@ private IdentityProvider<? extends TokenIdentity> defaultTokenProvider() {
return DefaultAwsTokenProvider.create();
}

protected ConfigurationUpdater<SdkServiceClientConfiguration.Builder> defaultConfigurationUpdater() {
return (consumer, configBuilder) -> {
JsonServiceClientConfigurationBuilder.BuilderInternal serviceConfigBuilder = JsonServiceClientConfigurationBuilder
.builder(configBuilder);
consumer.accept(serviceConfigBuilder);
return serviceConfigBuilder.buildSdkClientConfiguration();
};
}

@Override
protected SdkClientConfiguration setOverrides(SdkClientConfiguration configuration) {
ClientOverrideConfiguration overrideConfiguration = overrideConfiguration();
Expand All @@ -140,6 +130,20 @@ protected SdkClientConfiguration setOverrides(SdkClientConfiguration configurati
return SdkClientConfigurationUtil.copyOverridesToConfiguration(overrideConfiguration, configuration.toBuilder()).build();
}

@Override
protected SdkClientConfiguration invokePlugins(SdkClientConfiguration config) {
List<SdkPlugin> plugins = plugins();
if (plugins.isEmpty()) {
return config;
}
JsonServiceClientConfigurationBuilder.BuilderInternal serviceConfigBuilder = JsonServiceClientConfigurationBuilder
.builder(config.toBuilder());
for (SdkPlugin plugin : plugins) {
plugin.configureClient(serviceConfigBuilder);
}
return serviceConfigBuilder.buildSdkClientConfiguration();
}

protected static void validateClientOptions(SdkClientConfiguration c) {
Validate.notNull(c.option(AwsClientOption.TOKEN_IDENTITY_PROVIDER),
"The 'tokenProvider' must be configured in the client builder.");
Expand Down
Loading

0 comments on commit e064ef0

Please sign in to comment.