databricks-cli/project/project.go

120 lines
2.9 KiB
Go
Raw Normal View History

2022-05-13 13:30:22 +00:00
package project
import (
"context"
"fmt"
2022-05-14 17:55:00 +00:00
"sync"
2022-05-13 13:30:22 +00:00
"github.com/databrickslabs/terraform-provider-databricks/clusters"
2022-05-14 17:55:00 +00:00
"github.com/databrickslabs/terraform-provider-databricks/commands"
2022-05-13 13:30:22 +00:00
"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/scim"
)
// Current CLI application state - fixure out
2022-05-14 17:55:00 +00:00
var Current inner
type inner struct {
mu sync.Mutex
once sync.Once
project *Project
client *common.DatabricksClient
me *scim.User
}
func (i *inner) init() {
i.mu.Lock()
defer i.mu.Unlock()
i.once.Do(func() {
client := &common.DatabricksClient{}
2022-05-14 17:55:00 +00:00
client.WithCommandExecutor(func(
ctx context.Context, c *common.DatabricksClient) common.CommandExecutor {
return commands.NewCommandsAPI(ctx, c)
})
i.client = client
prj, err := loadProjectConf()
if err != nil {
panic(err)
}
client.Profile = prj.Profile // Databricks CLI profile
err = client.Configure()
if err != nil {
panic(err)
}
2022-05-14 17:55:00 +00:00
i.project = &prj
})
}
func (i *inner) Client() *common.DatabricksClient {
i.init()
return i.client
2022-05-13 13:30:22 +00:00
}
2022-05-14 17:55:00 +00:00
func (i *inner) Project() *Project {
i.init()
return i.project
2022-05-13 13:30:22 +00:00
}
2022-05-14 17:55:00 +00:00
func (i *inner) Me() *scim.User {
i.mu.Lock()
defer i.mu.Unlock()
if i.me != nil {
return i.me
}
me, err := scim.NewUsersAPI(context.Background(), i.Client()).Me()
2022-05-13 13:30:22 +00:00
if err != nil {
2022-05-14 17:55:00 +00:00
panic(err)
}
i.me = &me
return &me
}
2022-05-21 13:23:37 +00:00
func (i *inner) DeploymentIsolationPrefix() string {
if i.project.Isolation == None {
return i.project.Name
}
if i.project.Isolation == Soft {
me := i.Me()
return fmt.Sprintf("%s/%s", i.project.Name, me.UserName)
}
panic(fmt.Errorf("unknow project isolation: %s", i.project.Isolation))
}
2022-05-14 17:55:00 +00:00
func (i *inner) DevelopmentCluster(ctx context.Context) (cluster clusters.ClusterInfo, err error) {
api := clusters.NewClustersAPI(ctx, i.Client()) // TODO: rewrite with normal SDK
if i.project.DevCluster == nil {
i.project.DevCluster = &clusters.Cluster{}
}
dc := i.project.DevCluster
if i.project.Isolation == Soft {
if i.project.IsDevClusterJustReference() {
err = fmt.Errorf("projects with soft isolation cannot have named clusters")
return
}
2022-05-21 13:23:37 +00:00
dc.ClusterName = fmt.Sprintf("dev/%s", i.DeploymentIsolationPrefix())
2022-05-14 17:55:00 +00:00
}
if dc.ClusterName == "" {
err = fmt.Errorf("please either pick `isolation: soft` or specify a shared cluster name")
2022-05-13 13:30:22 +00:00
return
}
2022-05-14 17:55:00 +00:00
return api.GetOrCreateRunningCluster(dc.ClusterName, *dc)
2022-05-13 13:30:22 +00:00
}
func runCommandOnDev(ctx context.Context, language, command string) common.CommandResults {
2022-05-14 17:55:00 +00:00
cluster, err := Current.DevelopmentCluster(ctx)
exec := Current.Client().CommandExecutor(ctx)
2022-05-13 13:30:22 +00:00
if err != nil {
return common.CommandResults{
ResultType: "error",
2022-05-14 17:55:00 +00:00
Summary: err.Error(),
2022-05-13 13:30:22 +00:00
}
}
return exec.Execute(cluster.ClusterID, language, command)
}
func RunPythonOnDev(ctx context.Context, command string) common.CommandResults {
return runCommandOnDev(ctx, "python", command)
2022-05-14 17:55:00 +00:00
}