2023-04-06 10:54:58 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/terraform"
|
2023-04-06 10:54:58 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
2023-07-05 14:40:40 +00:00
|
|
|
tfjson "github.com/hashicorp/terraform-json"
|
2023-04-06 10:54:58 +00:00
|
|
|
)
|
|
|
|
|
2023-09-14 14:43:49 +00:00
|
|
|
// printPlanSummary prints a high level summary of the terraform plan that will
|
|
|
|
// be applied during bundle deploy / destroy.
|
|
|
|
func printPlanSummary(ctx context.Context, plan *tfjson.Plan) error {
|
|
|
|
cmdio.LogString(ctx, "\nPlan:")
|
2023-07-05 14:40:40 +00:00
|
|
|
for _, change := range plan.ResourceChanges {
|
|
|
|
tfActions := change.Change.Actions
|
|
|
|
if tfActions.Read() || tfActions.NoOp() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var action string
|
|
|
|
switch {
|
|
|
|
case tfActions.Update():
|
2023-07-12 16:24:05 +00:00
|
|
|
action = "Update"
|
2023-07-05 14:40:40 +00:00
|
|
|
case tfActions.Create():
|
2023-07-12 16:24:05 +00:00
|
|
|
action = "Create"
|
2023-07-05 14:40:40 +00:00
|
|
|
case tfActions.Delete():
|
2023-07-12 16:24:05 +00:00
|
|
|
action = "Delete"
|
2023-07-05 14:40:40 +00:00
|
|
|
case tfActions.Replace():
|
2023-07-12 16:24:05 +00:00
|
|
|
action = "Replace"
|
2023-07-05 14:40:40 +00:00
|
|
|
default:
|
2023-07-05 14:49:54 +00:00
|
|
|
return fmt.Errorf("unknown terraform actions: %s", tfActions)
|
2023-07-05 14:40:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 16:24:05 +00:00
|
|
|
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))
|
2023-07-05 14:40:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-12 16:24:05 +00:00
|
|
|
cmdio.LogString(ctx, "")
|
2023-07-05 14:40:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
type PlanGoal string
|
|
|
|
|
|
|
|
var (
|
|
|
|
PlanDeploy = PlanGoal("deploy")
|
|
|
|
PlanDestroy = PlanGoal("destroy")
|
|
|
|
)
|
|
|
|
|
|
|
|
type plan struct {
|
|
|
|
goal PlanGoal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *plan) Name() string {
|
|
|
|
return "terraform.Plan"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (p *plan) Apply(ctx context.Context, b *bundle.Bundle) error {
|
2023-04-06 10:54:58 +00:00
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("terraform not initialized")
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 14:43:49 +00:00
|
|
|
if p.goal == PlanDeploy {
|
|
|
|
cmdio.LogString(ctx, "Planning deployment")
|
|
|
|
}
|
|
|
|
if p.goal == PlanDestroy {
|
|
|
|
cmdio.LogString(ctx, "Planning destruction")
|
|
|
|
}
|
2023-04-18 14:55:06 +00:00
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
err := tf.Init(ctx, tfexec.Upgrade(true))
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("terraform init: %w", err)
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist computed plan
|
2023-09-11 08:18:43 +00:00
|
|
|
tfDir, err := Dir(ctx, b)
|
2023-04-06 10:54:58 +00:00
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
planPath := filepath.Join(tfDir, "plan")
|
|
|
|
destroy := p.goal == PlanDestroy
|
2023-04-18 14:55:06 +00:00
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
notEmpty, err := tf.Plan(ctx, tfexec.Destroy(destroy), tfexec.Out(planPath))
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set plan in main bundle struct for downstream mutators
|
|
|
|
b.Plan = &terraform.Plan{
|
|
|
|
Path: planPath,
|
|
|
|
ConfirmApply: b.AutoApprove,
|
|
|
|
IsEmpty: !notEmpty,
|
|
|
|
}
|
2023-04-18 14:55:06 +00:00
|
|
|
|
2023-09-14 14:43:49 +00:00
|
|
|
cmdio.LogString(ctx, "Planning complete")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plan returns a [bundle.Mutator] that runs the equivalent of `terraform plan -out ./plan`
|
|
|
|
// from the bundle's ephemeral working directory for Terraform.
|
|
|
|
func Plan(goal PlanGoal) bundle.Mutator {
|
|
|
|
return &plan{
|
|
|
|
goal: goal,
|
|
|
|
}
|
|
|
|
}
|