@@ -3,10 +3,12 @@ package postgresql
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
6
7
7
8
"github.com/blang/semver"
8
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11
+ "golang.org/x/oauth2/google"
10
12
11
13
"github.com/aws/aws-sdk-go-v2/aws"
12
14
awsConfig "github.com/aws/aws-sdk-go-v2/config"
@@ -201,6 +203,30 @@ func getRDSAuthToken(profile string, username string, host string, port int) (st
201
203
return token , err
202
204
}
203
205
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
+
204
230
func providerConfigure (d * schema.ResourceData ) (interface {}, error ) {
205
231
var sslMode string
206
232
if sslModeRaw , ok := d .GetOk ("sslmode" ); ok {
@@ -255,6 +281,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
255
281
}
256
282
}
257
283
284
+ if config .Scheme == "gcppostgres" {
285
+ if err := createGoogleCredsFileIfNeeded (); err != nil {
286
+ return nil , err
287
+ }
288
+ }
289
+
258
290
client := config .NewClient (d .Get ("database" ).(string ))
259
291
return client , nil
260
292
}
0 commit comments