Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bundle/direct/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
// The state is opened without a DMS client, so the writes below record nothing;
// phases.Bind and phases.Unbind refuse to run at all when recording is enabled.
var checkStateDB dstate.DeploymentState
if err := checkStateDB.Open(ctx, statePath, dstate.WithRecovery(true), dstate.WithWrite(false), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{}); err != nil {
if err := checkStateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil {
// State that cannot be read is not the same as state without a binding:
// the resource may well be managed already, and binding on top of it
// would take over a resource whose ownership was never checked. A state
Expand Down Expand Up @@ -95,7 +95,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
}

// Open temp state
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(false), dstate.WithWrite(true), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{})
if err != nil {
os.Remove(tmpStatePath)
return nil, err
Expand All @@ -118,7 +118,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
log.Infof(ctx, "Bound %s to id=%s (in temp state)", resourceKey, resourceID)

// First plan + update: populate state with resolved config
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), dstate.WithWrite(false), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
if err != nil {
os.Remove(tmpStatePath)
return nil, err
Expand Down Expand Up @@ -167,7 +167,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
return nil, fmt.Errorf("compacting state: %w", err)
}

err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), dstate.WithWrite(true), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
if err != nil {
os.Remove(tmpStatePath)
return nil, err
Expand All @@ -187,7 +187,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
}

// Second plan: this is the plan to present to the user (change between remote resource and config)
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), dstate.WithWrite(false), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
if err != nil {
os.Remove(tmpStatePath)
return nil, err
Expand Down Expand Up @@ -237,7 +237,7 @@ func (result *BindResult) Cancel() {
// Unbind removes a resource from direct engine state without deleting
// the workspace resource. Also removes associated permissions/grants entries.
func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error {
err := b.StateDB.Open(ctx, statePath, dstate.WithRecovery(true), dstate.WithWrite(true), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
err := b.StateDB.Open(ctx, statePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
if err != nil {
if errors.Is(err, dstate.ErrUnsettingRecording) {
return errors.New("unbind is not supported for a bundle target that records deployment history")
Expand Down
45 changes: 45 additions & 0 deletions bundle/direct/dstate/mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dstate

import "fmt"

// Mode selects the source of resource state and the persistence required for writes.
type Mode int

const (
ReadingFile Mode = iota
WritingFile
ReadingDMS
WritingDMS
WritingFileWithHistory
WritingFileWithHistoryEnforced
)

func (m Mode) isWritable() bool {
switch m {
case WritingFile, WritingDMS, WritingFileWithHistory, WritingFileWithHistoryEnforced:
return true
default:
return false
}
}

func (m Mode) usesDMSState() bool {
return m == ReadingDMS || m == WritingDMS
}

func (m Mode) isFileWithHistory() bool {
return m == WritingFileWithHistory || m == WritingFileWithHistoryEnforced
}

func (m Mode) recordsHistory() bool {
return m.usesDMSState() || m.isFileWithHistory()
}

func (m Mode) validate() error {
switch m {
case ReadingFile, WritingFile, ReadingDMS, WritingDMS, WritingFileWithHistory, WritingFileWithHistoryEnforced:
return nil
default:
return fmt.Errorf("invalid deployment state mode: %d", m)
}
}
Loading
Loading