Skip to content

Commit

Permalink
Merge pull request #207 from amhn/provider_connection_reuse
Browse files Browse the repository at this point in the history
fix: Enable http transport connection reuse
  • Loading branch information
smutel authored Aug 11, 2023
2 parents 92eb6a6 + 2fbd144 commit c1381e0
Showing 1 changed file with 4 additions and 39 deletions.
43 changes: 4 additions & 39 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package netbox

import (
"context"
"crypto/tls"
"fmt"
"net/http"

Expand Down Expand Up @@ -214,48 +213,14 @@ func configureProvider(ctx context.Context, d *schema.ResourceData) (interface{}
scheme := d.Get("scheme").(string)
insecure := d.Get("insecure").(bool)

var options runtimeclient.TLSClientOptions
options.InsecureSkipVerify = insecure
tlsConfig, _ := runtimeclient.TLSClientAuth(options)

headers := make(map[string]string)

// Create a custom client
// Override the default transport with a RoundTripper to inject dynamic headers
// Add TLSOptions
cli := &http.Client{
Transport: &transport{
headers: headers,
TLSClientConfig: tlsConfig,
},
}

defaultScheme := []string{scheme}

t := runtimeclient.NewWithClient(url, basepath, defaultScheme, cli)
t := runtimeclient.New(url, basepath, defaultScheme)
if insecure {
t.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = insecure
}
t.DefaultAuthentication = runtimeclient.APIKeyAuth(authHeaderName, "header",
fmt.Sprintf(authHeaderFormat, token))

return client.New(t, strfmt.Default), nil
}

type transport struct {
headers map[string]string
base http.RoundTripper
TLSClientConfig *tls.Config
}

func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
// Add headers to request
for k, v := range t.headers {
req.Header.Add(k, v)
}
base := t.base
if base == nil {
// init an http.Transport with TLSOptions
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = t.TLSClientConfig
base = customTransport
}
return base.RoundTrip(req)
}

0 comments on commit c1381e0

Please sign in to comment.