2023-03-30 10:01:09 +00:00
package terraform
import (
"context"
2024-07-31 14:07:25 +00:00
"errors"
"io/fs"
2023-03-30 10:01:09 +00:00
"os"
"path/filepath"
2023-05-16 16:35:39 +00:00
"github.com/databricks/cli/bundle"
2024-03-18 14:41:58 +00:00
"github.com/databricks/cli/bundle/deploy"
2023-12-21 08:00:37 +00:00
"github.com/databricks/cli/libs/cmdio"
2024-03-25 14:18:47 +00:00
"github.com/databricks/cli/libs/diag"
2023-05-16 16:35:39 +00:00
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/log"
2023-03-30 10:01:09 +00:00
)
2023-11-24 11:15:46 +00:00
type statePush struct {
2024-03-18 14:41:58 +00:00
filerFactory deploy . FilerFactory
2023-11-24 11:15:46 +00:00
}
2023-03-30 10:01:09 +00:00
func ( l * statePush ) Name ( ) string {
return "terraform:state-push"
}
2024-03-25 14:18:47 +00:00
func ( l * statePush ) Apply ( ctx context . Context , b * bundle . Bundle ) diag . Diagnostics {
2024-03-18 14:41:58 +00:00
f , err := l . filerFactory ( b )
2023-03-30 10:01:09 +00:00
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( err )
2023-03-30 10:01:09 +00:00
}
2023-09-11 08:18:43 +00:00
dir , err := Dir ( ctx , b )
2023-03-30 10:01:09 +00:00
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( 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 ) )
2024-07-31 14:07:25 +00:00
if errors . Is ( err , fs . ErrNotExist ) {
// The state file can be absent if terraform apply is skipped because
// there are no changes to apply in the plan.
log . Debugf ( ctx , "Local terraform state file does not exist." )
return nil
}
2023-03-30 10:01:09 +00:00
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( err )
2023-03-30 10:01:09 +00:00
}
2023-09-08 10:47:17 +00:00
defer local . Close ( )
2023-03-30 10:01:09 +00:00
2024-10-02 13:53:24 +00:00
if ! b . Config . Bundle . Force {
state , err := local . Stat ( )
if err != nil {
return diag . FromErr ( err )
}
if state . Size ( ) > deploy . MaxStateFileSize {
return diag . Errorf ( "Terraform state file size exceeds the maximum allowed size of %d bytes. Please reduce the number of resources in your bundle, split your bundle into multiple or re-run the command with --force flag" , deploy . MaxStateFileSize )
}
}
2023-03-30 10:01:09 +00:00
// Upload state file from local cache directory to filer.
2023-12-21 08:00:37 +00:00
cmdio . LogString ( ctx , "Updating deployment state..." )
2023-03-30 10:01:09 +00:00
log . Infof ( ctx , "Writing local state file to remote state directory" )
err = f . Write ( ctx , TerraformStateFileName , local , filer . CreateParentDirectories , filer . OverwriteIfExists )
if err != nil {
2024-03-25 14:18:47 +00:00
return diag . FromErr ( 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 {
2024-03-18 14:41:58 +00:00
return & statePush { deploy . StateFiler }
2023-03-30 10:01:09 +00:00
}