Skip to content

Commit

Permalink
Merge pull request #46 from hashicorp/atlas-address-env-var
Browse files Browse the repository at this point in the history
Set Atlas endpoint to ATLAS_ADDRESS if present
  • Loading branch information
justincampbell committed Jan 12, 2016
2 parents b66e377 + fca43fa commit 0008886
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 11 additions & 2 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import (
)

const (
// atlasEndpoint is the default base URL for connecting to Atlas.
atlasEndpoint = "https://atlas.hashicorp.com"
// atlasDefaultEndpoint is the default base URL for connecting to Atlas.
atlasDefaultEndpoint = "https://atlas.hashicorp.com"

// atlasEndpointEnvVar is the environment variable that overrrides the
// default Atlas address.
atlasEndpointEnvVar = "ATLAS_ADDRESS"

// atlasTokenHeader is the header key used for authenticating with Atlas
atlasTokenHeader = "X-Atlas-Token"
Expand Down Expand Up @@ -60,6 +64,11 @@ type Client struct {

// DefaultClient returns a client that connects to the Atlas API.
func DefaultClient() *Client {
atlasEndpoint := os.Getenv(atlasEndpointEnvVar)
if atlasEndpoint == "" {
atlasEndpoint = atlasDefaultEndpoint
}

client, err := NewClient(atlasEndpoint)
if err != nil {
panic(err)
Expand Down
21 changes: 19 additions & 2 deletions v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package atlas

import (
"net/url"
"os"
"reflect"
"strings"
"testing"
Expand All @@ -10,8 +11,24 @@ import (
func TestDefaultClient_url(t *testing.T) {
client := DefaultClient()

if client.URL.String() != atlasEndpoint {
t.Fatalf("expected %q to be %q", client.URL.String(), atlasEndpoint)
if client.URL.String() != atlasDefaultEndpoint {
t.Fatalf("expected %q to be %q", client.URL.String(), atlasDefaultEndpoint)
}
}

func TestDefaultClient_urlFromEnvVar(t *testing.T) {
defer os.Setenv(atlasEndpointEnvVar, os.Getenv(atlasEndpointEnvVar))
otherEndpoint := "http://127.0.0.1:1234"

err := os.Setenv(atlasEndpointEnvVar, otherEndpoint)
if err != nil {
t.Fatal(err)
}

client := DefaultClient()

if client.URL.String() != otherEndpoint {
t.Fatalf("expected %q to be %q", client.URL.String(), otherEndpoint)
}
}

Expand Down

0 comments on commit 0008886

Please # to comment.