Skip to content

Commit

Permalink
Fix how data.google_client_config handles errors from invalid crede…
Browse files Browse the repository at this point in the history
…ntials (#11470) (#19286)

[upstream:f5655cea2387483dd971b6be68c95492319c80f2]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 27, 2024
1 parent d78ea5b commit eef7f65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/11470.txt
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)...)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package resourcemanager_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit eef7f65

Please sign in to comment.