From fd67acbc7f88895bd3c8a81ce189a8bc953bb7f0 Mon Sep 17 00:00:00 2001 From: Wojciech Pratkowiecki Date: Thu, 3 Oct 2024 09:37:11 +0200 Subject: [PATCH] Move show plan to a function --- bundle/phases/deploy.go | 43 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/bundle/phases/deploy.go b/bundle/phases/deploy.go index 9cf5eeaa..444834ba 100644 --- a/bundle/phases/deploy.go +++ b/bundle/phases/deploy.go @@ -51,6 +51,43 @@ func parseTerraformActions(changes []*tfjson.ResourceChange, toInclude func(typ return res } +func showDryRunChanges(ctx context.Context, plan *tfjson.Plan) { + updateActions := make([]terraformlib.Action, 0) + for _, rc := range plan.ResourceChanges { + if rc.Change.Actions.Update() { + updateActions = append(updateActions, terraformlib.Action{ + Action: terraformlib.ActionTypeUpdate, + ResourceType: rc.Type, + ResourceName: rc.Name, + }) + } + } + createActions := make([]terraformlib.Action, 0) + for _, rc := range plan.ResourceChanges { + if rc.Change.Actions.Create() { + createActions = append(createActions, terraformlib.Action{ + Action: terraformlib.ActionTypeCreate, + ResourceType: rc.Type, + ResourceName: rc.Name, + }) + } + } + if len(updateActions) > 0 { + cmdio.LogString(ctx, "The following resources will be updated:") + for _, a := range updateActions { + cmdio.Log(ctx, a) + } + cmdio.LogString(ctx, "") + } + if len(createActions) > 0 { + cmdio.LogString(ctx, "The following resources will be created:") + for _, a := range createActions { + cmdio.Log(ctx, a) + } + cmdio.LogString(ctx, "") + } +} + func approvalForDeploy(ctx context.Context, b *bundle.Bundle) (bool, error) { tf := b.Terraform if tf == nil { @@ -64,11 +101,7 @@ func approvalForDeploy(ctx context.Context, b *bundle.Bundle) (bool, error) { } if b.DryRun { - cmdio.LogString(ctx, "Following changes would be deployed:") - _, err := tf.ShowPlanFile(ctx, b.Plan.Path) - if err != nil { - return false, err - } + showDryRunChanges(ctx, plan) return false, nil }