From 4c4fb68460027405aec0087e41a065b0caa6d6bc Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Tue, 21 Feb 2023 20:45:43 -0500 Subject: [PATCH] introduce PACKET_API_URL env variable Just as PACKET_AUTH_TOKEN can be used today to set an authentication token, PACKET_API_URL is being added to allow developers and users to set custom API URL targets. Some applications may already provide overrides via configuration files and environment. This environment variable will provide flexibility where applications did offer this. The existing default value has been preserved for NewClient, NewClientWithAuth, and NewClientWithBaseURL. The former can now take advatange of PACKET_API_URL. Signed-off-by: Marques Johansson --- metadata/metadata_test.go | 2 -- packngo.go | 14 +++++++++++--- packngo_test.go | 7 ++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index d841c88d..25eb24e1 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -4,7 +4,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "testing" "github.com/stretchr/testify/assert" @@ -48,5 +47,4 @@ func Test_Deserialization(t *testing.T) { assert.Equal(t, "10.144.51.11", volumes[0].IPs[1].String()) assert.Equal(t, 10, volumes[0].Capacity.Size) assert.Equal(t, "gb", volumes[0].Capacity.Unit) - } diff --git a/packngo.go b/packngo.go index 25c9c331..16e30ec6 100644 --- a/packngo.go +++ b/packngo.go @@ -20,7 +20,8 @@ import ( const ( authTokenEnvVar = "PACKET_AUTH_TOKEN" - baseURL = "https://api.equinix.com/metal/v1/" + urlEnvVar = "PACKET_API_URL" + defaultURL = "https://api.equinix.com/metal/v1/" mediaType = "application/json" debugEnvVar = "PACKNGO_DEBUG" @@ -339,7 +340,10 @@ func (c *Client) DoRequestWithHeader(method string, headers map[string]string, p // an older version of Go, pass in a custom http.Client with a custom TLS configuration // that sets "InsecureSkipVerify" to "true" func NewClientWithAuth(consumerToken string, apiKey string, httpClient *http.Client) *Client { - client, _ := NewClientWithBaseURL(consumerToken, apiKey, httpClient, baseURL) + client, err := NewClient(WithAuth(consumerToken, apiKey), WithHTTPClient(httpClient)) + if err != nil { + log.Printf("WARNING: NewClientWithAuth error %q suppressed", err) + } return client } @@ -376,7 +380,11 @@ func NewClient(opts ...ClientOpt) (*Client, error) { c.header.Set("Accept", mediaType) var err error - c.BaseURL, err = url.Parse(baseURL) + apiURL := os.Getenv(urlEnvVar) + if apiURL == "" { + apiURL = defaultURL + } + c.BaseURL, err = url.Parse(apiURL) if err != nil { return nil, err } diff --git a/packngo_test.go b/packngo_test.go index a916ac38..9922726f 100644 --- a/packngo_test.go +++ b/packngo_test.go @@ -19,7 +19,6 @@ import ( ) const ( - apiURLEnvVar = "PACKET_API_URL" packngoAccTestVar = "PACKNGO_TEST_ACTUAL_API" testProjectPrefix = "PACKNGO_TEST_DELME_2d768716_" testFacilityVar = "PACKNGO_TEST_FACILITY" @@ -98,7 +97,6 @@ func setupWithProject(t *testing.T) (*Client, string, func()) { } stopRecord() } - } func skipUnlessAcceptanceTestsAllowed(t *testing.T) { @@ -156,9 +154,9 @@ func setup(t *testing.T) (*Client, func()) { if err != nil { t.Fatal(err) } - apiURL := os.Getenv(apiURLEnvVar) + apiURL := os.Getenv(urlEnvVar) if apiURL == "" { - apiURL = baseURL + apiURL = defaultURL } r, stopRecord := testRecorder(t, name, mode) httpClient := http.DefaultClient @@ -216,7 +214,6 @@ func TestAccInvalidCredentials(t *testing.T) { if !matched { t.Fatalf("Unexpected error string: %s", expectedErr) } - } func Test_dumpDeprecation(t *testing.T) {