github.com/databricks/cli@v0.203.0/bundle/deploy/terraform/load.go (about)

     1  package terraform
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/databricks/cli/bundle"
     8  	"github.com/hashicorp/terraform-exec/tfexec"
     9  	tfjson "github.com/hashicorp/terraform-json"
    10  )
    11  
    12  type load struct{}
    13  
    14  func (l *load) Name() string {
    15  	return "terraform.Load"
    16  }
    17  
    18  func (l *load) Apply(ctx context.Context, b *bundle.Bundle) error {
    19  	tf := b.Terraform
    20  	if tf == nil {
    21  		return fmt.Errorf("terraform not initialized")
    22  	}
    23  
    24  	err := tf.Init(ctx, tfexec.Upgrade(true))
    25  	if err != nil {
    26  		return fmt.Errorf("terraform init: %w", err)
    27  	}
    28  
    29  	state, err := b.Terraform.Show(ctx)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	err = ValidateState(state)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	// Merge state into configuration.
    40  	err = TerraformToBundle(state, &b.Config)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func ValidateState(state *tfjson.State) error {
    49  	if state.Values == nil {
    50  		return fmt.Errorf("no deployment state. Did you forget to run 'databricks bundle deploy'?")
    51  	}
    52  
    53  	if state.Values.RootModule == nil {
    54  		return fmt.Errorf("malformed terraform state: RootModule not set")
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func Load() bundle.Mutator {
    61  	return &load{}
    62  }