2022-12-15 14:12:47 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-30 10:01:09 +00:00
|
|
|
"fmt"
|
2022-12-15 14:12:47 +00:00
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle"
|
2023-03-30 10:01:09 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
2022-12-15 14:12:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type load struct{}
|
|
|
|
|
|
|
|
func (l *load) Name() string {
|
|
|
|
return "terraform.Load"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *load) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
2023-03-30 10:01:09 +00:00
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
|
|
|
return nil, fmt.Errorf("terraform not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := tf.Init(ctx, tfexec.Upgrade(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("terraform init: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
state, err := b.Terraform.Show(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge state into configuration.
|
|
|
|
err = TerraformToBundle(state, &b.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load() bundle.Mutator {
|
|
|
|
return &load{}
|
|
|
|
}
|