databricks-cli/bundle/deploy/terraform/apply.go

95 lines
1.9 KiB
Go
Raw Normal View History

package terraform
import (
"context"
"fmt"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/hashicorp/terraform-exec/tfexec"
)
2023-07-05 15:27:44 +00:00
type ApplyGoal string
const (
ApplyGoalDeploy = "deploy"
ApplyGoalDestroy = "destroy"
)
type apply struct {
goal ApplyGoal
}
func (w *apply) Name() string {
return "terraform.Apply"
}
func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) error {
2023-07-05 14:40:40 +00:00
// Return early if plan is empty
if b.Plan.IsEmpty {
cmdio.LogString(ctx, "Terraform plan is empty. Skipping apply.")
return nil
}
// Error if terraform is not initialized
tf := b.Terraform
if tf == nil {
return fmt.Errorf("terraform not initialized")
}
2023-07-05 14:40:40 +00:00
// Error if plan is missing
if b.Plan.Path == "" {
return fmt.Errorf("no plan found")
}
2023-07-05 14:40:40 +00:00
// Read and log plan file
plan, err := tf.ShowPlanFile(ctx, b.Plan.Path)
if err != nil {
return err
}
err = logPlan(ctx, plan)
if err != nil {
2023-07-05 14:40:40 +00:00
return err
}
2023-07-05 14:40:40 +00:00
// Ask for confirmation, if needed
if !b.Plan.ConfirmApply {
b.Plan.ConfirmApply, err = cmdio.Ask(ctx, "Proceed with apply? [y/n]: ")
if err != nil {
return err
}
}
if !b.Plan.ConfirmApply {
// return if confirmation was not provided
return nil
}
2023-07-05 15:27:44 +00:00
switch w.goal {
case ApplyGoalDeploy:
cmdio.LogString(ctx, "Starting deployment")
case ApplyGoalDestroy:
cmdio.LogString(ctx, "Starting destruction")
}
2023-07-05 14:40:40 +00:00
// Apply terraform according to the plan
err = tf.Apply(ctx, tfexec.DirOrPlan(b.Plan.Path))
if err != nil {
return fmt.Errorf("terraform apply: %w", err)
}
2023-07-05 15:27:44 +00:00
switch w.goal {
case ApplyGoalDeploy:
cmdio.LogString(ctx, "Deployment Successful!")
case ApplyGoalDestroy:
cmdio.LogString(ctx, "Destruction Successful!")
}
return nil
}
2023-07-05 15:27:44 +00:00
// Apply returns a [bundle.Mutator] that runs the equivalent of `terraform apply ./plan`
// from the bundle's ephemeral working directory for Terraform.
2023-07-05 15:27:44 +00:00
func Apply(goal ApplyGoal) bundle.Mutator {
return &apply{goal}
}