Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix our provider validation to allow file paths. #832

Merged
merged 1 commit into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"encoding/json"
"fmt"
"os"

"github.com/hashicorp/terraform/helper/mutexkv"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -198,6 +199,10 @@ func validateCredentials(v interface{}, k string) (warnings []string, errors []e
return
}
creds := v.(string)
// if this is a path and we can stat it, assume it's ok
if _, err := os.Stat(creds); err == nil {
return
}
var account accountFile
if err := json.Unmarshal([]byte(creds), &account); err != nil {
errors = append(errors,
Expand Down
24 changes: 24 additions & 0 deletions google/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ func TestProvider_getRegionFromZone(t *testing.T) {
}
}

func TestProvider_loadCredentialsFromFile(t *testing.T) {
ws, es := validateCredentials(testFakeCredentialsPath, "")
if len(ws) != 0 {
t.Errorf("Expected %d warnings, got %v", len(ws), ws)
}
if len(es) != 0 {
t.Errorf("Expected %d errors, got %v", len(es), es)
}
}

func TestProvider_loadCredentialsFromJSON(t *testing.T) {
contents, err := ioutil.ReadFile(testFakeCredentialsPath)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
ws, es := validateCredentials(string(contents), "")
if len(ws) != 0 {
t.Errorf("Expected %d warnings, got %v", len(ws), ws)
}
if len(es) != 0 {
t.Errorf("Expected %d errors, got %v", len(es), es)
}
}

// getTestRegion has the same logic as the provider's getRegion, to be used in tests.
func getTestRegion(is *terraform.InstanceState, config *Config) (string, error) {
if res, ok := is.Attributes["region"]; ok {
Expand Down