Skip to content

Commit

Permalink
Backfill user_project_overrides + billing project for Bigtable resour…
Browse files Browse the repository at this point in the history
…ces (GoogleCloudPlatform#5125)

Co-authored-by: upodroid <cy@borg.dev>
  • Loading branch information
upodroid authored and khajduczenia committed Oct 12, 2021
1 parent 325cc8f commit 08832ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
31 changes: 27 additions & 4 deletions mmv1/third_party/terraform/utils/bigtable_client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,44 @@ package google

import (
"context"
"os"

"cloud.google.com/go/bigtable"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)

type BigtableClientFactory struct {
UserAgent string
TokenSource oauth2.TokenSource
UserAgent string
TokenSource oauth2.TokenSource
BillingProject string
UserProjectOverride bool
}

func (s BigtableClientFactory) NewInstanceAdminClient(project string) (*bigtable.InstanceAdminClient, error) {
return bigtable.NewInstanceAdminClient(context.Background(), project, option.WithTokenSource(s.TokenSource), option.WithUserAgent(s.UserAgent))
var opts []option.ClientOption
if requestReason := os.Getenv("CLOUDSDK_CORE_REQUEST_REASON"); requestReason != "" {
opts = append(opts, option.WithRequestReason(requestReason))
}

if s.UserProjectOverride && s.BillingProject != "" {
opts = append(opts, option.WithQuotaProject(s.BillingProject))
}

opts = append(opts, option.WithTokenSource(s.TokenSource), option.WithUserAgent(s.UserAgent))
return bigtable.NewInstanceAdminClient(context.Background(), project, opts...)
}

func (s BigtableClientFactory) NewAdminClient(project, instance string) (*bigtable.AdminClient, error) {
return bigtable.NewAdminClient(context.Background(), project, instance, option.WithTokenSource(s.TokenSource), option.WithUserAgent(s.UserAgent))
var opts []option.ClientOption
if requestReason := os.Getenv("CLOUDSDK_CORE_REQUEST_REASON"); requestReason != "" {
opts = append(opts, option.WithRequestReason(requestReason))
}

if s.UserProjectOverride && s.BillingProject != "" {
opts = append(opts, option.WithQuotaProject(s.BillingProject))
}

opts = append(opts, option.WithTokenSource(s.TokenSource), option.WithUserAgent(s.UserAgent))
return bigtable.NewAdminClient(context.Background(), project, instance, opts...)
}
6 changes: 4 additions & 2 deletions mmv1/third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,10 @@ func (c *Config) NewCloudIdentityClient(userAgent string) *cloudidentity.Service

func (c *Config) BigTableClientFactory(userAgent string) *BigtableClientFactory {
bigtableClientFactory := &BigtableClientFactory{
UserAgent: userAgent,
TokenSource: c.tokenSource,
UserAgent: userAgent,
TokenSource: c.tokenSource,
BillingProject: c.BillingProject,
UserProjectOverride: c.UserProjectOverride,
}

return bigtableClientFactory
Expand Down

0 comments on commit 08832ba

Please sign in to comment.