Skip to content

Commit 792fa31

Browse files
authored
Merge pull request #832 from terraform-providers/paddy_creds_file_or_path
Fix our provider validation to allow file paths.
2 parents 2829f10 + f18607b commit 792fa31

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

google/provider.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package google
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

78
"github.com/hashicorp/terraform/helper/mutexkv"
89
"github.com/hashicorp/terraform/helper/schema"
@@ -199,6 +200,10 @@ func validateCredentials(v interface{}, k string) (warnings []string, errors []e
199200
return
200201
}
201202
creds := v.(string)
203+
// if this is a path and we can stat it, assume it's ok
204+
if _, err := os.Stat(creds); err == nil {
205+
return
206+
}
202207
var account accountFile
203208
if err := json.Unmarshal([]byte(creds), &account); err != nil {
204209
errors = append(errors,

google/provider_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ func TestProvider_getRegionFromZone(t *testing.T) {
8888
}
8989
}
9090

91+
func TestProvider_loadCredentialsFromFile(t *testing.T) {
92+
ws, es := validateCredentials(testFakeCredentialsPath, "")
93+
if len(ws) != 0 {
94+
t.Errorf("Expected %d warnings, got %v", len(ws), ws)
95+
}
96+
if len(es) != 0 {
97+
t.Errorf("Expected %d errors, got %v", len(es), es)
98+
}
99+
}
100+
101+
func TestProvider_loadCredentialsFromJSON(t *testing.T) {
102+
contents, err := ioutil.ReadFile(testFakeCredentialsPath)
103+
if err != nil {
104+
t.Fatalf("Unexpected error: %s", err)
105+
}
106+
ws, es := validateCredentials(string(contents), "")
107+
if len(ws) != 0 {
108+
t.Errorf("Expected %d warnings, got %v", len(ws), ws)
109+
}
110+
if len(es) != 0 {
111+
t.Errorf("Expected %d errors, got %v", len(es), es)
112+
}
113+
}
114+
91115
// getTestRegion has the same logic as the provider's getRegion, to be used in tests.
92116
func getTestRegion(is *terraform.InstanceState, config *Config) (string, error) {
93117
if res, ok := is.Attributes["region"]; ok {

0 commit comments

Comments
 (0)