move http with proxy call into a helper function
This commit is contained in:
parent
904c4b9adb
commit
c63b54a1e7
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
commonhelper "github.com/hashicorp/packer/helper/common"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
@ -28,11 +29,7 @@ func (s *stepSetISO) Run(_ context.Context, state multistep.StateBag) multistep.
|
|||
|
||||
req.Header.Set("User-Agent", "Packer")
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
}
|
||||
httpClient := commonhelper.HttpClientWithEnvironmentProxy()
|
||||
|
||||
res, err := httpClient.Do(req)
|
||||
if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) {
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
"runtime"
|
||||
"strings"
|
||||
|
||||
commonhelper "github.com/hashicorp/packer/helper/common"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
|
@ -257,11 +258,7 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
|
|||
req.Header.Set("User-Agent", d.userAgent)
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
}
|
||||
httpClient := commonhelper.HttpClientWithEnvironmentProxy()
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil || resp == nil {
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -13,6 +12,7 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
commonhelper "github.com/hashicorp/packer/helper/common"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
|
@ -258,11 +258,7 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
}
|
||||
httpClient := commonhelper.HttpClientWithEnvironmentProxy()
|
||||
|
||||
_, err = httpClient.Do(req)
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func HttpClientWithEnvironmentProxy() *http.Client {
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
}
|
||||
return httpClient
|
||||
}
|
|
@ -9,6 +9,8 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
commonhelper "github.com/hashicorp/packer/helper/common"
|
||||
)
|
||||
|
||||
type VagrantCloudClient struct {
|
||||
|
@ -37,11 +39,7 @@ func (v VagrantCloudErrors) FormatErrors() string {
|
|||
|
||||
func (v VagrantCloudClient) New(baseUrl string, token string) (*VagrantCloudClient, error) {
|
||||
c := &VagrantCloudClient{
|
||||
client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
},
|
||||
client: commonhelper.HttpClientWithEnvironmentProxy(),
|
||||
BaseURL: baseUrl,
|
||||
AccessToken: token,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue