Skip to content

Commit

Permalink
Adds SDK v1 endpoint resolver generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Jun 20, 2024
1 parent c3da0dd commit 8c406c2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
78 changes: 74 additions & 4 deletions internal/generate/servicepackage/endpoint_resolver.go.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,93 @@ package {{ .ProviderPackage }}

import (
"context"
{{- if and .GenerateClient .ClientSDKV2 }}
{{- if .GenerateClient }}
"fmt"
"net"
{{ if .ClientSDKV1 -}}
"net/url"
{{ end }}
{{ end }}

{{ if .GenerateClient }}
{{- if .ClientSDKV2 }}
{{- if .ClientSDKV1 }}
endpoints_sdkv1 "github.com/aws/aws-sdk-go/aws/endpoints"
{{- end }}
{{- if .ClientSDKV2 }}
aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws"
{{ .GoV2Package }}_sdkv2 "github.com/aws/aws-sdk-go-v2/service/{{ .GoV2Package }}"
smithyendpoints "github.com/aws/smithy-go/endpoints"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
{{- end }}
{{- end }}
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
{{- end }}
)

{{- if .GenerateClient }}
{{ if .ClientSDKV1 }}
var _ endpoints_sdkv1.Resolver = resolverSDKv1{}

type resolverSDKv1 struct {
ctx context.Context
}

func newEndpointResolverSDKv1(ctx context.Context) resolverSDKv1 {
return resolverSDKv1{
ctx: ctx,
}
}

func (r resolverSDKv1) EndpointFor(service, region string, opts ...func(*endpoints_sdkv1.Options)) (endpoint endpoints_sdkv1.ResolvedEndpoint, err error) {
ctx := r.ctx

var opt endpoints_sdkv1.Options
opt.Set(opts...)

useFIPS := opt.UseFIPSEndpoint == endpoints_sdkv1.FIPSEndpointStateEnabled

defaultResolver := endpoints_sdkv1.DefaultResolver()

if useFIPS {
ctx = tflog.SetField(ctx, "tf_aws.use_fips", useFIPS)

endpoint, err = defaultResolver.EndpointFor(service, region, opts...)
if err != nil {
return endpoint, err
}

tflog.Debug(ctx, "endpoint resolved", map[string]any{
"tf_aws.endpoint": endpoint.URL,
})

var endpointURL *url.URL
endpointURL, err = url.Parse(endpoint.URL)
if err != nil {
return endpoint, err
}

hostname := endpointURL.Hostname()
_, err = net.LookupHost(hostname)
if err != nil {
if dnsErr, ok := errs.As[*net.DNSError](err); ok && dnsErr.IsNotFound {
tflog.Debug(ctx, "default endpoint host not found, disabling FIPS", map[string]any{
"tf_aws.hostname": hostname,
})
opts = append(opts, func(o *endpoints_sdkv1.Options) {
o.UseFIPSEndpoint = endpoints_sdkv1.FIPSEndpointStateDisabled
})
} else {
err = fmt.Errorf("looking up accessanalyzer endpoint %q: %s", hostname, err)
return
}
} else {
return endpoint, err
}
}

return defaultResolver.EndpointFor(service, region, opts...)
}
{{ end }}

{{ if .ClientSDKV2 }}
var _ {{ .GoV2Package }}_sdkv2.EndpointResolverV2 = resolverSDKv2{}

Expand Down
8 changes: 2 additions & 6 deletions internal/generate/servicepackage/file.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
{{ if .GenerateClient }}
{{- if .ClientSDKV1 }}
aws_sdkv1 "github.com/aws/aws-sdk-go/aws"
endpoints_sdkv1 "github.com/aws/aws-sdk-go/aws/endpoints"
session_sdkv1 "github.com/aws/aws-sdk-go/aws/session"
{{ .GoV1Package }}_sdkv1 "github.com/aws/aws-sdk-go/service/{{ .GoV1Package }}"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -142,11 +141,8 @@ func (p *servicePackage) NewConn(ctx context.Context, config map[string]any) (*{
"tf_aws.endpoint": endpoint,
})
cfg.Endpoint = aws_sdkv1.String(endpoint)

if sess.Config.UseFIPSEndpoint == endpoints_sdkv1.FIPSEndpointStateEnabled {
tflog.Debug(ctx, "endpoint set, ignoring UseFIPSEndpoint setting")
cfg.UseFIPSEndpoint = endpoints_sdkv1.FIPSEndpointStateDisabled
}
} else {
cfg.EndpointResolver = newEndpointResolverSDKv1(ctx)
}

return {{ .GoV1Package }}_sdkv1.New(sess.Copy(&cfg)), nil
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/servicepackage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func main() {
g.Fatalf("generating file (%s): %s", filename, err)
}

if s.GenerateClient && s.ClientSDKV2 {
if s.GenerateClient {
g.Infof("Generating internal/service/%s/%s", servicePackage, endpointResolverFilenamne)

d = g.NewGoFileDestination(endpointResolverFilenamne)
Expand Down

0 comments on commit 8c406c2

Please sign in to comment.