From e7a19e7afc98130e95a4573559ced7a9c41bd9a3 Mon Sep 17 00:00:00 2001 From: megan07 Date: Mon, 12 Apr 2021 12:15:50 -0500 Subject: [PATCH] update default creds (#4684) * move logic of default func to configure * move multiEnvSearch to utils --- .../terraform/utils/provider.go.erb | 20 +++++++++++-------- .../terraform/utils/provider_test.go.erb | 9 --------- mmv1/third_party/terraform/utils/utils.go.erb | 9 +++++++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index 1719a28cb76c..f17b08cc3b72 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -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": { @@ -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) diff --git a/mmv1/third_party/terraform/utils/provider_test.go.erb b/mmv1/third_party/terraform/utils/provider_test.go.erb index 4d7e4ee7829d..8cad4c85d654 100644 --- a/mmv1/third_party/terraform/utils/provider_test.go.erb +++ b/mmv1/third_party/terraform/utils/provider_test.go.erb @@ -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 diff --git a/mmv1/third_party/terraform/utils/utils.go.erb b/mmv1/third_party/terraform/utils/utils.go.erb index e73f6abba23e..b810c491b4e1 100644 --- a/mmv1/third_party/terraform/utils/utils.go.erb +++ b/mmv1/third_party/terraform/utils/utils.go.erb @@ -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 "" +}