Repository: GoogleCloudPlatform/terraform-validator
Pull request #622 (2022-04-04 15:39:22)

Revert "Change ancestry from string to map[string]string"
Reverts GoogleCloudPlatform/terraform-validator#607


Patch:
diff --git a/cmd/convert.go b/cmd/convert.go
index a7b04b39..f0c6b521 100644
--- a/cmd/convert.go
+++ b/cmd/convert.go
@@ -93,10 +93,7 @@ func (o *convertOptions) validateArgs(args []string) error {
 
 func (o *convertOptions) run(plan string) error {
 	ctx := context.Background()
-	ancestryCache := map[string]string{
-		o.project: o.ancestry,
-	}
-	assets, err := o.readPlannedAssets(ctx, plan, o.project, ancestryCache, o.offline, false, o.rootOptions.errorLogger)
+	assets, err := o.readPlannedAssets(ctx, plan, o.project, o.ancestry, 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 cb48fb75..bf04ea96 100644
--- a/cmd/validate.go
+++ b/cmd/validate.go
@@ -112,10 +112,7 @@ func (o *validateOptions) run(plan string) error {
 	var assets []google.Asset
 	if err := json.Unmarshal(content, &assets); err != nil {
 		var err error
-		ancestryCache := map[string]string{
-			o.project: o.ancestry,
-		}
-		assets, err = o.readPlannedAssets(ctx, plan, o.project, ancestryCache, o.offline, false, o.rootOptions.errorLogger)
+		assets, err = o.readPlannedAssets(ctx, plan, o.project, o.ancestry, 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/tfgcv/planned_assets.go b/tfgcv/planned_assets.go
index 3060830e..2c54ed01 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 string, ancestryCache map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error)
+type ReadPlannedAssetsFunc func(ctx context.Context, path, project, ancestry 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,8 +39,8 @@ type ReadPlannedAssetsFunc func(ctx context.Context, path, project string, ances
 // 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 string, ancestryCache map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error) {
-	converter, err := newConverter(ctx, path, project, ancestryCache, offline, convertUnchanged, errorLogger)
+func ReadPlannedAssets(ctx context.Context, path, project, ancestry 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,16 +63,19 @@ func ReadPlannedAssets(ctx context.Context, path, project string, ancestryCache
 	return converter.Assets(), nil
 }
 
-func newConverter(ctx context.Context, path, project string, ancestryCache map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) (*google.Converter, error) {
+func newConverter(ctx context.Context, path, project, ancestry 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, ancestryCache, errorLogger)
+	ancestryManager, err := ancestrymanager.New(resourceManager, entries, 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 f1ae5925..e1f3c75d 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 string, ancestryCache map[string]string, offline, convertUnchanged bool, errorLogger *zap.Logger) ([]google.Asset, error) {
+func MockReadPlannedAssets(ctx context.Context, path, project, ancestry 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 d5a6405c..dd4d9719 100644
--- a/test/read_test.go
+++ b/test/read_test.go
@@ -138,12 +138,9 @@ func TestReadPlannedAssetsCoverage(t *testing.T) {
 
 			planfile := filepath.Join(dir, c.name+".tfplan.json")
 			ctx := context.Background()
-			ancestryCache := map[string]string{
-				data.Provider["project"]: data.Ancestry,
-			}
-			got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], ancestryCache, true, false, zaptest.NewLogger(t))
+			got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], data.Ancestry, true, false, zaptest.NewLogger(t))
 			if err != nil {
-				t.Fatalf("ReadPlannedAssets(%s, %s, %s, %t): %v", planfile, data.Provider["project"], ancestryCache, true, err)
+				t.Fatalf("ReadPlannedAssets(%s, %s, %s, %t): %v", planfile, data.Provider["project"], data.Ancestry, true, err)
 			}
 
 			expectedAssets := normalizeAssets(t, want, true)
diff --git a/tfgcv/planned_assets_test.go b/tfgcv/planned_assets_test.go
index 425bc841..2d385816 100644
--- a/tfgcv/planned_assets_test.go
+++ b/tfgcv/planned_assets_test.go
@@ -2,10 +2,9 @@ package tfgcv
 
 import (
 	"context"
+	"go.uber.org/zap"
 	"path/filepath"
 	"testing"
-
-	"go.uber.org/zap"
 )
 
 const (
@@ -78,10 +77,7 @@ func TestReadPlannedAssets(t *testing.T) {
 			var offline bool
 			offline = true
 			ctx := context.Background()
-			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())
+			got, err := ReadPlannedAssets(ctx, testFile, tt.args.project, tt.args.ancestry, offline, tt.args.convertUnchanged, zap.NewExample())
 			if (err != nil) != tt.wantErr {
 				t.Errorf("ReadPlannedAssets() error = %v, wantErr %v", err, tt.wantErr)
 				return