diff --git a/bundle/bundle.go b/bundle/bundle.go index 5c74c541..69a2f6a4 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -3,13 +3,20 @@ package bundle import ( "context" "path/filepath" + "sync" "github.com/databricks/bricks/bundle/config" "github.com/databricks/bricks/bundle/config/mutator" + "github.com/databricks/databricks-sdk-go/workspaces" ) type Bundle struct { Config config.Root + + // Store a pointer to the workspace client. + // It can be initialized on demand after loading the configuration. + clientOnce sync.Once + client *workspaces.WorkspacesClient } func (b *Bundle) MutateForEnvironment(env string) error { @@ -51,3 +58,10 @@ func ConfigureForEnvironment(ctx context.Context, env string) (context.Context, return Context(ctx, b), nil } + +func (b *Bundle) WorkspaceClient() *workspaces.WorkspacesClient { + b.clientOnce.Do(func() { + b.client = b.Config.Workspace.Client() + }) + return b.client +} diff --git a/bundle/config/workspace.go b/bundle/config/workspace.go index 7e777db7..0da2acbb 100644 --- a/bundle/config/workspace.go +++ b/bundle/config/workspace.go @@ -1,8 +1,53 @@ package config +import ( + "github.com/databricks/databricks-sdk-go/databricks" + "github.com/databricks/databricks-sdk-go/workspaces" +) + // Workspace defines configurables at the workspace level. type Workspace struct { - // TODO: Add all unified authentication configurables. + // Unified authentication attributes. + // + // We omit sensitive attributes as they should never be hardcoded. + // They must be specified through environment variables instead. + // + // For example: token, password, Google credentials, Azure client secret, etc. + // + + // Generic attributes. Host string `json:"host,omitempty"` Profile string `json:"profile,omitempty"` + + // Google specific attributes. + GoogleServiceAccount string `json:"google_service_account,omitempty"` + + // Azure specific attributes. + AzureResourceID string `json:"azure_workspace_resource_id,omitempty"` + AzureUseMSI bool `json:"azure_use_msi,omitempty"` + AzureClientID string `json:"azure_client_id,omitempty"` + AzureTenantID string `json:"azure_tenant_id,omitempty"` + AzureEnvironment string `json:"azure_environment,omitempty"` + AzureLoginAppID string `json:"azure_login_app_id,omitempty"` +} + +func (w *Workspace) Client() *workspaces.WorkspacesClient { + config := databricks.Config{ + // Generic + Host: w.Host, + Profile: w.Profile, + + // Google + GoogleServiceAccount: w.GoogleServiceAccount, + + // Azure + AzureResourceID: w.AzureResourceID, + AzureUseMSI: w.AzureUseMSI, + AzureClientID: w.AzureClientID, + AzureTenantID: w.AzureTenantID, + AzureEnvironment: w.AzureEnvironment, + AzureLoginAppID: w.AzureLoginAppID, + } + + return workspaces.New(&config) } diff --git a/cmd/bundle/debug/debug.go b/cmd/bundle/debug/debug.go new file mode 100644 index 00000000..9750b7fb --- /dev/null +++ b/cmd/bundle/debug/debug.go @@ -0,0 +1,15 @@ +package debug + +import ( + "github.com/spf13/cobra" + + parent "github.com/databricks/bricks/cmd/bundle" +) + +var debugCmd = &cobra.Command{ + Use: "debug", +} + +func init() { + parent.AddCommand(debugCmd) +} diff --git a/cmd/bundle/debug/whoami.go b/cmd/bundle/debug/whoami.go new file mode 100644 index 00000000..1fe238c8 --- /dev/null +++ b/cmd/bundle/debug/whoami.go @@ -0,0 +1,30 @@ +package debug + +import ( + "fmt" + + "github.com/databricks/bricks/bundle" + parent "github.com/databricks/bricks/cmd/bundle" + "github.com/spf13/cobra" +) + +var whoamiCmd = &cobra.Command{ + Use: "whoami", + + PreRunE: parent.ConfigureBundle, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + w := bundle.Get(ctx).WorkspaceClient() + user, err := w.CurrentUser.Me(ctx) + if err != nil { + return err + } + + fmt.Fprintln(cmd.OutOrStdout(), user.UserName) + return nil + }, +} + +func init() { + debugCmd.AddCommand(whoamiCmd) +} diff --git a/cmd/bundle/validate.go b/cmd/bundle/validate.go index af4416c3..3dfed687 100644 --- a/cmd/bundle/validate.go +++ b/cmd/bundle/validate.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" ) -var validate = &cobra.Command{ +var validateCmd = &cobra.Command{ Use: "validate", Short: "Validate configuration", @@ -24,5 +24,5 @@ var validate = &cobra.Command{ } func init() { - AddCommand(validate) + AddCommand(validateCmd) } diff --git a/main.go b/main.go index a03285e4..fc927e75 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( _ "github.com/databricks/bricks/cmd/api" _ "github.com/databricks/bricks/cmd/bundle" + _ "github.com/databricks/bricks/cmd/bundle/debug" _ "github.com/databricks/bricks/cmd/configure" _ "github.com/databricks/bricks/cmd/fs" _ "github.com/databricks/bricks/cmd/init"