use logPlan for destroy

This commit is contained in:
Shreyas Goenka 2023-07-12 18:24:05 +02:00
parent 57421f185c
commit 7fe4113f5b
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 21 additions and 53 deletions

View File

@ -3,59 +3,13 @@ package terraform
import (
"context"
"fmt"
"strings"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/fatih/color"
"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
)
type PlanResourceChange struct {
ResourceType string `json:"resource_type"`
Action string `json:"action"`
ResourceName string `json:"resource_name"`
}
func (c *PlanResourceChange) String() string {
result := strings.Builder{}
switch c.Action {
case "delete":
result.WriteString(" delete ")
default:
result.WriteString(c.Action + " ")
}
switch c.ResourceType {
case "databricks_job":
result.WriteString("job ")
case "databricks_pipeline":
result.WriteString("pipeline ")
default:
result.WriteString(c.ResourceType + " ")
}
result.WriteString(c.ResourceName)
return result.String()
}
func (c *PlanResourceChange) IsInplaceSupported() bool {
return false
}
func logDestroyPlan(ctx context.Context, changes []*tfjson.ResourceChange) error {
cmdio.LogString(ctx, "The following resources will be removed:")
for _, c := range changes {
if c.Change.Actions.Delete() {
cmdio.Log(ctx, &PlanResourceChange{
ResourceType: c.Type,
Action: "delete",
ResourceName: c.Name,
})
}
}
return nil
}
type destroy struct{}
func (w *destroy) Name() string {
@ -81,7 +35,7 @@ func (w *destroy) Apply(ctx context.Context, b *bundle.Bundle) error {
}
// print the resources that will be destroyed
err = logDestroyPlan(ctx, plan.ResourceChanges)
err = logPlan(ctx, plan)
if err != nil {
return err
}
@ -89,7 +43,7 @@ func (w *destroy) Apply(ctx context.Context, b *bundle.Bundle) error {
// Ask for confirmation, if needed
if !b.Plan.ConfirmApply {
red := color.New(color.FgRed).SprintFunc()
b.Plan.ConfirmApply, err = cmdio.Ask(ctx, fmt.Sprintf("\nThis will permanently %s resources! Proceed? [y/n]: ", red("destroy")))
b.Plan.ConfirmApply, err = cmdio.Ask(ctx, fmt.Sprintf("This will permanently %s resources! Proceed? [y/n]: ", red("destroy")))
if err != nil {
return err
}

View File

@ -13,6 +13,7 @@ import (
)
func logPlan(ctx context.Context, plan *tfjson.Plan) error {
cmdio.LogString(ctx, "Plan:")
for _, change := range plan.ResourceChanges {
tfActions := change.Change.Actions
if tfActions.Read() || tfActions.NoOp() {
@ -22,22 +23,35 @@ func logPlan(ctx context.Context, plan *tfjson.Plan) error {
var action string
switch {
case tfActions.Update():
action = "update"
action = "Update"
case tfActions.Create():
action = "create"
action = "Create"
case tfActions.Delete():
action = "delete"
action = "Delete"
case tfActions.Replace():
action = "replace"
action = "Replace"
default:
return fmt.Errorf("unknown terraform actions: %s", tfActions)
}
err := cmdio.RenderWithTemplate(ctx, change, fmt.Sprintf("%s {{.Type}} {{.Name}}\n", action))
resourceType := change.Type
switch resourceType {
case "databricks_job":
resourceType = "Job"
case "databricks_pipeline":
resourceType = "DLT Pipeline"
case "databricks_mlflow_model":
resourceType = "Mlflow Model"
case "databricks_mlflow_experiment":
resourceType = "Mlflow Experiment"
}
err := cmdio.RenderWithTemplate(ctx, change, fmt.Sprintf("%s %s {{.Name}}\n", action, resourceType))
if err != nil {
return err
}
}
cmdio.LogString(ctx, "")
return nil
}