From c63b54a1e7a464e9f4f9a0155439a404efd13c92 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 23 Jan 2019 16:45:07 -0800 Subject: [PATCH] move http with proxy call into a helper function --- builder/qemu/step_set_iso.go | 7 ++----- common/download.go | 7 ++----- common/download_test.go | 8 ++------ helper/common/default_client.go | 14 ++++++++++++++ post-processor/vagrant-cloud/client.go | 8 +++----- 5 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 helper/common/default_client.go diff --git a/builder/qemu/step_set_iso.go b/builder/qemu/step_set_iso.go index dba34777f..f2a89e10a 100644 --- a/builder/qemu/step_set_iso.go +++ b/builder/qemu/step_set_iso.go @@ -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) { diff --git a/common/download.go b/common/download.go index a5b217a62..a6248407b 100644 --- a/common/download.go +++ b/common/download.go @@ -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 { diff --git a/common/download_test.go b/common/download_test.go index be4382393..7a0792c3d 100644 --- a/common/download_test.go +++ b/common/download_test.go @@ -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 { diff --git a/helper/common/default_client.go b/helper/common/default_client.go new file mode 100644 index 000000000..a9829568d --- /dev/null +++ b/helper/common/default_client.go @@ -0,0 +1,14 @@ +package common + +import ( + "net/http" +) + +func HttpClientWithEnvironmentProxy() *http.Client { + httpClient := &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + }, + } + return httpClient +} diff --git a/post-processor/vagrant-cloud/client.go b/post-processor/vagrant-cloud/client.go index 318427dbf..bbea6b245 100644 --- a/post-processor/vagrant-cloud/client.go +++ b/post-processor/vagrant-cloud/client.go @@ -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, }