Check for host mismatch when initializing workspace client

This commit is contained in:
Anders Rex 2024-07-18 13:39:19 +03:00
parent feb77686b5
commit e403630e4d
No known key found for this signature in database
GPG Key ID: 06A88681C237F740
4 changed files with 21 additions and 36 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/env"
"github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/diag"
) )
@ -20,7 +21,22 @@ func (m *initializeWorkspaceClient) Name() string {
// Apply initializes the workspace client for the bundle. We do this here so // Apply initializes the workspace client for the bundle. We do this here so
// downstream calls to b.WorkspaceClient() do not panic if there's an error in the // downstream calls to b.WorkspaceClient() do not panic if there's an error in the
// auth configuration. // auth configuration.
func (m *initializeWorkspaceClient) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics { func (m *initializeWorkspaceClient) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
if err := validateWorkspaceHost(ctx, b); err != nil {
return err
}
_, err := b.InitializeWorkspaceClient() _, err := b.InitializeWorkspaceClient()
return diag.FromErr(err) return diag.FromErr(err)
} }
func validateWorkspaceHost(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
env_host, _ := env.Host(ctx)
target_host := b.Config.Workspace.Host
if env_host != "" && target_host != "" && env_host != target_host {
return diag.Errorf("target host and DATABRICKS_HOST environment variable mismatch")
}
return nil
}

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestValidateValidateWorkspaceHost(t *testing.T) { func TestInitializeWorkspaceClient(t *testing.T) {
host := "https://host.databricks.com" host := "https://host.databricks.com"
b := &bundle.Bundle{ b := &bundle.Bundle{
Config: config.Root{ Config: config.Root{
@ -21,12 +21,12 @@ func TestValidateValidateWorkspaceHost(t *testing.T) {
} }
t.Setenv(env.HostVariable, host) t.Setenv(env.HostVariable, host)
m := ValidateWorkspaceHost() m := InitializeWorkspaceClient()
diags := bundle.Apply(context.Background(), b, m) diags := bundle.Apply(context.Background(), b, m)
assert.NoError(t, diags.Error()) assert.NoError(t, diags.Error())
} }
func TestValidateValidateWorkspaceHostMismatch(t *testing.T) { func TestInitializeWorkspaceClientWorkspaceHostMismatch(t *testing.T) {
b := &bundle.Bundle{ b := &bundle.Bundle{
Config: config.Root{ Config: config.Root{
Workspace: config.Workspace{ Workspace: config.Workspace{
@ -36,7 +36,7 @@ func TestValidateValidateWorkspaceHostMismatch(t *testing.T) {
} }
t.Setenv(env.HostVariable, "https://env-host.databricks.com") t.Setenv(env.HostVariable, "https://env-host.databricks.com")
m := ValidateWorkspaceHost() m := InitializeWorkspaceClient()
diags := bundle.Apply(context.Background(), b, m) diags := bundle.Apply(context.Background(), b, m)
expectedError := "target host and DATABRICKS_HOST environment variable mismatch" expectedError := "target host and DATABRICKS_HOST environment variable mismatch"
assert.EqualError(t, diags.Error(), expectedError) assert.EqualError(t, diags.Error(), expectedError)

View File

@ -1,30 +0,0 @@
package mutator
import (
"context"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/env"
"github.com/databricks/cli/libs/diag"
)
type validateWorkspaceHost struct{}
func ValidateWorkspaceHost() *validateWorkspaceHost {
return &validateWorkspaceHost{}
}
func (m *validateWorkspaceHost) Name() string {
return "ValidateWorkspaceHost"
}
func (m *validateWorkspaceHost) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
env_host, _ := env.Host(ctx)
target_host := b.Config.Workspace.Host
if env_host != "" && target_host != "" && env_host != target_host {
return diag.Errorf("target host and DATABRICKS_HOST environment variable mismatch")
}
return nil
}

View File

@ -25,7 +25,6 @@ func Deploy() bundle.Mutator {
bundle.Seq( bundle.Seq(
terraform.StatePull(), terraform.StatePull(),
deploy.StatePull(), deploy.StatePull(),
mutator.ValidateWorkspaceHost(),
mutator.ValidateGitDetails(), mutator.ValidateGitDetails(),
libraries.ValidateLocalLibrariesExist(), libraries.ValidateLocalLibrariesExist(),
artifacts.CleanUp(), artifacts.CleanUp(),