@@ -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"
@@ -210,6 +212,30 @@ func getRDSAuthToken(region string, profile string, username string, host string
210212 return token , err
211213}
212214
215+ func createGoogleCredsFileIfNeeded () error {
216+ if _ , err := google .FindDefaultCredentials (context .Background ()); err == nil {
217+ return nil
218+ }
219+
220+ rawGoogleCredentials := os .Getenv ("GOOGLE_CREDENTIALS" )
221+ if rawGoogleCredentials == "" {
222+ return nil
223+ }
224+
225+ tmpFile , err := os .CreateTemp ("" , "" )
226+ if err != nil {
227+ return fmt .Errorf ("could not create temporary file: %w" , err )
228+ }
229+ defer tmpFile .Close ()
230+
231+ _ , err = tmpFile .WriteString (rawGoogleCredentials )
232+ if err != nil {
233+ return fmt .Errorf ("could not write in temporary file: %w" , err )
234+ }
235+
236+ return os .Setenv ("GOOGLE_APPLICATION_CREDENTIALS" , tmpFile .Name ())
237+ }
238+
213239func providerConfigure (d * schema.ResourceData ) (interface {}, error ) {
214240 var sslMode string
215241 if sslModeRaw , ok := d .GetOk ("sslmode" ); ok {
@@ -265,6 +291,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
265291 }
266292 }
267293
294+ if config .Scheme == "gcppostgres" {
295+ if err := createGoogleCredsFileIfNeeded (); err != nil {
296+ return nil , err
297+ }
298+ }
299+
268300 client := config .NewClient (d .Get ("database" ).(string ))
269301 return client , nil
270302}
0 commit comments