Skip to content
Merged
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
2 changes: 2 additions & 0 deletions common/unified_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (c *DatabricksClient) getDatabricksClientForUnifiedProvider(ctx context.Con
if client, ok := c.cachedDatabricksClients[workspaceIDInt]; ok && client != nil {
return &DatabricksClient{
DatabricksClient: client,
commandFactory: c.commandFactory,
}, nil
}
}
Expand All @@ -204,6 +205,7 @@ func (c *DatabricksClient) getDatabricksClientForUnifiedProvider(ctx context.Con
// Return the Databricks Client.
return &DatabricksClient{
DatabricksClient: c.cachedDatabricksClients[workspaceIDInt],
commandFactory: c.commandFactory,
}, nil
}

Expand Down
79 changes: 79 additions & 0 deletions common/unified_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,82 @@ func TestNamespaceCustomizeDiff_ForceNewOnChange(t *testing.T) {
require.True(t, ok, "workspace_id should be in diff attributes")
assert.True(t, wsAttr.RequiresNew, "changing workspace_id should require new resource")
}

func TestGetDatabricksClientForUnifiedProvider_CopiesCommandFactory(t *testing.T) {
// Setup: parent client with commandFactory set via WithCommandMock
parentClient := &DatabricksClient{
DatabricksClient: &client.DatabricksClient{
Config: &config.Config{
Host: "https://test.cloud.databricks.com",
Token: "test-token",
},
},
}
parentClient.WithCommandMock(func(commandStr string) CommandResults {
return CommandResults{ResultType: "text", Data: "mock"}
})

// Verify parent's CommandExecutor works
assert.NotPanics(t, func() {
parentClient.CommandExecutor(context.Background())
}, "parent client should have a working CommandExecutor")

// Pre-cache an inner client for workspace ID 123456
innerClient := &client.DatabricksClient{
Config: &config.Config{
Host: "https://workspace-123456.cloud.databricks.com",
Token: "test-token",
},
}
parentClient.cachedDatabricksClients = map[int64]*client.DatabricksClient{
123456: innerClient,
}

// Call getDatabricksClientForUnifiedProvider — returned client must have
// commandFactory copied from the parent so CommandExecutor works.
result, err := parentClient.getDatabricksClientForUnifiedProvider(
context.Background(), "123456",
)
require.NoError(t, err)
require.NotNil(t, result)
assert.NotPanics(t, func() {
result.CommandExecutor(context.Background())
}, "returned client should have commandFactory copied from parent")

// Also verify via the public entry point DatabricksClientForUnifiedProvider
testSchema := map[string]*schema.Schema{
"provider_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"workspace_id": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
},
}
d := schema.TestResourceDataRaw(t, testSchema, map[string]any{
"name": "test",
"provider_config": []any{
map[string]any{
"workspace_id": "123456",
},
},
})
result2, err := parentClient.DatabricksClientForUnifiedProvider(
context.Background(), d,
)
require.NoError(t, err)
require.NotNil(t, result2)
assert.NotPanics(t, func() {
result2.CommandExecutor(context.Background())
}, "client from DatabricksClientForUnifiedProvider should have commandFactory")
}
44 changes: 44 additions & 0 deletions storage/mounts_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,50 @@ func TestAccCreateDatabricksMount(t *testing.T) {
})
}

// TestAccCreateDatabricksMountWithProviderConfig creates a mount using
// provider_config { workspace_id } pointing at the same workspace the provider
// is configured for. This exercises getDatabricksClientForUnifiedProvider which
// creates a new DatabricksClient — verifying that commandFactory (needed by
// mount's CommandExecutor) is correctly copied to the new client.
func TestAccCreateDatabricksMountWithProviderConfig(t *testing.T) {
workspaceID := acceptance.GetEnvOrSkipTest(t, "THIS_WORKSPACE_ID")
mountHclWithProviderConfig := `
data "databricks_spark_version" "latest" {}

resource "databricks_cluster" "this" {
cluster_name = "acc-test-mounts-{var.STICKY_RANDOM}"
spark_version = data.databricks_spark_version.latest.id
instance_pool_id = "{env.TEST_INSTANCE_POOL_ID}"
num_workers = 1

aws_attributes {
instance_profile_arn = "{env.TEST_INSTANCE_PROFILE_ARN}"
}

provider_config {
workspace_id = "` + workspaceID + `"
}
}

resource "databricks_mount" "my_mount" {
name = "test-mount-pc-{var.STICKY_RANDOM}"
cluster_id = databricks_cluster.this.id

s3 {
bucket_name = "{env.TEST_S3_BUCKET_NAME}"
}

provider_config {
workspace_id = "` + workspaceID + `"
}
}`

acceptance.WorkspaceLevel(t,
acceptance.Step{
Template: mountHclWithProviderConfig,
})
}

func TestAccCreateDatabricksMountIsFineOnClusterRecreate(t *testing.T) {
clusterId1 := ""
clusterId2 := ""
Expand Down
Loading