Revert "set client during initialization"

This reverts commit 8cc50c2a19.
This commit is contained in:
Shreyas Goenka 2025-01-21 16:43:20 +01:00
parent 79a52dc592
commit c07f1370d2
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
5 changed files with 12 additions and 16 deletions

View File

@ -143,14 +143,20 @@ func (b *Bundle) InitializeWorkspaceClient() (*databricks.WorkspaceClient, error
} }
func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient { func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient {
if b.client == nil { b.clientOnce.Do(func() {
panic("workspace client not initialized yet. This is a bug in the Databricks CLI.") var err error
} b.client, err = b.InitializeWorkspaceClient()
if err != nil {
panic(err)
}
})
return b.client return b.client
} }
// SetWorkpaceClient sets the workspace client for this bundle. // SetWorkpaceClient sets the workspace client for this bundle.
// This is used to inject a mock client for testing.
func (b *Bundle) SetWorkpaceClient(w *databricks.WorkspaceClient) { func (b *Bundle) SetWorkpaceClient(w *databricks.WorkspaceClient) {
b.clientOnce.Do(func() {})
b.client = w b.client = w
} }

View File

@ -48,10 +48,7 @@ func TestInitEnvironmentVariables(t *testing.T) {
// TODO(pietern): create test fixture that initializes a mocked client. // TODO(pietern): create test fixture that initializes a mocked client.
t.Setenv("DATABRICKS_HOST", "https://x") t.Setenv("DATABRICKS_HOST", "https://x")
t.Setenv("DATABRICKS_TOKEN", "foobar") t.Setenv("DATABRICKS_TOKEN", "foobar")
b.WorkspaceClient()
client, err := b.InitializeWorkspaceClient()
require.NoError(t, err)
b.SetWorkpaceClient(client)
diags := bundle.Apply(context.Background(), b, Initialize()) diags := bundle.Apply(context.Background(), b, Initialize())
require.NoError(t, diags.Error()) require.NoError(t, diags.Error())

View File

@ -30,10 +30,7 @@ func TestLoadWithNoState(t *testing.T) {
t.Setenv("DATABRICKS_HOST", "https://x") t.Setenv("DATABRICKS_HOST", "https://x")
t.Setenv("DATABRICKS_TOKEN", "foobar") t.Setenv("DATABRICKS_TOKEN", "foobar")
b.WorkspaceClient()
client, err := b.InitializeWorkspaceClient()
require.NoError(t, err)
b.SetWorkpaceClient(client)
diags := bundle.Apply(context.Background(), b, bundle.Seq( diags := bundle.Apply(context.Background(), b, bundle.Seq(
Initialize(), Initialize(),

View File

@ -93,7 +93,6 @@ func configureBundle(cmd *cobra.Command, b *bundle.Bundle) (*bundle.Bundle, diag
} }
ctx = context.WithValue(ctx, &configUsed, client.Config) ctx = context.WithValue(ctx, &configUsed, client.Config)
cmd.SetContext(ctx) cmd.SetContext(ctx)
b.SetWorkpaceClient(client)
return b, diags return b, diags
} }

View File

@ -91,10 +91,7 @@ func assertBuiltinTemplateValid(t *testing.T, template string, settings map[stri
}) })
b.Tagging = tags.ForCloud(w.Config) b.Tagging = tags.ForCloud(w.Config)
b.WorkspaceClient()
client, err := b.InitializeWorkspaceClient()
require.NoError(t, err)
b.SetWorkpaceClient(client)
diags = bundle.Apply(ctx, b, bundle.Seq( diags = bundle.Apply(ctx, b, bundle.Seq(
phases.Initialize(), phases.Initialize(),