From d5c8f1fee85b60c357af73b5800acc02db3dc0fe Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Thu, 27 Jul 2023 09:12:22 +0900 Subject: [PATCH] refactor: process each changes instead of plan --- internal/format/format_json.go | 19 +------ internal/format/format_unknown.go | 19 +------ internal/terraform/plan.go | 39 +++++++++----- test/format_json_test/format_json_test.go | 56 ++++++-------------- test/format_json_test/format_unknown_test.go | 56 ++++++++------------ 5 files changed, 66 insertions(+), 123 deletions(-) diff --git a/internal/format/format_json.go b/internal/format/format_json.go index 0d48e19..1014139 100644 --- a/internal/format/format_json.go +++ b/internal/format/format_json.go @@ -2,27 +2,10 @@ package format import ( "encoding/json" - "errors" tfjson "github.com/hashicorp/terraform-json" ) -func FormatJsonPlan(plan *tfjson.Plan) (*tfjson.Plan, error) { - var err error - if plan == nil { - return nil, errors.New("nil plan supplied") - } - - for i := range plan.ResourceChanges { - plan.ResourceChanges[i].Change, err = formatJsonChange(plan.ResourceChanges[i].Change) - if err != nil { - return nil, err - } - } - - return plan, nil -} - -func formatJsonChange(change *tfjson.Change) (*tfjson.Change, error) { +func FormatJsonChange(change *tfjson.Change) (*tfjson.Change, error) { var err error change.Before, err = formatJsonChangeValue(change.Before) diff --git a/internal/format/format_unknown.go b/internal/format/format_unknown.go index c2506cc..aa8dada 100644 --- a/internal/format/format_unknown.go +++ b/internal/format/format_unknown.go @@ -1,27 +1,10 @@ package format import ( - "errors" tfjson "github.com/hashicorp/terraform-json" ) -func FormatUnknownPlan(plan *tfjson.Plan) (*tfjson.Plan, error) { - var err error - if plan == nil { - return nil, errors.New("nil plan supplied") - } - - for i := range plan.ResourceChanges { - plan.ResourceChanges[i].Change, err = formatUnknownChange(plan.ResourceChanges[i].Change) - if err != nil { - return nil, err - } - } - - return plan, nil -} - -func formatUnknownChange(change *tfjson.Change) (*tfjson.Change, error) { +func FormatUnknownChange(change *tfjson.Change) (*tfjson.Change, error) { if change.Actions.Update() { for k, v := range change.AfterUnknown.(map[string]interface{}) { switch v.(type) { diff --git a/internal/terraform/plan.go b/internal/terraform/plan.go index 6372ec0..3347871 100644 --- a/internal/terraform/plan.go +++ b/internal/terraform/plan.go @@ -143,6 +143,29 @@ func (plan *PlanData) Render(w io.Writer, escapeHTML bool) error { return nil } +func processPlan(plan *tfjson.Plan) (*tfjson.Plan, error) { + var err error + + for i := range plan.ResourceChanges { + plan.ResourceChanges[i].Change, err = sanitize.SanitizeChange(plan.ResourceChanges[i].Change, sanitize.DefaultSensitiveValue) + if err != nil { + return nil, fmt.Errorf("failed to sanitize plan: %w", err) + } + + plan.ResourceChanges[i].Change, err = format.FormatJsonChange(plan.ResourceChanges[i].Change) + if err != nil { + return nil, fmt.Errorf("failed to prettify plan: %w", err) + } + + plan.ResourceChanges[i].Change, err = format.FormatUnknownChange(plan.ResourceChanges[i].Change) + if err != nil { + return nil, fmt.Errorf("failed to prettify plan: %w", err) + } + } + + return plan, nil +} + func NewPlanData(input io.Reader) (*PlanData, error) { var err error var plan tfjson.Plan @@ -150,23 +173,13 @@ func NewPlanData(input io.Reader) (*PlanData, error) { return nil, fmt.Errorf("cannot parse input: %w", err) } - sanitizedPlan, err := sanitize.SanitizePlan(&plan) - if err != nil { - return nil, fmt.Errorf("failed to sanitize plan: %w", err) - } - - formattedJsonPlan, err := format.FormatJsonPlan(sanitizedPlan) + processedPlan, err := processPlan(&plan) if err != nil { - return nil, fmt.Errorf("failed to prettify plan: %w", err) - } - - formattedUnknownPlan, err := format.FormatUnknownPlan(formattedJsonPlan) - if err != nil { - return nil, fmt.Errorf("failed to prettify plan: %w", err) + return nil, err } planData := PlanData{} - for _, c := range formattedUnknownPlan.ResourceChanges { + for _, c := range processedPlan.ResourceChanges { if c.Change.Actions.NoOp() || c.Change.Actions.Read() { continue } diff --git a/test/format_json_test/format_json_test.go b/test/format_json_test/format_json_test.go index 8ca10f2..3746a98 100644 --- a/test/format_json_test/format_json_test.go +++ b/test/format_json_test/format_json_test.go @@ -9,80 +9,56 @@ import ( func TestFormatJsonPlan(t *testing.T) { type args struct { - old *tfjson.Plan + old *tfjson.Change } tests := []struct { name string args args - want *tfjson.Plan + want *tfjson.Change wantErr bool }{ { name: "plain string", args: args{ - old: &tfjson.Plan{ - ResourceChanges: []*tfjson.ResourceChange{ - { - Change: &tfjson.Change{ - Before: "plain string", - After: "plain string", - }, - }, - }, + old: &tfjson.Change{ + Before: "plain string", + After: "plain string", }, }, - want: &tfjson.Plan{ - ResourceChanges: []*tfjson.ResourceChange{ - { - Change: &tfjson.Change{ - Before: "plain string", - After: "plain string", - }, - }, - }, + want: &tfjson.Change{ + Before: "plain string", + After: "plain string", }, wantErr: false, }, { name: "json string", args: args{ - old: &tfjson.Plan{ - ResourceChanges: []*tfjson.ResourceChange{ - { - Change: &tfjson.Change{ - Before: `{"foo":"bar"}`, - After: `{"foo":"bar"}`, - }, - }, - }, + old: &tfjson.Change{ + Before: `{"foo":"bar"}`, + After: `{"foo":"bar"}`, }, }, - want: &tfjson.Plan{ - ResourceChanges: []*tfjson.ResourceChange{ - { - Change: &tfjson.Change{ - Before: `{ + want: &tfjson.Change{ + Before: `{ "foo": "bar" }`, - After: `{ + After: `{ "foo": "bar" }`, - }, - }, - }, }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := format.FormatJsonPlan(tt.args.old) + got, err := format.FormatJsonChange(tt.args.old) if (err != nil) != tt.wantErr { t.Errorf("FormatJsonPlan() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("FormatJsonPlan() got = \n%v\n, want \n%v", got.ResourceChanges[0].Change.After, tt.want.ResourceChanges[0].Change.After) + t.Errorf("FormatJsonPlan() got = \n%v\n, want \n%v", got.After, tt.want.After) } }) } diff --git a/test/format_json_test/format_unknown_test.go b/test/format_json_test/format_unknown_test.go index 872f899..a8fdd07 100644 --- a/test/format_json_test/format_unknown_test.go +++ b/test/format_json_test/format_unknown_test.go @@ -9,50 +9,38 @@ import ( func TestFormatUnknownPlan(t *testing.T) { type args struct { - old *tfjson.Plan + old *tfjson.Change } tests := []struct { name string args args - want *tfjson.Plan + want *tfjson.Change wantErr bool }{ { name: "plain string", args: args{ - old: &tfjson.Plan{ - ResourceChanges: []*tfjson.ResourceChange{ - { - Change: &tfjson.Change{ - Actions: tfjson.Actions{tfjson.ActionUpdate}, - Before: map[string]interface{}{ - "foo": "bar", - }, - After: map[string]interface{}{}, - AfterUnknown: map[string]interface{}{ - "foo": true, - }, - }, - }, + old: &tfjson.Change{ + Actions: tfjson.Actions{tfjson.ActionUpdate}, + Before: map[string]interface{}{ + "foo": "bar", + }, + After: map[string]interface{}{}, + AfterUnknown: map[string]interface{}{ + "foo": true, }, }, }, - want: &tfjson.Plan{ - ResourceChanges: []*tfjson.ResourceChange{ - { - Change: &tfjson.Change{ - Actions: tfjson.Actions{tfjson.ActionUpdate}, - Before: map[string]interface{}{ - "foo": "bar", - }, - After: map[string]interface{}{ - "foo": "(known after apply)", - }, - AfterUnknown: map[string]interface{}{ - "foo": true, - }, - }, - }, + want: &tfjson.Change{ + Actions: tfjson.Actions{tfjson.ActionUpdate}, + Before: map[string]interface{}{ + "foo": "bar", + }, + After: map[string]interface{}{ + "foo": "(known after apply)", + }, + AfterUnknown: map[string]interface{}{ + "foo": true, }, }, wantErr: false, @@ -60,13 +48,13 @@ func TestFormatUnknownPlan(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := format.FormatUnknownPlan(tt.args.old) + got, err := format.FormatUnknownChange(tt.args.old) if (err != nil) != tt.wantErr { t.Errorf("FormatJsonPlan() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("FormatJsonPlan() got = \n%v\n, want \n%v", got.ResourceChanges[0].Change.After, tt.want.ResourceChanges[0].Change.After) + t.Errorf("FormatJsonPlan() got = \n%v\n, want \n%v", got.After, tt.want.After) } }) }