From 153510bfbf0035ce32f866cab9c5b5f7c9044b88 Mon Sep 17 00:00:00 2001 From: Anders Rex Date: Thu, 18 Jul 2024 10:04:16 +0300 Subject: [PATCH] Add workspace host mismatch check --- .../config/mutator/validate_workspace_host.go | 30 +++++++++++++++++++ bundle/env/host.go | 11 +++++++ bundle/phases/deploy.go | 1 + 3 files changed, 42 insertions(+) create mode 100644 bundle/config/mutator/validate_workspace_host.go create mode 100644 bundle/env/host.go diff --git a/bundle/config/mutator/validate_workspace_host.go b/bundle/config/mutator/validate_workspace_host.go new file mode 100644 index 000000000..3ffb27b98 --- /dev/null +++ b/bundle/config/mutator/validate_workspace_host.go @@ -0,0 +1,30 @@ +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 +} diff --git a/bundle/env/host.go b/bundle/env/host.go new file mode 100644 index 000000000..859072a60 --- /dev/null +++ b/bundle/env/host.go @@ -0,0 +1,11 @@ +package env + +import "context" + +const HostVariable = "DATABRICKS_HOST" + +func Host(ctx context.Context) (string, bool) { + return get(ctx, []string{ + HostVariable, + }) +} diff --git a/bundle/phases/deploy.go b/bundle/phases/deploy.go index 46c389189..e30a69c91 100644 --- a/bundle/phases/deploy.go +++ b/bundle/phases/deploy.go @@ -25,6 +25,7 @@ func Deploy() bundle.Mutator { bundle.Seq( terraform.StatePull(), deploy.StatePull(), + mutator.ValidateWorkspaceHost(), mutator.ValidateGitDetails(), libraries.ValidateLocalLibrariesExist(), artifacts.CleanUp(),