Skip to content

Commit 8b3c091

Browse files
authored
direct: Return an error remote state is not populated but should be (#4420)
Also, few internal clean ups & renames.
1 parent 9498145 commit 8b3c091

File tree

7 files changed

+20
-28
lines changed

7 files changed

+20
-28
lines changed

bundle/deployplan/plan.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ const (
102102
ReasonAlias = "alias"
103103
ReasonRemoteAlreadySet = "remote_already_set"
104104
ReasonBuiltinRule = "builtin_rule"
105-
ReasonConfigOnly = "config_only"
106105
ReasonEmptySlice = "empty_slice"
107106
ReasonEmptyMap = "empty_map"
108107
ReasonEmptyStruct = "empty_struct"

bundle/direct/bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
113113

114114
// Populate the state with the resolved config
115115
entry := plan.Plan[resourceKey]
116-
sv, ok := b.StructVarCache.Load(resourceKey)
116+
sv, ok := b.StateCache.Load(resourceKey)
117117
if ok && sv != nil {
118118
var dependsOn []deployplan.DependsOnEntry
119119
if entry != nil {

bundle/direct/bundle_apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
101101
}
102102

103103
// Get the cached StructVar to check for unresolved refs and get value
104-
sv, ok := b.StructVarCache.Load(resourceKey)
104+
sv, ok := b.StateCache.Load(resourceKey)
105105
if !ok {
106106
logdiag.LogError(ctx, fmt.Errorf("%s: internal error: missing cached StructVar", errorPrefix))
107107
return false

bundle/direct/bundle_plan.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (b *DeploymentBundle) InitForApply(ctx context.Context, client *databricks.
102102
return fmt.Errorf("loading plan entry %s: %w", resourceKey, err)
103103
}
104104

105-
b.StructVarCache.Store(resourceKey, sv)
105+
b.StateCache.Store(resourceKey, sv)
106106
}
107107

108108
b.Plan = plan
@@ -218,9 +218,9 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks
218218
// for integers: compare 0 with actual object ID. As long as real object IDs are never 0 we're good.
219219
// Once we add non-id fields or add per-field details to "bundle plan", we must read dynamic data and deal with references as first class citizen.
220220
// This means distinguishing between 0 that are actually object ids and 0 that are there because typed struct integer cannot contain ${...} string.
221-
sv, ok := b.StructVarCache.Load(resourceKey)
221+
sv, ok := b.StateCache.Load(resourceKey)
222222
if !ok {
223-
logdiag.LogError(ctx, fmt.Errorf("%s: internal error: no state found for %q", errorPrefix, resourceKey))
223+
logdiag.LogError(ctx, fmt.Errorf("%s: internal error: no state cache entry found for %q", errorPrefix, resourceKey))
224224
return false
225225
}
226226
localDiff, err := structdiff.GetStructDiff(savedState, sv.Value, adapter.KeyedSlices())
@@ -558,10 +558,9 @@ func (b *DeploymentBundle) LookupReferencePreDeploy(ctx context.Context, path *s
558558
return nil, fmt.Errorf("internal error: %s: action is %q missing new_state", targetResourceKey, targetEntry.Action)
559559
}
560560

561-
// Get StructVar from cache
562-
sv, ok := b.StructVarCache.Load(targetResourceKey)
561+
sv, ok := b.StateCache.Load(targetResourceKey)
563562
if !ok {
564-
return nil, fmt.Errorf("internal error: %s: missing cached StructVar", targetResourceKey)
563+
return nil, fmt.Errorf("internal error: %s: missing state cache entry", targetResourceKey)
565564
}
566565

567566
_, isUnresolved := sv.Refs[fieldPathS]
@@ -604,6 +603,8 @@ func (b *DeploymentBundle) LookupReferencePreDeploy(ctx context.Context, path *s
604603
remoteState, ok := b.RemoteStateCache.Load(targetResourceKey)
605604
if ok {
606605
return structaccess.Get(remoteState, fieldPath)
606+
} else {
607+
return nil, fmt.Errorf("internal error: no entry in remote state cache for %q (remote-only)", targetResourceKey)
607608
}
608609
}
609610
return nil, errDelayed
@@ -621,27 +622,19 @@ func (b *DeploymentBundle) LookupReferencePreDeploy(ctx context.Context, path *s
621622
remoteState, ok := b.RemoteStateCache.Load(targetResourceKey)
622623
if ok {
623624
return structaccess.Get(remoteState, fieldPath)
625+
} else {
626+
return nil, fmt.Errorf("internal error: no entry in remote state cache for %q", targetResourceKey)
624627
}
625628
}
626629

627630
return nil, errDelayed
628631
}
629632

630-
// getStructVar returns the cached StructVar for the given resource key.
631-
// The StructVar must have been eagerly loaded during plan creation or InitForApply.
632-
func (b *DeploymentBundle) getStructVar(resourceKey string) (*structvar.StructVar, error) {
633-
sv, ok := b.StructVarCache.Load(resourceKey)
634-
if !ok {
635-
return nil, fmt.Errorf("internal error: StructVar not found in cache for %s", resourceKey)
636-
}
637-
return sv, nil
638-
}
639-
640633
// resolveReferences processes all references in entry.NewState.Refs.
641-
func (b *DeploymentBundle) resolveReferences(ctx context.Context, resourceKey string, entry *deployplan.PlanEntry, errorPrefix string, isLocal bool) bool {
642-
sv, err := b.getStructVar(resourceKey)
643-
if err != nil {
644-
logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err))
634+
func (b *DeploymentBundle) resolveReferences(ctx context.Context, resourceKey string, entry *deployplan.PlanEntry, errorPrefix string, isPreDeploy bool) bool {
635+
sv, ok := b.StateCache.Load(resourceKey)
636+
if !ok {
637+
logdiag.LogError(ctx, fmt.Errorf("%s: internal error: no cache entry found for %q", errorPrefix, resourceKey))
645638
return false
646639
}
647640

@@ -662,7 +655,7 @@ func (b *DeploymentBundle) resolveReferences(ctx context.Context, resourceKey st
662655
}
663656

664657
var value any
665-
if isLocal {
658+
if isPreDeploy {
666659
value, err = b.LookupReferencePreDeploy(ctx, targetPath)
667660
if err != nil {
668661
if errors.Is(err, errDelayed) {
@@ -853,7 +846,7 @@ func (b *DeploymentBundle) makePlan(ctx context.Context, configRoot *config.Root
853846
}
854847

855848
// Store in cache for use during planning phase
856-
b.StructVarCache.Store(node, newState)
849+
b.StateCache.Store(node, newState)
857850

858851
// Convert to JSON for serialization in plan
859852
newStateJSON, err := newState.ToJSON()

bundle/direct/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type DeploymentBundle struct {
4343
Adapters map[string]*dresources.Adapter
4444
Plan *deployplan.Plan
4545
RemoteStateCache sync.Map
46-
StructVarCache structvar.Cache
46+
StateCache structvar.Cache
4747
}
4848

4949
// SetRemoteState updates the remote state with type validation and marks as fresh.

bundle/statemgmt/upload_state_for_yaml_sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (m *uploadStateForYamlSync) convertState(ctx context.Context, b *bundle.Bun
166166
if etag == "" {
167167
continue
168168
}
169-
sv, ok := deploymentBundle.StructVarCache.Load(key)
169+
sv, ok := deploymentBundle.StateCache.Load(key)
170170
if !ok {
171171
continue
172172
}

cmd/bundle/deployment/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ To start using direct engine, deploy with DATABRICKS_BUNDLE_ENGINE=direct env va
246246
if etag == "" {
247247
continue
248248
}
249-
sv, ok := deploymentBundle.StructVarCache.Load(key)
249+
sv, ok := deploymentBundle.StateCache.Load(key)
250250
if !ok {
251251
return fmt.Errorf("failed to read state for %q", key)
252252
}

0 commit comments

Comments
 (0)