2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2022-11-21 14:39:53 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"path/filepath"
|
2022-11-23 14:20:03 +00:00
|
|
|
"sync"
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle/config"
|
2022-11-21 14:39:53 +00:00
|
|
|
"github.com/databricks/bricks/bundle/config/mutator"
|
2022-11-24 20:41:57 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
2022-11-18 09:57:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bundle struct {
|
|
|
|
Config config.Root
|
2022-11-23 14:20:03 +00:00
|
|
|
|
|
|
|
// Store a pointer to the workspace client.
|
|
|
|
// It can be initialized on demand after loading the configuration.
|
|
|
|
clientOnce sync.Once
|
2022-11-24 20:41:57 +00:00
|
|
|
client *databricks.WorkspaceClient
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 14:39:53 +00:00
|
|
|
func (b *Bundle) MutateForEnvironment(env string) error {
|
|
|
|
return mutator.Apply(&b.Config, mutator.DefaultMutatorsForEnvironment(env))
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
func Load(path string) (*Bundle, error) {
|
|
|
|
bundle := &Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := bundle.Config.Load(filepath.Join(path, config.FileName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bundle, nil
|
|
|
|
}
|
2022-11-21 14:39:53 +00:00
|
|
|
|
|
|
|
func LoadFromRoot() (*Bundle, error) {
|
|
|
|
root, err := getRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Load(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigureForEnvironment(ctx context.Context, env string) (context.Context, error) {
|
|
|
|
b, err := LoadFromRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.MutateForEnvironment(env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Context(ctx, b), nil
|
|
|
|
}
|
2022-11-23 14:20:03 +00:00
|
|
|
|
2022-11-24 20:41:57 +00:00
|
|
|
func (b *Bundle) WorkspaceClient() *databricks.WorkspaceClient {
|
2022-11-23 14:20:03 +00:00
|
|
|
b.clientOnce.Do(func() {
|
2022-11-24 20:41:57 +00:00
|
|
|
var err error
|
|
|
|
b.client, err = b.Config.Workspace.Client()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-23 14:20:03 +00:00
|
|
|
})
|
|
|
|
return b.client
|
|
|
|
}
|