Skip to content
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

Added fallback behavior to custom v1 resolvers #2206

Merged
merged 7 commits into from
Jul 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ public class AddAwsConfigFields implements GoIntegration {
.documentation("API stack mutators")
.generatedOnClient(false)
.build(),
AwsConfigField.builder()
.name(ENDPOINT_RESOLVER_CONFIG_NAME)
.type(getAwsCoreSymbol("EndpointResolver"))
.generatedOnClient(false)
.awsResolveFunction(SymbolUtils.createValueSymbolBuilder(RESOLVE_AWS_CONFIG_ENDPOINT_RESOLVER)
.build())
.build(),
AwsConfigField.builder()
.name(LOGGER_CONFIG_NAME)
.type(getAwsCoreSymbol("Logger"))
Expand Down Expand Up @@ -255,6 +262,7 @@ private void writeAwsDefaultResolvers(GoWriter writer) {
writeHttpClientResolver(writer);
writeRetryerResolvers(writer);
writeRetryMaxAttemptsFinalizeResolver(writer);
writeAwsConfigEndpointResolver(writer);
}

private void writerAwsDefaultResolversTests(GoWriter writer) {
Expand Down Expand Up @@ -573,6 +581,24 @@ private void writeHttpClientResolver(GoWriter writer) {
writer.popState();
}

private void writeAwsConfigEndpointResolver(GoWriter writer) {
writer.pushState();
writer.putContext("resolverName", RESOLVE_AWS_CONFIG_ENDPOINT_RESOLVER);
writer.putContext("clientOption", ENDPOINT_RESOLVER_CONFIG_NAME);
writer.putContext("wrapperHelper", EndpointGenerator.AWS_ENDPOINT_RESOLVER_HELPER);
writer.putContext("awsResolver", ENDPOINT_RESOLVER_CONFIG_NAME);
writer.putContext("awsResolverWithOptions", AWS_ENDPOINT_RESOLVER_WITH_OPTIONS);
writer.write("""
func $resolverName:L(cfg aws.Config, o *Options) {
if cfg.$awsResolver:L == nil && cfg.$awsResolverWithOptions:L == nil {
return
}
o.$clientOption:L = $wrapperHelper:L(cfg.$awsResolver:L, cfg.$awsResolverWithOptions:L)
}
""");
writer.popState();
}

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
List<RuntimeClientPlugin> plugins = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,33 +394,13 @@ private void generateAwsEndpointResolverWrapper(GoWriter writer) {
writer.write("""
type $T struct {
awsResolver $T
resolver $T
}
""", wrappedResolverSymbol, endpointResolverWithOptions, resolverInterface);
""", wrappedResolverSymbol, endpointResolverWithOptions);
var endpointNotFoundError = SymbolUtils.createValueSymbolBuilder("EndpointNotFoundError",
AwsGoDependency.AWS_CORE).build();

writeExternalResolveEndpointImplementation(writer, wrappedResolverSymbol, "w", () -> {
var endpointNotFoundError = SymbolUtils.createValueSymbolBuilder("EndpointNotFoundError",
AwsGoDependency.AWS_CORE).build();
var errorf = SymbolUtils.createValueSymbolBuilder("Errorf",
SmithyGoDependency.FMT).build();
writer.write("""
if w.awsResolver == nil {
goto fallback
}
endpoint, err = w.awsResolver.ResolveEndpoint(ServiceID, region, options)
if err == nil {
return endpoint, nil
}

if nf := (&$T{}); !errors.As(err, &nf) {
return endpoint, err
}

fallback:
if w.resolver == nil {
return endpoint, $T("default endpoint resolver provided was nil")
}
return w.resolver.ResolveEndpoint(region, options)""", endpointNotFoundError, errorf);
writer.write("return w.awsResolver.ResolveEndpoint(ServiceID, region, options)");

writer.addUseImports(SmithyGoDependency.ERRORS);
});
Expand All @@ -441,28 +421,31 @@ private void generateAwsEndpointResolverWrapper(GoWriter writer) {
// Generate exported helper for constructing a wrapper around the AWS EndpointResolver type that is compatible
// with the clients EndpointResolver interface.
writer.write("""
// $L returns an EndpointResolver that first delegates endpoint resolution to the awsResolver.
// If awsResolver returns aws.EndpointNotFoundError error, the resolver will use the the provided
// fallbackResolver for resolution.
// $1L returns an $3T that first delegates endpoint resolution to the awsResolver.
// If awsResolver returns $7T error, the v1 resolver middleware will swallow the error,
// and set an appropriate context flag such that fallback will occur when $8L is invoked
// via its middleware.
//
// fallbackResolver must not be nil
func $L(awsResolver $T, awsResolverWithOptions $T, fallbackResolver $T) $T {
var resolver $T
// If another error (besides $7T) is returned, then that error will be propagated.
func $1L(awsResolver $2T, awsResolverWithOptions $3T) $6L {
var resolver $3T

if awsResolverWithOptions != nil {
resolver = awsResolverWithOptions
} else if awsResolver != nil {
resolver = $L(awsResolver.ResolveEndpoint)
resolver = $4L(awsResolver.ResolveEndpoint)
}

return &$T{
return &$5T{
awsResolver: resolver,
resolver: fallbackResolver,
}
}
""", AWS_ENDPOINT_RESOLVER_HELPER, AWS_ENDPOINT_RESOLVER_HELPER,
endpointResolver, endpointResolverWithOptions, resolverInterface,
resolverInterface, endpointResolverWithOptions, AWS_ENDPOINT_RESOLVER_ADAPTOR, wrappedResolverSymbol);
""", AWS_ENDPOINT_RESOLVER_HELPER,
endpointResolver, endpointResolverWithOptions,
AWS_ENDPOINT_RESOLVER_ADAPTOR, wrappedResolverSymbol,
resolverInterface, endpointNotFoundError,
EndpointResolutionGenerator.RESOLVER_INTERFACE_NAME
);
}

private void generateMiddleware(GoWriter writer) {
Expand Down Expand Up @@ -532,9 +515,22 @@ private void generateMiddlewareResolverBody(GoStackStepMiddlewareGenerator g, Go
w.write("var endpoint $T", SymbolUtils.createValueSymbolBuilder("Endpoint", AwsGoDependency.AWS_CORE)
.build());
w.write("endpoint, err = m.Resolver.ResolveEndpoint(awsmiddleware.GetRegion(ctx), eo)");
w.openBlock("if err != nil {", "}", () -> {
w.write("return out, metadata, fmt.Errorf(\"failed to resolve service endpoint, %w\", err)");
});
w.write(
"""
if err != nil {
nf := (&$1T{})
if $2T(err, &nf) {
ctx = $3T(ctx, false)
return next.HandleSerialize(ctx, in)
}
return out, metadata, $4T(\"failed to resolve service endpoint, %w\", err)
}
""",
SymbolUtils.createValueSymbolBuilder("EndpointNotFoundError", AwsGoDependency.AWS_CORE).build(),
SymbolUtils.createValueSymbolBuilder("As", SmithyGoDependency.ERRORS).build(),
SymbolUtils.createValueSymbolBuilder("SetRequiresLegacyEndpoints", AwsGoDependency.AWS_MIDDLEWARE).build(),
SymbolUtils.createValueSymbolBuilder("Errorf", SmithyGoDependency.FMT).build()
);
w.write("");

w.write("req.URL, err = url.Parse(endpoint.URL)");
Expand Down Expand Up @@ -625,7 +621,6 @@ private void generatePublicResolverTypes(GoWriter writer) {
writer.write("return fn(region, options)");
});


// Generate EndpointResolverFromURL helper
writer.writeDocs(String.format("%s returns an EndpointResolver configured using the provided endpoint url. "
+ "By default, the resolved endpoint resolver uses the client region as signing region, and "
Expand Down
8 changes: 8 additions & 0 deletions service/s3/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 12 additions & 24 deletions service/s3/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions service/s3/internal/customizations/update_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,18 @@ func TestVPC_CustomEndpoint(t *testing.T) {
expectedSigningName: "s3",
expectedSigningRegion: "us-west-2",
},
"custom resolver to v2 fallback": {
bucket: "bucketname",
options: s3.Options{
EndpointResolver: EndpointResolverFunc(func(region string, options s3.EndpointResolverOptions) (aws.Endpoint, error) {
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
}),
Region: "us-west-2",
},
expectedReqURL: "https://bucketname.s3.us-west-2.amazonaws.com/",
expectedSigningName: "s3",
expectedSigningRegion: "us-west-2",
},
"AccessPoint with custom endpoint url": {
bucket: "arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint",
options: s3.Options{
Expand Down
8 changes: 8 additions & 0 deletions service/s3control/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 12 additions & 24 deletions service/s3control/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions service/s3outposts/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading