Display plan on bundle deploy

This commit is contained in:
Shreyas Goenka 2023-07-05 16:40:40 +02:00
parent 335475095e
commit e87edd7435
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 67 additions and 6 deletions

View File

@ -16,19 +16,49 @@ func (w *apply) Name() string {
} }
func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) error { func (w *apply) Apply(ctx context.Context, b *bundle.Bundle) error {
// 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 tf := b.Terraform
if tf == nil { if tf == nil {
return fmt.Errorf("terraform not initialized") return fmt.Errorf("terraform not initialized")
} }
cmdio.LogString(ctx, "Starting resource deployment") // Error if plan is missing
if b.Plan.Path == "" {
err := tf.Init(ctx, tfexec.Upgrade(true)) return fmt.Errorf("no plan found")
if err != nil {
return fmt.Errorf("terraform init: %w", err)
} }
err = tf.Apply(ctx) // 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 {
return err
}
// 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
}
cmdio.LogString(ctx, "Starting deployment")
// Apply terraform according to the plan
err = tf.Apply(ctx, tfexec.DirOrPlan(b.Plan.Path))
if err != nil { if err != nil {
return fmt.Errorf("terraform apply: %w", err) return fmt.Errorf("terraform apply: %w", err)
} }

View File

@ -9,8 +9,38 @@ import (
"github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/terraform" "github.com/databricks/cli/libs/terraform"
"github.com/hashicorp/terraform-exec/tfexec" "github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
) )
func logPlan(ctx context.Context, plan *tfjson.Plan) error {
for _, change := range plan.ResourceChanges {
tfActions := change.Change.Actions
if tfActions.Read() || tfActions.NoOp() {
continue
}
var action string
switch {
case tfActions.Update():
action = "update"
case tfActions.Create():
action = "create"
case tfActions.Delete():
action = "delete"
case tfActions.Replace():
action = "replace"
default:
fmt.Errorf("unknown terraform actions: %s", tfActions)
}
err := cmdio.RenderWithTemplate(ctx, change, fmt.Sprintf("%s {{.Type}} {{.Name}}\n", action))
if err != nil {
return err
}
}
return nil
}
type PlanGoal string type PlanGoal string
var ( var (

View File

@ -19,6 +19,7 @@ func Deploy() bundle.Mutator {
terraform.Interpolate(), terraform.Interpolate(),
terraform.Write(), terraform.Write(),
terraform.StatePull(), terraform.StatePull(),
terraform.Plan(terraform.PlanGoal("deploy")),
terraform.Apply(), terraform.Apply(),
terraform.StatePush(), terraform.StatePush(),
), ),