Function to return workspace client on bundle.Bundle (#100)

Complementary command to check the identity in the context of a bundle
environment:

For example:
```
bricks bundle debug whoami -e development
```
This commit is contained in:
Pieter Noordhuis 2022-11-23 15:20:03 +01:00 committed by GitHub
parent 696e337516
commit 07f07694a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 108 additions and 3 deletions

View File

@ -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
}

View File

@ -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)
}

15
cmd/bundle/debug/debug.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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"