From eef7f6549fd1aaba69c52820835e435e52e49c3d Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 27 Aug 2024 11:18:13 -0700 Subject: [PATCH] Fix how `data.google_client_config` handles errors from invalid credentials (#11470) (#19286) [upstream:f5655cea2387483dd971b6be68c95492319c80f2] Signed-off-by: Modular Magician --- .changelog/11470.txt | 3 +++ .../data_source_google_client_config.go | 4 +--- .../data_source_google_client_config_test.go | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 .changelog/11470.txt diff --git a/.changelog/11470.txt b/.changelog/11470.txt new file mode 100644 index 00000000000..7bad66dc7d9 --- /dev/null +++ b/.changelog/11470.txt @@ -0,0 +1,3 @@ +```release-note:bug +resourcemanager: fixed a bug where data.google_client_config failed silently when inadequate credentials were used to configure the provider +``` \ No newline at end of file diff --git a/google/services/resourcemanager/data_source_google_client_config.go b/google/services/resourcemanager/data_source_google_client_config.go index 5d2e331f468..2149f6a4959 100644 --- a/google/services/resourcemanager/data_source_google_client_config.go +++ b/google/services/resourcemanager/data_source_google_client_config.go @@ -8,7 +8,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-provider-google/google/fwmodels" "github.com/hashicorp/terraform-provider-google/google/fwresource" @@ -121,7 +120,6 @@ func (d *GoogleClientConfigDataSource) Configure(ctx context.Context, req dataso func (d *GoogleClientConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data GoogleClientConfigModel var metaData *fwmodels.ProviderMetaModel - var diags diag.Diagnostics // Read Provider meta into the meta model resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metaData)...) @@ -147,7 +145,7 @@ func (d *GoogleClientConfigDataSource) Read(ctx context.Context, req datasource. token, err := d.providerConfig.TokenSource.Token() if err != nil { - diags.AddError("Error setting access_token", err.Error()) + resp.Diagnostics.AddError("Error setting access_token", err.Error()) return } data.AccessToken = types.StringValue(token.AccessToken) diff --git a/google/services/resourcemanager/data_source_google_client_config_test.go b/google/services/resourcemanager/data_source_google_client_config_test.go index 73b194f2a44..ad4282d4070 100644 --- a/google/services/resourcemanager/data_source_google_client_config_test.go +++ b/google/services/resourcemanager/data_source_google_client_config_test.go @@ -3,6 +3,7 @@ package resourcemanager_test import ( + "regexp" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -55,6 +56,22 @@ func TestAccDataSourceGoogleClientConfig_omitLocation(t *testing.T) { }) } +func TestAccDataSourceGoogleClientConfig_invalidCredentials(t *testing.T) { + badCreds := acctest.GenerateFakeCredentialsJson("test") + t.Setenv("GOOGLE_CREDENTIALS", badCreds) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleClientConfig_basic, + ExpectError: regexp.MustCompile("Error setting access_token"), + }, + }, + }) +} + const testAccCheckGoogleClientConfig_basic = ` provider "google" { default_labels = {