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

     1  package terraform
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/databricks/cli/bundle"
     9  	"github.com/databricks/cli/libs/filer"
    10  	"github.com/databricks/cli/libs/log"
    11  )
    12  
    13  type statePush struct{}
    14  
    15  func (l *statePush) Name() string {
    16  	return "terraform:state-push"
    17  }
    18  
    19  func (l *statePush) Apply(ctx context.Context, b *bundle.Bundle) error {
    20  	f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(), b.Config.Workspace.StatePath)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	dir, err := Dir(b)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	// Expect the state file to live under dir.
    31  	local, err := os.Open(filepath.Join(dir, TerraformStateFileName))
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	// Upload state file from local cache directory to filer.
    37  	log.Infof(ctx, "Writing local state file to remote state directory")
    38  	err = f.Write(ctx, TerraformStateFileName, local, filer.CreateParentDirectories, filer.OverwriteIfExists)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  func StatePush() bundle.Mutator {
    47  	return &statePush{}
    48  }