Skip to content

Commit

Permalink
Merge pull request #1 from MansaGroup/master
Browse files Browse the repository at this point in the history
feat: create temporary file containing Google credentials
  • Loading branch information
felipe-minka authored Sep 8, 2022
2 parents d03ab42 + a212a24 commit 6cdcd89
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package postgresql
import (
"context"
"fmt"
"os"

"github.com/blang/semver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"golang.org/x/oauth2/google"

"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -201,6 +203,30 @@ func getRDSAuthToken(profile string, username string, host string, port int) (st
return token, err
}

func createGoogleCredsFileIfNeeded() error {
if _, err := google.FindDefaultCredentials(context.Background()); err == nil {
return nil
}

rawGoogleCredentials := os.Getenv("GOOGLE_CREDENTIALS")
if rawGoogleCredentials == "" {
return nil
}

tmpFile, err := os.CreateTemp("", "")
if err != nil {
return err
}
defer tmpFile.Close()

_, err = tmpFile.WriteString(rawGoogleCredentials)
if err != nil {
return err
}

return os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", tmpFile.Name())
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
var sslMode string
if sslModeRaw, ok := d.GetOk("sslmode"); ok {
Expand Down Expand Up @@ -255,6 +281,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}

if config.Scheme == "gcppostgres" {
if err := createGoogleCredsFileIfNeeded(); err != nil {
return nil, err
}
}

client := config.NewClient(d.Get("database").(string))
return client, nil
}

0 comments on commit 6cdcd89

Please sign in to comment.