Skip to content

Commit df16914

Browse files
authored
Add GetResourceID to dstate and refactor callers (#4862)
## Changes Adds GetResourceID(key) string returning the resource ID or empty string if not found, replacing the bool-returning GetResourceEntry in all callers that only needed the ID. Renames the implementation to getResourceEntry (unexported); keeps GetResourceEntry public for the one caller that also needs the State field. ## Why Separate write users that only need access to ID and read users that need full state. Preparing for WAL.
1 parent 7500a56 commit df16914

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

bundle/direct/apply.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,23 @@ import (
1414
)
1515

1616
func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState) error {
17-
entry, hasEntry := db.GetResourceEntry(d.ResourceKey)
18-
if !hasEntry {
17+
id := db.GetResourceID(d.ResourceKey)
18+
if id == "" {
1919
log.Infof(ctx, "Cannot delete %s: missing from state", d.ResourceKey)
2020
return nil
2121
}
2222

23-
if entry.ID == "" {
24-
return errors.New("invalid state: empty id")
25-
}
26-
27-
return d.Delete(ctx, db, entry.ID)
23+
return d.Delete(ctx, db, id)
2824
}
2925

3026
func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState, newState any, actionType deployplan.ActionType, planEntry *deployplan.PlanEntry) error {
3127
if actionType == deployplan.Create {
3228
return d.Create(ctx, db, newState)
3329
}
3430

35-
entry, hasEntry := db.GetResourceEntry(d.ResourceKey)
36-
if !hasEntry {
37-
return errors.New("state entry not found")
38-
}
39-
40-
oldID := entry.ID
31+
oldID := db.GetResourceID(d.ResourceKey)
4132
if oldID == "" {
42-
return errors.New("invalid state: empty id")
33+
return errors.New("state entry not found")
4334
}
4435

4536
switch actionType {

bundle/direct/bind.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
6363
// Check if the resource is already managed (bound to a different ID)
6464
var checkStateDB dstate.DeploymentState
6565
if err := checkStateDB.Open(statePath); err == nil {
66-
if existing, ok := checkStateDB.GetResourceEntry(resourceKey); ok {
66+
if existingID := checkStateDB.GetResourceID(resourceKey); existingID != "" {
6767
return nil, ErrResourceAlreadyBound{
6868
ResourceKey: resourceKey,
69-
ExistingID: existing.ID,
69+
ExistingID: existingID,
7070
NewID: resourceID,
7171
}
7272
}

bundle/direct/bundle_apply.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
113113

114114
if migrateMode {
115115
// In migration mode we're reading resources in DAG order so that we have fully resolved config snapshots stored
116-
dbentry, hasEntry := b.StateDB.GetResourceEntry(resourceKey)
117-
if !hasEntry || dbentry.ID == "" {
116+
id := b.StateDB.GetResourceID(resourceKey)
117+
if id == "" {
118118
logdiag.LogError(ctx, fmt.Errorf("state entry not found for %q", resourceKey))
119119
return false
120120
}
121-
err = b.StateDB.SaveState(resourceKey, dbentry.ID, sv.Value, entry.DependsOn)
121+
err = b.StateDB.SaveState(resourceKey, id, sv.Value, entry.DependsOn)
122122
} else {
123123
// TODO: redo calcDiff to downgrade planned action if possible (?)
124124
err = d.Deploy(ctx, &b.StateDB, sv.Value, action, entry)
@@ -135,13 +135,13 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
135135
// already resolved and should not play a role here.
136136
needRemoteState := len(g.Adj[resourceKey]) > 0
137137
if needRemoteState {
138-
entry, _ := b.StateDB.GetResourceEntry(d.ResourceKey)
139-
if entry.ID == "" {
138+
id := b.StateDB.GetResourceID(d.ResourceKey)
139+
if id == "" {
140140
logdiag.LogError(ctx, fmt.Errorf("%s: internal error: missing entry in state after deploy", errorPrefix))
141141
return false
142142
}
143143

144-
err = d.refreshRemoteState(ctx, entry.ID)
144+
err = d.refreshRemoteState(ctx, id)
145145
if err != nil {
146146
logdiag.LogError(ctx, fmt.Errorf("%s: failed to read remote state: %w", errorPrefix, err))
147147
return false
@@ -180,11 +180,11 @@ func (b *DeploymentBundle) LookupReferencePostDeploy(ctx context.Context, path *
180180
}
181181

182182
if fieldPathS == "id" {
183-
dbentry, hasEntry := b.StateDB.GetResourceEntry(targetResourceKey)
184-
if !hasEntry || dbentry.ID == "" {
183+
id := b.StateDB.GetResourceID(targetResourceKey)
184+
if id == "" {
185185
return nil, errors.New("internal error: no db entry")
186186
}
187-
return dbentry.ID, nil
187+
return id, nil
188188
}
189189

190190
remoteState, ok := b.RemoteStateCache.Load(targetResourceKey)

bundle/direct/bundle_plan.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,19 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks
168168
}
169169

170170
if entry.Action == deployplan.Delete {
171-
dbentry, hasEntry := b.StateDB.GetResourceEntry(resourceKey)
172-
if !hasEntry {
171+
id := b.StateDB.GetResourceID(resourceKey)
172+
if id == "" {
173173
logdiag.LogError(ctx, fmt.Errorf("%s: internal error, missing in state", errorPrefix))
174174
return false
175175
}
176176

177-
remoteState, err := adapter.DoRead(ctx, dbentry.ID)
177+
remoteState, err := adapter.DoRead(ctx, id)
178178
if err != nil {
179179
if isResourceGone(err) {
180180
// no such resource
181181
plan.RemoveEntry(resourceKey)
182182
} else {
183-
log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, dbentry.ID, err)
183+
log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, err)
184184
// This is not an error during deletion, so don't return false here
185185
}
186186
}
@@ -605,12 +605,11 @@ func (b *DeploymentBundle) LookupReferencePreDeploy(ctx context.Context, path *s
605605

606606
if fieldPathS == "id" {
607607
if targetAction.KeepsID() {
608-
dbentry, hasEntry := b.StateDB.GetResourceEntry(targetResourceKey)
609-
idValue := dbentry.ID
610-
if !hasEntry || idValue == "" {
608+
id := b.StateDB.GetResourceID(targetResourceKey)
609+
if id == "" {
611610
return nil, errors.New("internal error: no db entry")
612611
}
613-
return idValue, nil
612+
return id, nil
614613
}
615614
// id may change after deployment, this needs to be done later
616615
return nil, errDelayed

bundle/direct/dstate/state.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (db *DeploymentState) DeleteState(key string) error {
8484
return nil
8585
}
8686

87-
func (db *DeploymentState) GetResourceEntry(key string) (ResourceEntry, bool) {
87+
func (db *DeploymentState) getResourceEntry(key string) (ResourceEntry, bool) {
8888
db.AssertOpened()
8989
db.mu.Lock()
9090
defer db.mu.Unlock()
@@ -97,6 +97,17 @@ func (db *DeploymentState) GetResourceEntry(key string) (ResourceEntry, bool) {
9797
return result, ok
9898
}
9999

100+
// GetResourceEntry returns the full resource entry for the given key.
101+
func (db *DeploymentState) GetResourceEntry(key string) (ResourceEntry, bool) {
102+
return db.getResourceEntry(key)
103+
}
104+
105+
// GetResourceID returns the ID of the resource for the given key, or an empty string if not found.
106+
func (db *DeploymentState) GetResourceID(key string) string {
107+
entry, _ := db.getResourceEntry(key)
108+
return entry.ID
109+
}
110+
100111
func (db *DeploymentState) Open(path string) error {
101112
db.mu.Lock()
102113
defer db.mu.Unlock()

0 commit comments

Comments
 (0)