Repository: GoogleCloudPlatform/terraform-validator
Pull request #634 (2022-04-07 14:22:53)

change ancestry cache from string to map[string]string
This PR should be merged together with https://github.com/GoogleCloudPlatform/magic-modules/pull/5894/files


Patch:
diff --git a/cmd/convert.go b/cmd/convert.go
index f0c6b521..a7b04b39 100644
--- a/cmd/convert.go
+++ b/cmd/convert.go
@@ -93,7 +93,10 @@ func (o *convertOptions) validateArgs(args []string) error {
 
 func (o *convertOptions) run(plan string) error {
 	ctx := context.Background()
-	assets, err := o.readPlannedAssets(ctx, plan, o.project, o.ancestry, o.offline, false, o.rootOptions.errorLogger)
+	ancestryCache := map[string]string{
+		o.project: o.ancestry,
+	}
+	assets, err := o.readPlannedAssets(ctx, plan, o.project, ancestryCache, o.offline, false, o.rootOptions.errorLogger)
 	if err != nil {
 		if errors.Cause(err) == tfgcv.ErrParsingProviderProject {
 			return errors.New("unable to parse provider project, please use --project flag")
diff --git a/cmd/validate.go b/cmd/validate.go
index bf04ea96..cb48fb75 100644
--- a/cmd/validate.go
+++ b/cmd/validate.go
@@ -112,7 +112,10 @@ func (o *validateOptions) run(plan string) error {
 	var assets []google.Asset
 	if err := json.Unmarshal(content, &assets); err != nil {
 		var err error
-		assets, err = o.readPlannedAssets(ctx, plan, o.project, o.ancestry, o.offline, false, o.rootOptions.errorLogger)
+		ancestryCache := map[string]string{
+			o.project: o.ancestry,
+		}
+		assets, err = o.readPlannedAssets(ctx, plan, o.project, ancestryCache, o.offline, false, o.rootOptions.errorLogger)
 		if err != nil {
 			if errors.Cause(err) == tfgcv.ErrParsingProviderProject {
 				return errors.New("unable to parse provider project, please use --project flag")
diff --git a/go.mod b/go.mod
index 4a79b9ad..fac64000 100644
--- a/go.mod
+++ b/go.mod
@@ -12,7 +12,7 @@ require (
 	github.com/hashicorp/go-cleanhttp v0.5.2
 	github.com/hashicorp/terraform-json v0.13.0
 	github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
-	github.com/hashicorp/terraform-provider-google v1.20.1-0.20220406185319-b4ba411cbd5d
+	github.com/hashicorp/terraform-provider-google v1.20.1-0.20220407151527-a80547d2a1d2
 	github.com/kr/pretty v0.3.0 // indirect
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/onsi/gomega v1.17.0 // indirect
diff --git a/go.sum b/go.sum
index 8b8aa53b..d6ebd0c0 100644
--- a/go.sum
+++ b/go.sum
@@ -696,8 +696,8 @@ github.com/hashicorp/terraform-plugin-log v0.2.0 h1:rjflRuBqCnSk3UHOR25MP1G5BDLK
 github.com/hashicorp/terraform-plugin-log v0.2.0/go.mod h1:E1kJmapEHzqu1x6M++gjvhzM2yMQNXPVWZRCB8sgYjg=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1 h1:B9AocC+dxrCqcf4vVhztIkSkt3gpRjUkEka8AmZWGlQ=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1/go.mod h1:FjM9DXWfP0w/AeOtJoSKHBZ01LqmaO6uP4bXhv3fekw=
-github.com/hashicorp/terraform-provider-google v1.20.1-0.20220406185319-b4ba411cbd5d h1:9NRqTxpI7CN4MpWU+3IaIbmh+q/0povbU15PbZ5W/Uo=
-github.com/hashicorp/terraform-provider-google v1.20.1-0.20220406185319-b4ba411cbd5d/go.mod h1:zuersTi7DrjFzHJ9B6noGikoBdHD6EJj6RlQ2YMQNb4=
+github.com/hashicorp/terraform-provider-google v1.20.1-0.20220407151527-a80547d2a1d2 h1:mqIcQpnmjZwnF/L+D2lLDbjg6TaNlc7OB+Rc9klYPHE=
+github.com/hashicorp/terraform-provider-google v1.20.1-0.20220407151527-a80547d2a1d2/go.mod h1:zuersTi7DrjFzHJ9B6noGikoBdHD6EJj6RlQ2YMQNb4=
 github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
 github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
 github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
diff --git a/tfgcv/planned_assets.go b/tfgcv/planned_assets.go
index 2c54ed01..99da7d10 100644
--- a/tfgcv/planned_assets.go
+++ b/tfgcv/planned_assets.go
@@ -30,7 +30,7 @@ import (
 	"go.uber.org/zap"
 )
 
-type ReadPlannedAssetsFunc func(ctx context.Context, path, project, ancestry string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error)
+type ReadPlannedAssetsFunc func(ctx context.Context, path, project string, ancestry map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error)
 
 // ReadPlannedAssets extracts CAI assets from a terraform plan file.
 // If ancestry path is provided, it assumes the project is in that path rather
@@ -39,7 +39,7 @@ type ReadPlannedAssetsFunc func(ctx context.Context, path, project, ancestry str
 // are also reported in the output, otherwise only resources that are going to
 // be changed are reported.
 // It ignores non-supported resources.
-func ReadPlannedAssets(ctx context.Context, path, project, ancestry string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error) {
+func ReadPlannedAssets(ctx context.Context, path, project string, ancestry map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error) {
 	converter, err := newConverter(ctx, path, project, ancestry, offline, convertUnchanged, errorLogger)
 	if err != nil {
 		return nil, err
@@ -63,19 +63,17 @@ func ReadPlannedAssets(ctx context.Context, path, project, ancestry string, offl
 	return converter.Assets(), nil
 }
 
-func newConverter(ctx context.Context, path, project, ancestry string, offline, convertUnchanged bool, errorLogger *zap.Logger) (*google.Converter, error) {
+func newConverter(ctx context.Context, path, project string, ancestry map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) (*google.Converter, error) {
 	cfg, err := resources.GetConfig(ctx, project, offline)
 	if err != nil {
 		return nil, errors.Wrap(err, "building google configuration")
 	}
-	entries := map[string]string{
-		project: ancestry,
-	}
+
 	var resourceManager *cloudresourcemanager.Service
 	if !offline {
 		resourceManager = cfg.NewResourceManagerClient(cfg.UserAgent())
 	}
-	ancestryManager, err := ancestrymanager.New(resourceManager, entries, errorLogger)
+	ancestryManager, err := ancestrymanager.New(resourceManager, ancestry, errorLogger)
 	if err != nil {
 		return nil, errors.Wrap(err, "building google ancestry manager")
 	}


Test patch:
diff --git a/cmd/convert_test.go b/cmd/convert_test.go
index e1f3c75d..54e7db9e 100644
--- a/cmd/convert_test.go
+++ b/cmd/convert_test.go
@@ -36,7 +36,7 @@ func testAssets() []google.Asset {
 	}
 }
 
-func MockReadPlannedAssets(ctx context.Context, path, project, ancestry string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error) {
+func MockReadPlannedAssets(ctx context.Context, path, project string, ancestry map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error) {
 	return testAssets(), nil
 }
 
diff --git a/test/read_test.go b/test/read_test.go
index dd4d9719..d5a6405c 100644
--- a/test/read_test.go
+++ b/test/read_test.go
@@ -138,9 +138,12 @@ func TestReadPlannedAssetsCoverage(t *testing.T) {
 
 			planfile := filepath.Join(dir, c.name+".tfplan.json")
 			ctx := context.Background()
-			got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], data.Ancestry, true, false, zaptest.NewLogger(t))
+			ancestryCache := map[string]string{
+				data.Provider["project"]: data.Ancestry,
+			}
+			got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], ancestryCache, true, false, zaptest.NewLogger(t))
 			if err != nil {
-				t.Fatalf("ReadPlannedAssets(%s, %s, %s, %t): %v", planfile, data.Provider["project"], data.Ancestry, true, err)
+				t.Fatalf("ReadPlannedAssets(%s, %s, %s, %t): %v", planfile, data.Provider["project"], ancestryCache, true, err)
 			}
 
 			expectedAssets := normalizeAssets(t, want, true)
diff --git a/tfgcv/planned_assets_test.go b/tfgcv/planned_assets_test.go
index 2d385816..d6fc3a55 100644
--- a/tfgcv/planned_assets_test.go
+++ b/tfgcv/planned_assets_test.go
@@ -2,9 +2,10 @@ package tfgcv
 
 import (
 	"context"
-	"go.uber.org/zap"
 	"path/filepath"
 	"testing"
+
+	"go.uber.org/zap"
 )
 
 const (
@@ -74,10 +75,12 @@ func TestReadPlannedAssets(t *testing.T) {
 	for _, tt := range testCases(t) {
 		t.Run(tt.name, func(t *testing.T) {
 			testFile := filepath.Join(testDataDir, tt.args.file)
-			var offline bool
-			offline = true
+			offline := true
 			ctx := context.Background()
-			got, err := ReadPlannedAssets(ctx, testFile, tt.args.project, tt.args.ancestry, offline, tt.args.convertUnchanged, zap.NewExample())
+			ancestryCache := map[string]string{
+				tt.args.project: tt.args.ancestry,
+			}
+			got, err := ReadPlannedAssets(ctx, testFile, tt.args.project, ancestryCache, offline, tt.args.convertUnchanged, zap.NewExample())
 			if (err != nil) != tt.wantErr {
 				t.Errorf("ReadPlannedAssets() error = %v, wantErr %v", err, tt.wantErr)
 				return