@@ -3,10 +3,12 @@ package postgresql
33import (
44 "context"
55 "fmt"
6+ "os"
67
78 "github.com/blang/semver"
89 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+ "golang.org/x/oauth2/google"
1012
1113 "github.com/aws/aws-sdk-go-v2/aws"
1214 awsConfig "github.com/aws/aws-sdk-go-v2/config"
@@ -201,6 +203,30 @@ func getRDSAuthToken(profile string, username string, host string, port int) (st
201203 return token , err
202204}
203205
206+ func createGoogleCredsFileIfNeeded () error {
207+ if _ , err := google .FindDefaultCredentials (context .Background ()); err == nil {
208+ return nil
209+ }
210+
211+ rawGoogleCredentials := os .Getenv ("GOOGLE_CREDENTIALS" )
212+ if rawGoogleCredentials == "" {
213+ return nil
214+ }
215+
216+ tmpFile , err := os .CreateTemp ("" , "" )
217+ if err != nil {
218+ return err
219+ }
220+ defer tmpFile .Close ()
221+
222+ _ , err = tmpFile .WriteString (rawGoogleCredentials )
223+ if err != nil {
224+ return err
225+ }
226+
227+ return os .Setenv ("GOOGLE_APPLICATION_CREDENTIALS" , tmpFile .Name ())
228+ }
229+
204230func providerConfigure (d * schema.ResourceData ) (interface {}, error ) {
205231 var sslMode string
206232 if sslModeRaw , ok := d .GetOk ("sslmode" ); ok {
@@ -255,6 +281,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
255281 }
256282 }
257283
284+ if config .Scheme == "gcppostgres" {
285+ if err := createGoogleCredsFileIfNeeded (); err != nil {
286+ return nil , err
287+ }
288+ }
289+
258290 client := config .NewClient (d .Get ("database" ).(string ))
259291 return client , nil
260292}
0 commit comments