2024-02-14 18:04:45 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-02-14 18:04:45 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type unbind struct {
|
|
|
|
resourceType string
|
|
|
|
resourceKey string
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *unbind) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-02-14 18:04:45 +00:00
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform not initialized")
|
2024-02-14 18:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := tf.Init(ctx, tfexec.Upgrade(true))
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform init: %v", err)
|
2024-02-14 18:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tf.StateRm(ctx, fmt.Sprintf("%s.%s", m.resourceType, m.resourceKey))
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform state rm: %v", err)
|
2024-02-14 18:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*unbind) Name() string {
|
|
|
|
return "terraform.Unbind"
|
|
|
|
}
|
|
|
|
|
|
|
|
func Unbind(resourceType string, resourceKey string) bundle.Mutator {
|
|
|
|
return &unbind{resourceType: resourceType, resourceKey: resourceKey}
|
|
|
|
}
|