Skip to content

Commit

Permalink
update default creds (#4684)
Browse files Browse the repository at this point in the history
* move logic of default func to configure

* move multiEnvSearch to utils
  • Loading branch information
megan07 authored Apr 12, 2021
1 parent 9756993 commit e7a19e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
20 changes: 12 additions & 8 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,12 @@ func Provider() *schema.Provider {
"credentials": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_CREDENTIALS",
"GOOGLE_CLOUD_KEYFILE_JSON",
"GCLOUD_KEYFILE_JSON",
}, nil),
ValidateFunc: validateCredentials,
},

"access_token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_OAUTH_ACCESS_TOKEN",
}, nil),
ConflictsWith: []string{"credentials"},
},
"impersonate_service_account": {
Expand Down Expand Up @@ -510,6 +502,18 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
return nil, diag.FromErr(err)
}
}

// Search for default credentials
config.Credentials = multiEnvSearch([]string{
"GOOGLE_CREDENTIALS",
"GOOGLE_CLOUD_KEYFILE_JSON",
"GCLOUD_KEYFILE_JSON",
})

config.AccessToken = multiEnvSearch([]string{
"GOOGLE_OAUTH_ACCESS_TOKEN",
})

// Add credential source
if v, ok := d.GetOk("access_token"); ok {
config.AccessToken = v.(string)
Expand Down
9 changes: 0 additions & 9 deletions mmv1/third_party/terraform/utils/provider_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -938,15 +938,6 @@ func getTestServiceAccountFromEnv(t *testing.T) string {
return multiEnvSearch(serviceAccountEnvVars)
}

func multiEnvSearch(ks []string) string {
for _, k := range ks {
if v := os.Getenv(k); v != "" {
return v
}
}
return ""
}

// Some tests fail during VCR. One common case is race conditions when creating resources.
// If a test config adds two fine-grained resources with the same parent it is undefined
// which will be created first, causing VCR to fail ~50% of the time
Expand Down
9 changes: 9 additions & 0 deletions mmv1/third_party/terraform/utils/utils.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,12 @@ func SnakeToPascalCase(s string) (string) {
}
return strings.Join(split, "")
}

func multiEnvSearch(ks []string) string {
for _, k := range ks {
if v := os.Getenv(k); v != "" {
return v
}
}
return ""
}

0 comments on commit e7a19e7

Please sign in to comment.