2022-12-09 07:57:30 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2022-12-09 07:57:30 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apply struct{}
|
|
|
|
|
|
|
|
func (w *apply) Name() string {
|
|
|
|
return "terraform.Apply"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
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
|
2022-12-15 16:30:33 +00:00
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("terraform not initialized")
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:40:40 +00:00
|
|
|
// Error if plan is missing
|
|
|
|
if b.Plan.Path == "" {
|
|
|
|
return fmt.Errorf("no plan found")
|
|
|
|
}
|
2023-04-18 14:55:06 +00:00
|
|
|
|
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)
|
2022-12-09 07:57:30 +00:00
|
|
|
if err != nil {
|
2023-07-05 14:40:40 +00:00
|
|
|
return err
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 16:02:41 +00:00
|
|
|
// We do not block for confirmation checks at deploy
|
2023-07-05 14:40:40 +00:00
|
|
|
cmdio.LogString(ctx, "Starting deployment")
|
|
|
|
|
|
|
|
// Apply terraform according to the plan
|
|
|
|
err = tf.Apply(ctx, tfexec.DirOrPlan(b.Plan.Path))
|
2022-12-09 07:57:30 +00:00
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("terraform apply: %w", err)
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 14:55:06 +00:00
|
|
|
cmdio.LogString(ctx, "Resource deployment completed!")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply returns a [bundle.Mutator] that runs the equivalent of `terraform apply`
|
|
|
|
// from the bundle's ephemeral working directory for Terraform.
|
|
|
|
func Apply() bundle.Mutator {
|
|
|
|
return &apply{}
|
|
|
|
}
|