2022-12-09 07:57:30 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-10-10 11:18:23 +00:00
|
|
|
"github.com/databricks/cli/bundle/permissions"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-12-21 08:00:37 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
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"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-07-31 14:07:25 +00:00
|
|
|
// return early if plan is empty
|
|
|
|
if b.Plan.IsEmpty {
|
|
|
|
log.Debugf(ctx, "No changes in plan. Skipping terraform apply.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:30:33 +00:00
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform not initialized")
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 14:07:25 +00:00
|
|
|
if b.Plan.Path == "" {
|
|
|
|
return diag.Errorf("no plan found")
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 14:07:25 +00:00
|
|
|
// Apply terraform according to the computed plan
|
|
|
|
err := tf.Apply(ctx, tfexec.DirOrPlan(b.Plan.Path))
|
2022-12-09 07:57:30 +00:00
|
|
|
if err != nil {
|
2024-10-10 11:18:23 +00:00
|
|
|
diags := permissions.TryExtendTerraformPermissionError(ctx, b, err)
|
|
|
|
if diags != nil {
|
|
|
|
return diags
|
|
|
|
}
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform apply: %v", err)
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 14:07:25 +00:00
|
|
|
log.Infof(ctx, "terraform apply completed")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-12-09 07:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 14:07:25 +00:00
|
|
|
// Apply returns a [bundle.Mutator] that runs the equivalent of `terraform apply ./plan`
|
2022-12-09 07:57:30 +00:00
|
|
|
// from the bundle's ephemeral working directory for Terraform.
|
|
|
|
func Apply() bundle.Mutator {
|
|
|
|
return &apply{}
|
|
|
|
}
|