diff --git a/remote/atlas.go b/remote/atlas.go index e2afe3cab..6d7e22164 100644 --- a/remote/atlas.go +++ b/remote/atlas.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "os" "path" "strings" ) @@ -48,8 +49,13 @@ func (c *AtlasRemoteClient) validateConfig(conf map[string]string) error { c.serverURL = url token, ok := conf["access_token"] + if token == "" { + token = os.Getenv("ATLAS_TOKEN") + ok = true + } if !ok || token == "" { - return fmt.Errorf("missing 'access_token' configuration") + return fmt.Errorf( + "missing 'access_token' configuration or ATLAS_TOKEN environmental variable") } c.accessToken = token diff --git a/remote/atlas_test.go b/remote/atlas_test.go index 6f86e1354..e492acc97 100644 --- a/remote/atlas_test.go +++ b/remote/atlas_test.go @@ -35,6 +35,21 @@ func TestAtlasRemote_Validate(t *testing.T) { } } +func TestAtlasRemote_Validate_envVar(t *testing.T) { + conf := map[string]string{} + if _, err := NewAtlasRemoteClient(conf); err == nil { + t.Fatalf("expect error") + } + + defer os.Setenv("ATLAS_TOKEN", os.Getenv("ATLAS_TOKEN")) + os.Setenv("ATLAS_TOKEN", "foo") + + conf["name"] = "hashicorp/test-state" + if _, err := NewAtlasRemoteClient(conf); err != nil { + t.Fatalf("err: %v", err) + } +} + func TestAtlasRemote(t *testing.T) { checkAtlas(t) remote := &terraform.RemoteState{