Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temporary file with Google credential for terraformcloud #249

Merged
merged 4 commits into from
Nov 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 fmt.Errorf("could not create temporary file: %w", err)
}
defer tmpFile.Close()

_, err = tmpFile.WriteString(rawGoogleCredentials)
if err != nil {
return fmt.Errorf("could not write in temporary file: %w", 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
}