Skip to content

Commit 3ce9a13

Browse files
committed
added support for SDKv2 resources which do not use go sdk
1 parent bdec6fc commit 3ce9a13

File tree

5 files changed

+681
-92
lines changed

5 files changed

+681
-92
lines changed

common/resource.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/unified_provider.go

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
346310
func (c *DatabricksClient) setCachedDatabricksClient(ctx context.Context, workspaceID string) error {
347311
// Acquire the lock to avoid race conditions.

common/unified_provider_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func TestDatabricksClientForUnifiedProvider(t *testing.T) {
556556
description: "When workspace_id is invalid, should return error",
557557
},
558558
{
559-
name: "account level provider without workspace_id - returns current client",
559+
name: "account level provider without workspace_id - returns error",
560560
resourceData: map[string]interface{}{
561561
"name": "test",
562562
},
@@ -569,9 +569,9 @@ func TestDatabricksClientForUnifiedProvider(t *testing.T) {
569569
},
570570
},
571571
},
572-
expectError: false,
573-
expectSameClient: true,
574-
description: "Account-level provider without workspace_id should return current client",
572+
expectError: true,
573+
errorContains: "managing workspace-level resources requires a workspace_id",
574+
description: "Account-level provider without workspace_id should return error",
575575
},
576576
{
577577
name: "workspace_id mismatch - returns error",

0 commit comments

Comments
 (0)