@@ -125,7 +125,7 @@ func namespaceForceNew(ctx context.Context, d *schema.ResourceDiff, c *Databrick
125125 newEffective = strconv .FormatInt (c .cachedWorkspaceID , 10 )
126126 }
127127
128- // CUJ-14: If a resource has a workspace ID in state but the new effective
128+ // If a resource has a workspace ID in state but the new effective
129129 // workspace ID is empty (user removed workspace_id and there's no
130130 // explicit provider_config), error out. This prevents silently continuing
131131 // to operate against a workspace the user thought they disconnected from.
@@ -244,9 +244,16 @@ func (c *DatabricksClient) DatabricksClientForUnifiedProvider(ctx context.Contex
244244 if ! ok {
245245 return nil , fmt .Errorf ("workspace_id must be a string" )
246246 }
247- // If the workspace_id is not passed in the resource configuration, we don't need to create a new client
248- // and can return the current client .
247+ // If the workspace_id is not passed in the resource configuration,
248+ // fall back to workspace_id for account-level providers .
249249 if workspaceID == "" {
250+ if c .DatabricksClient != nil && c .Config .HostType () == config .AccountHost {
251+ if c .Config .WorkspaceID != "" {
252+ return c .getDatabricksClientForUnifiedProvider (ctx , c .Config .WorkspaceID )
253+ }
254+ return nil , fmt .Errorf ("managing workspace-level resources requires a workspace_id, " +
255+ "but none was found in provider_config or the provider configuration" )
256+ }
250257 return c , nil
251258 }
252259 return c .getDatabricksClientForUnifiedProvider (ctx , workspaceID )
@@ -299,49 +306,6 @@ func workspaceIDFromRawConfig(d *schema.ResourceData) (string, bool) {
299306 return "" , false
300307}
301308
302- // populateProviderConfigInState writes the effective workspace ID into
303- // provider_config in the resource state.
304- //
305- // During refresh reads (terraform plan), the prior state value must be preserved
306- // so that CustomizeDiff can compare the old effective workspace ID against the
307- // new one and trigger ForceNew when they differ. If this hook resolved the "new
308- // effective" workspace ID and wrote it during refresh, it would overwrite the old
309- // value before CustomizeDiff runs, making workspace-change detection impossible.
310- //
311- // Therefore this hook only resolves from provider-level sources (workspace_id,
312- // host) on the first time — when no workspace ID exists in state yet (after Create).
313- // On subsequent reads, it preserves whatever is already in state.
314- func populateProviderConfigInState (ctx context.Context , d * schema.ResourceData , c * DatabricksClient ) error {
315- // If provider_config.workspace_id already exists in state, preserve it.
316- // During refresh reads the state value reflects the workspace the Read
317- // actually targeted. Overwriting it would prevent CustomizeDiff from
318- // detecting workspace changes.
319- if existing := d .Get (workspaceIDSchemaKey ); existing != nil {
320- if existingStr , ok := existing .(string ); ok && existingStr != "" {
321- d .Set ("provider_config" , []map [string ]any {{"workspace_id" : existingStr }})
322- return nil
323- }
324- }
325-
326- // No workspace ID in state yet (first time — after Create/Import).
327- // Resolve from provider config to populate state for the first time:
328- // 1. provider_config.workspace_id from raw config
329- // 2. workspace_id from provider
330- // 3. cachedWorkspaceID (workspace host, primed during provider init)
331- wsID , _ := workspaceIDFromRawConfig (d )
332- if wsID == "" && c .DatabricksClient != nil && c .Config != nil {
333- wsID = c .Config .WorkspaceID
334- }
335- if wsID == "" && c .cachedWorkspaceID != 0 {
336- wsID = strconv .FormatInt (c .cachedWorkspaceID , 10 )
337- }
338-
339- if wsID != "" {
340- d .Set ("provider_config" , []map [string ]any {{"workspace_id" : wsID }})
341- }
342- return nil
343- }
344-
345309// setCachedDatabricksClient sets the cached Databricks Client.
346310func (c * DatabricksClient ) setCachedDatabricksClient (ctx context.Context , workspaceID string ) error {
347311 // Acquire the lock to avoid race conditions.
0 commit comments