mirror of https://github.com/databricks/cli.git
Check for host mismatch when initializing workspace client
This commit is contained in:
parent
feb77686b5
commit
e403630e4d
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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(),
|
||||||
|
|
Loading…
Reference in New Issue