Repository: GoogleCloudPlatform/terraform-validator
Pull request #758 (2022-06-09 17:01:58)

Made client settable and gettable by TFV
<!-- Put a description of what this PR is for here, along with any references to issues that this resolves or contributes to -->

This is necessary for us to be able to support using an alternative client in some contexts.


<!-- AUTOCHANGELOG for Downstream PRs.

Please select one of the following "release-note:" headings:
    - release-note:enhancement
    - release-note:bug
    - release-note:note
    - release-note:new-resource
    - release-note:new-datasource
    - release-note:deprecation
    - release-note:breaking-change
    - release-note:none
    
Unless you choose release-note:none, please add a release note.

See .ci/RELEASE_NOTES_GUIDE.md for writing good release notes.

You can add more release note blocks if you want more than one CHANGELOG
entry for this PR.
-->
**Release Note Template for Downstream PRs (will be copied)**

```release-note:none

```

Derived from https://github.com/GoogleCloudPlatform/magic-modules/pull/6113


Patch:
diff --git a/converters/google/resources/getconfig.go b/converters/google/resources/getconfig.go
index 106aa5e1..0decea70 100644
--- a/converters/google/resources/getconfig.go
+++ b/converters/google/resources/getconfig.go
@@ -2,16 +2,28 @@ package google
 
 import (
 	"context"
+	"net/http"
 
 	"github.com/pkg/errors"
 )
 
+// Compatibility shim to let this change happen in two commits.
+// NewConfig better matches golang best practices.
+func GetConfig(ctx context.Context, project string, offline bool, userAgent string) (*Config, error) {
+	return NewConfig(ctx, project, offline, userAgent, nil)
+}
+
 // Return the value of the private userAgent field
 func (c *Config) UserAgent() string {
 	return c.userAgent
 }
 
-func GetConfig(ctx context.Context, project string, offline bool, userAgent string) (*Config, error) {
+// Return the value of the private client field
+func (c *Config) Client() *http.Client {
+	return c.client
+}
+
+func NewConfig(ctx context.Context, project string, offline bool, userAgent string, client *http.Client) (*Config, error) {
 	cfg := &Config{
 		Project:   project,
 		userAgent: userAgent,
@@ -49,6 +61,9 @@ func GetConfig(ctx context.Context, project string, offline bool, userAgent stri
 		if err := cfg.LoadAndValidate(ctx); err != nil {
 			return nil, errors.Wrap(err, "load and validate config")
 		}
+		if client != nil {
+			cfg.client = client
+		}
 	}
 
 	return cfg, nil


Test patch:
diff --git a/converters/google/resources/getconfig_test.go b/converters/google/resources/getconfig_test.go
index a776984e..61fae106 100644
--- a/converters/google/resources/getconfig_test.go
+++ b/converters/google/resources/getconfig_test.go
@@ -2,6 +2,7 @@ package google
 
 import (
 	"context"
+	"net/http"
 	"os"
 	"testing"
 
@@ -19,9 +20,6 @@ func getAccessToken(cfg *Config) string {
 func getImpersonateServiceAccount(cfg *Config) string {
 	return cfg.ImpersonateServiceAccount
 }
-func getUserAgent(cfg *Config) string {
-	return cfg.UserAgent()
-}
 func getZoneValue(cfg *Config) string {
 	return cfg.Zone
 }
@@ -169,10 +167,33 @@ func TestGetConfigUserAgent(t *testing.T) {
 		t.Run(c.userAgent, func(t *testing.T) {
 			cfg, err := GetConfig(ctx, "project", offline, c.userAgent)
 			if err != nil {
-				t.Fatalf("error building converter: %s", err)
+				t.Fatalf("error building config: %s", err)
 			}
 
-			assert.Equal(t, c.expected, getUserAgent(cfg))
+			assert.Equal(t, c.expected, cfg.UserAgent())
 		})
 	}
 }
+
+func TestGetConfigUserAgent_nilClientUsesDefault(t *testing.T) {
+	ctx := context.Background()
+	offline := false
+	cfg, err := NewConfig(ctx, "project", offline, "", nil)
+	if err != nil {
+		t.Fatalf("error building config: %s", err)
+	}
+
+	assert.NotEmpty(t, cfg.Client())
+}
+
+func TestGetConfigUserAgent_usesPassedClient(t *testing.T) {
+	ctx := context.Background()
+	offline := false
+	client := &http.Client{}
+	cfg, err := NewConfig(ctx, "project", offline, "", client)
+	if err != nil {
+		t.Fatalf("error building config: %s", err)
+	}
+
+	assert.Exactly(t, client, cfg.Client())
+}