2023-03-30 10:01:09 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-03-30 10:01:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type statePush struct{}
|
|
|
|
|
|
|
|
func (l *statePush) Name() string {
|
|
|
|
return "terraform:state-push"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (l *statePush) Apply(ctx context.Context, b *bundle.Bundle) error {
|
2023-04-12 14:54:36 +00:00
|
|
|
f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(), b.Config.Workspace.StatePath)
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := Dir(b)
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expect the state file to live under dir.
|
|
|
|
local, err := os.Open(filepath.Join(dir, TerraformStateFileName))
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload state file from local cache directory to filer.
|
|
|
|
log.Infof(ctx, "Writing local state file to remote state directory")
|
|
|
|
err = f.Write(ctx, TerraformStateFileName, local, filer.CreateParentDirectories, filer.OverwriteIfExists)
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StatePush() bundle.Mutator {
|
|
|
|
return &statePush{}
|
|
|
|
}
|