mirror of https://github.com/databricks/cli.git
Compare commits
9 Commits
3d7cd5d5a4
...
1def3e3640
Author | SHA1 | Date |
---|---|---|
Gleb Kanterov | 1def3e3640 | |
shreyas-goenka | abc2f3c825 | |
shreyas-goenka | c2e2abcc35 | |
Pieter Noordhuis | 14fe03dcb9 | |
Gleb Kanterov | bbcbffd7f4 | |
Gleb Kanterov | 6b4641c530 | |
Gleb Kanterov | 55c36bc2fa | |
Gleb Kanterov | 6fd701ec84 | |
Gleb Kanterov | 37d677c079 |
|
@ -5,8 +5,7 @@
|
||||||
},
|
},
|
||||||
"batch": {
|
"batch": {
|
||||||
".codegen/cmds-workspace.go.tmpl": "cmd/workspace/cmd.go",
|
".codegen/cmds-workspace.go.tmpl": "cmd/workspace/cmd.go",
|
||||||
".codegen/cmds-account.go.tmpl": "cmd/account/cmd.go",
|
".codegen/cmds-account.go.tmpl": "cmd/account/cmd.go"
|
||||||
".codegen/lookup.go.tmpl": "bundle/config/variable/lookup.go"
|
|
||||||
},
|
},
|
||||||
"toolchain": {
|
"toolchain": {
|
||||||
"required": ["go"],
|
"required": ["go"],
|
||||||
|
|
|
@ -1,134 +0,0 @@
|
||||||
// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.
|
|
||||||
|
|
||||||
package variable
|
|
||||||
|
|
||||||
{{ $allowlist :=
|
|
||||||
list
|
|
||||||
"alerts"
|
|
||||||
"clusters"
|
|
||||||
"cluster-policies"
|
|
||||||
"clusters"
|
|
||||||
"dashboards"
|
|
||||||
"instance-pools"
|
|
||||||
"jobs"
|
|
||||||
"metastores"
|
|
||||||
"pipelines"
|
|
||||||
"service-principals"
|
|
||||||
"queries"
|
|
||||||
"warehouses"
|
|
||||||
}}
|
|
||||||
|
|
||||||
{{ $customField :=
|
|
||||||
dict
|
|
||||||
"service-principals" "ApplicationId"
|
|
||||||
}}
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/databricks/databricks-sdk-go"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Lookup struct {
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
{{.Singular.PascalName}} string `json:"{{.Singular.SnakeName}},omitempty"`
|
|
||||||
|
|
||||||
{{end}}
|
|
||||||
{{- end}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func LookupFromMap(m map[string]any) *Lookup {
|
|
||||||
l := &Lookup{}
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
if v, ok := m["{{.Singular.SnakeName}}"]; ok {
|
|
||||||
l.{{.Singular.PascalName}} = v.(string)
|
|
||||||
}
|
|
||||||
{{end -}}
|
|
||||||
{{- end}}
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
|
||||||
if err := l.validate(); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
r := allResolvers()
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
if l.{{.Singular.PascalName}} != "" {
|
|
||||||
return r.{{.Singular.PascalName}}(ctx, w, l.{{.Singular.PascalName}})
|
|
||||||
}
|
|
||||||
{{end -}}
|
|
||||||
{{- end}}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("no valid lookup fields provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Lookup) String() string {
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
if l.{{.Singular.PascalName}} != "" {
|
|
||||||
return fmt.Sprintf("{{.Singular.KebabName}}: %s", l.{{.Singular.PascalName}})
|
|
||||||
}
|
|
||||||
{{end -}}
|
|
||||||
{{- end}}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Lookup) validate() error {
|
|
||||||
// Validate that only one field is set
|
|
||||||
count := 0
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
if l.{{.Singular.PascalName}} != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
{{end -}}
|
|
||||||
{{- end}}
|
|
||||||
|
|
||||||
if count != 1 {
|
|
||||||
return fmt.Errorf("exactly one lookup field must be provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(l.String(), "${var") {
|
|
||||||
return fmt.Errorf("lookup fields cannot contain variable references")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error)
|
|
||||||
type resolvers struct {
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
{{.Singular.PascalName}} resolverFunc
|
|
||||||
{{end -}}
|
|
||||||
{{- end}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func allResolvers() *resolvers {
|
|
||||||
r := &resolvers{}
|
|
||||||
{{range .Services -}}
|
|
||||||
{{- if in $allowlist .KebabName -}}
|
|
||||||
r.{{.Singular.PascalName}} = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["{{.Singular.PascalName}}"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.{{.PascalName}}.GetBy{{range .NamedIdMap.NamePath}}{{.PascalName}}{{end}}(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.{{ getOrDefault $customField .KebabName ((index .NamedIdMap.IdPath 0).PascalName) }}), nil
|
|
||||||
}
|
|
||||||
{{end -}}
|
|
||||||
{{- end}}
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
|
@ -1,4 +1,3 @@
|
||||||
bundle/config/variable/lookup.go linguist-generated=true
|
|
||||||
cmd/account/access-control/access-control.go linguist-generated=true
|
cmd/account/access-control/access-control.go linguist-generated=true
|
||||||
cmd/account/billable-usage/billable-usage.go linguist-generated=true
|
cmd/account/billable-usage/billable-usage.go linguist-generated=true
|
||||||
cmd/account/budgets/budgets.go linguist-generated=true
|
cmd/account/budgets/budgets.go linguist-generated=true
|
||||||
|
|
|
@ -38,7 +38,7 @@ jobs:
|
||||||
- name: Setup Python
|
- name: Setup Python
|
||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: '3.9'
|
python-version: '3.10'
|
||||||
|
|
||||||
- name: Set go env
|
- name: Set go env
|
||||||
run: |
|
run: |
|
||||||
|
|
|
@ -126,8 +126,34 @@ func (t *translateContext) rewritePath(
|
||||||
func (t *translateContext) translateNotebookPath(literal, localFullPath, localRelPath, remotePath string) (string, error) {
|
func (t *translateContext) translateNotebookPath(literal, localFullPath, localRelPath, remotePath string) (string, error) {
|
||||||
nb, _, err := notebook.DetectWithFS(t.b.SyncRoot, filepath.ToSlash(localRelPath))
|
nb, _, err := notebook.DetectWithFS(t.b.SyncRoot, filepath.ToSlash(localRelPath))
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
if filepath.Ext(localFullPath) != notebook.ExtensionNone {
|
||||||
return "", fmt.Errorf("notebook %s not found", literal)
|
return "", fmt.Errorf("notebook %s not found", literal)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extensions := []string{
|
||||||
|
notebook.ExtensionPython,
|
||||||
|
notebook.ExtensionR,
|
||||||
|
notebook.ExtensionScala,
|
||||||
|
notebook.ExtensionSql,
|
||||||
|
notebook.ExtensionJupyter,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether a file with a notebook extension already exists. This
|
||||||
|
// way we can provide a more targeted error message.
|
||||||
|
for _, ext := range extensions {
|
||||||
|
literalWithExt := literal + ext
|
||||||
|
localRelPathWithExt := filepath.ToSlash(localRelPath + ext)
|
||||||
|
if _, err := fs.Stat(t.b.SyncRoot, localRelPathWithExt); err == nil {
|
||||||
|
return "", fmt.Errorf(`notebook %s not found. Did you mean %s?
|
||||||
|
Local notebook references are expected to contain one of the following
|
||||||
|
file extensions: [%s]`, literal, literalWithExt, strings.Join(extensions, ", "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a generic error message if no matching possible file is found.
|
||||||
|
return "", fmt.Errorf(`notebook %s not found. Local notebook references are expected
|
||||||
|
to contain one of the following file extensions: [%s]`, literal, strings.Join(extensions, ", "))
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("unable to determine if %s is a notebook: %w", localFullPath, err)
|
return "", fmt.Errorf("unable to determine if %s is a notebook: %w", localFullPath, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package mutator_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -508,6 +509,59 @@ func TestPipelineNotebookDoesNotExistError(t *testing.T) {
|
||||||
assert.EqualError(t, diags.Error(), "notebook ./doesnt_exist.py not found")
|
assert.EqualError(t, diags.Error(), "notebook ./doesnt_exist.py not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPipelineNotebookDoesNotExistErrorWithoutExtension(t *testing.T) {
|
||||||
|
for _, ext := range []string{
|
||||||
|
".py",
|
||||||
|
".r",
|
||||||
|
".scala",
|
||||||
|
".sql",
|
||||||
|
".ipynb",
|
||||||
|
"",
|
||||||
|
} {
|
||||||
|
t.Run("case_"+ext, func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
|
||||||
|
if ext != "" {
|
||||||
|
touchEmptyFile(t, filepath.Join(dir, "foo"+ext))
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &bundle.Bundle{
|
||||||
|
SyncRootPath: dir,
|
||||||
|
SyncRoot: vfs.MustNew(dir),
|
||||||
|
Config: config.Root{
|
||||||
|
Resources: config.Resources{
|
||||||
|
Pipelines: map[string]*resources.Pipeline{
|
||||||
|
"pipeline": {
|
||||||
|
PipelineSpec: &pipelines.PipelineSpec{
|
||||||
|
Libraries: []pipelines.PipelineLibrary{
|
||||||
|
{
|
||||||
|
Notebook: &pipelines.NotebookLibrary{
|
||||||
|
Path: "./foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
bundletest.SetLocation(b, ".", []dyn.Location{{File: filepath.Join(dir, "fake.yml")}})
|
||||||
|
diags := bundle.Apply(context.Background(), b, mutator.TranslatePaths())
|
||||||
|
|
||||||
|
if ext == "" {
|
||||||
|
assert.EqualError(t, diags.Error(), `notebook ./foo not found. Local notebook references are expected
|
||||||
|
to contain one of the following file extensions: [.py, .r, .scala, .sql, .ipynb]`)
|
||||||
|
} else {
|
||||||
|
assert.EqualError(t, diags.Error(), fmt.Sprintf(`notebook ./foo not found. Did you mean ./foo%s?
|
||||||
|
Local notebook references are expected to contain one of the following
|
||||||
|
file extensions: [.py, .r, .scala, .sql, .ipynb]`, ext))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPipelineFileDoesNotExistError(t *testing.T) {
|
func TestPipelineFileDoesNotExistError(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.
|
|
||||||
|
|
||||||
package variable
|
package variable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/databricks/databricks-sdk-go"
|
"github.com/databricks/databricks-sdk-go"
|
||||||
)
|
)
|
||||||
|
@ -34,323 +31,75 @@ type Lookup struct {
|
||||||
Warehouse string `json:"warehouse,omitempty"`
|
Warehouse string `json:"warehouse,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LookupFromMap(m map[string]any) *Lookup {
|
type resolver interface {
|
||||||
l := &Lookup{}
|
// Resolve resolves the underlying entity's ID.
|
||||||
if v, ok := m["alert"]; ok {
|
Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error)
|
||||||
l.Alert = v.(string)
|
|
||||||
|
// String returns a human-readable representation of the resolver.
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Lookup) constructResolver() (resolver, error) {
|
||||||
|
var resolvers []resolver
|
||||||
|
|
||||||
|
if l.Alert != "" {
|
||||||
|
resolvers = append(resolvers, resolveAlert{name: l.Alert})
|
||||||
}
|
}
|
||||||
if v, ok := m["cluster_policy"]; ok {
|
if l.ClusterPolicy != "" {
|
||||||
l.ClusterPolicy = v.(string)
|
resolvers = append(resolvers, resolveClusterPolicy{name: l.ClusterPolicy})
|
||||||
}
|
}
|
||||||
if v, ok := m["cluster"]; ok {
|
if l.Cluster != "" {
|
||||||
l.Cluster = v.(string)
|
resolvers = append(resolvers, resolveCluster{name: l.Cluster})
|
||||||
}
|
}
|
||||||
if v, ok := m["dashboard"]; ok {
|
if l.Dashboard != "" {
|
||||||
l.Dashboard = v.(string)
|
resolvers = append(resolvers, resolveDashboard{name: l.Dashboard})
|
||||||
}
|
}
|
||||||
if v, ok := m["instance_pool"]; ok {
|
if l.InstancePool != "" {
|
||||||
l.InstancePool = v.(string)
|
resolvers = append(resolvers, resolveInstancePool{name: l.InstancePool})
|
||||||
}
|
}
|
||||||
if v, ok := m["job"]; ok {
|
if l.Job != "" {
|
||||||
l.Job = v.(string)
|
resolvers = append(resolvers, resolveJob{name: l.Job})
|
||||||
}
|
}
|
||||||
if v, ok := m["metastore"]; ok {
|
if l.Metastore != "" {
|
||||||
l.Metastore = v.(string)
|
resolvers = append(resolvers, resolveMetastore{name: l.Metastore})
|
||||||
}
|
}
|
||||||
if v, ok := m["pipeline"]; ok {
|
if l.Pipeline != "" {
|
||||||
l.Pipeline = v.(string)
|
resolvers = append(resolvers, resolvePipeline{name: l.Pipeline})
|
||||||
}
|
}
|
||||||
if v, ok := m["query"]; ok {
|
if l.Query != "" {
|
||||||
l.Query = v.(string)
|
resolvers = append(resolvers, resolveQuery{name: l.Query})
|
||||||
}
|
}
|
||||||
if v, ok := m["service_principal"]; ok {
|
if l.ServicePrincipal != "" {
|
||||||
l.ServicePrincipal = v.(string)
|
resolvers = append(resolvers, resolveServicePrincipal{name: l.ServicePrincipal})
|
||||||
}
|
}
|
||||||
if v, ok := m["warehouse"]; ok {
|
if l.Warehouse != "" {
|
||||||
l.Warehouse = v.(string)
|
resolvers = append(resolvers, resolveWarehouse{name: l.Warehouse})
|
||||||
}
|
}
|
||||||
|
|
||||||
return l
|
switch len(resolvers) {
|
||||||
|
case 0:
|
||||||
|
return nil, fmt.Errorf("no valid lookup fields provided")
|
||||||
|
case 1:
|
||||||
|
return resolvers[0], nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("exactly one lookup field must be provided")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
if err := l.validate(); err != nil {
|
r, err := l.constructResolver()
|
||||||
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := allResolvers()
|
return r.Resolve(ctx, w)
|
||||||
if l.Alert != "" {
|
|
||||||
return r.Alert(ctx, w, l.Alert)
|
|
||||||
}
|
|
||||||
if l.ClusterPolicy != "" {
|
|
||||||
return r.ClusterPolicy(ctx, w, l.ClusterPolicy)
|
|
||||||
}
|
|
||||||
if l.Cluster != "" {
|
|
||||||
return r.Cluster(ctx, w, l.Cluster)
|
|
||||||
}
|
|
||||||
if l.Dashboard != "" {
|
|
||||||
return r.Dashboard(ctx, w, l.Dashboard)
|
|
||||||
}
|
|
||||||
if l.InstancePool != "" {
|
|
||||||
return r.InstancePool(ctx, w, l.InstancePool)
|
|
||||||
}
|
|
||||||
if l.Job != "" {
|
|
||||||
return r.Job(ctx, w, l.Job)
|
|
||||||
}
|
|
||||||
if l.Metastore != "" {
|
|
||||||
return r.Metastore(ctx, w, l.Metastore)
|
|
||||||
}
|
|
||||||
if l.Pipeline != "" {
|
|
||||||
return r.Pipeline(ctx, w, l.Pipeline)
|
|
||||||
}
|
|
||||||
if l.Query != "" {
|
|
||||||
return r.Query(ctx, w, l.Query)
|
|
||||||
}
|
|
||||||
if l.ServicePrincipal != "" {
|
|
||||||
return r.ServicePrincipal(ctx, w, l.ServicePrincipal)
|
|
||||||
}
|
|
||||||
if l.Warehouse != "" {
|
|
||||||
return r.Warehouse(ctx, w, l.Warehouse)
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("no valid lookup fields provided")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Lookup) String() string {
|
func (l *Lookup) String() string {
|
||||||
if l.Alert != "" {
|
r, _ := l.constructResolver()
|
||||||
return fmt.Sprintf("alert: %s", l.Alert)
|
if r == nil {
|
||||||
}
|
|
||||||
if l.ClusterPolicy != "" {
|
|
||||||
return fmt.Sprintf("cluster-policy: %s", l.ClusterPolicy)
|
|
||||||
}
|
|
||||||
if l.Cluster != "" {
|
|
||||||
return fmt.Sprintf("cluster: %s", l.Cluster)
|
|
||||||
}
|
|
||||||
if l.Dashboard != "" {
|
|
||||||
return fmt.Sprintf("dashboard: %s", l.Dashboard)
|
|
||||||
}
|
|
||||||
if l.InstancePool != "" {
|
|
||||||
return fmt.Sprintf("instance-pool: %s", l.InstancePool)
|
|
||||||
}
|
|
||||||
if l.Job != "" {
|
|
||||||
return fmt.Sprintf("job: %s", l.Job)
|
|
||||||
}
|
|
||||||
if l.Metastore != "" {
|
|
||||||
return fmt.Sprintf("metastore: %s", l.Metastore)
|
|
||||||
}
|
|
||||||
if l.Pipeline != "" {
|
|
||||||
return fmt.Sprintf("pipeline: %s", l.Pipeline)
|
|
||||||
}
|
|
||||||
if l.Query != "" {
|
|
||||||
return fmt.Sprintf("query: %s", l.Query)
|
|
||||||
}
|
|
||||||
if l.ServicePrincipal != "" {
|
|
||||||
return fmt.Sprintf("service-principal: %s", l.ServicePrincipal)
|
|
||||||
}
|
|
||||||
if l.Warehouse != "" {
|
|
||||||
return fmt.Sprintf("warehouse: %s", l.Warehouse)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Lookup) validate() error {
|
return r.String()
|
||||||
// Validate that only one field is set
|
|
||||||
count := 0
|
|
||||||
if l.Alert != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.ClusterPolicy != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Cluster != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Dashboard != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.InstancePool != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Job != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Metastore != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Pipeline != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Query != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.ServicePrincipal != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if l.Warehouse != "" {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
|
|
||||||
if count != 1 {
|
|
||||||
return fmt.Errorf("exactly one lookup field must be provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(l.String(), "${var") {
|
|
||||||
return fmt.Errorf("lookup fields cannot contain variable references")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type resolverFunc func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error)
|
|
||||||
type resolvers struct {
|
|
||||||
Alert resolverFunc
|
|
||||||
ClusterPolicy resolverFunc
|
|
||||||
Cluster resolverFunc
|
|
||||||
Dashboard resolverFunc
|
|
||||||
InstancePool resolverFunc
|
|
||||||
Job resolverFunc
|
|
||||||
Metastore resolverFunc
|
|
||||||
Pipeline resolverFunc
|
|
||||||
Query resolverFunc
|
|
||||||
ServicePrincipal resolverFunc
|
|
||||||
Warehouse resolverFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func allResolvers() *resolvers {
|
|
||||||
r := &resolvers{}
|
|
||||||
r.Alert = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Alert"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Alerts.GetByDisplayName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.Id), nil
|
|
||||||
}
|
|
||||||
r.ClusterPolicy = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["ClusterPolicy"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.ClusterPolicies.GetByName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.PolicyId), nil
|
|
||||||
}
|
|
||||||
r.Cluster = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Cluster"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Clusters.GetByClusterName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.ClusterId), nil
|
|
||||||
}
|
|
||||||
r.Dashboard = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Dashboard"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Dashboards.GetByName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.Id), nil
|
|
||||||
}
|
|
||||||
r.InstancePool = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["InstancePool"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.InstancePools.GetByInstancePoolName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.InstancePoolId), nil
|
|
||||||
}
|
|
||||||
r.Job = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Job"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Jobs.GetBySettingsName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.JobId), nil
|
|
||||||
}
|
|
||||||
r.Metastore = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Metastore"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Metastores.GetByName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.MetastoreId), nil
|
|
||||||
}
|
|
||||||
r.Pipeline = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Pipeline"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Pipelines.GetByName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.PipelineId), nil
|
|
||||||
}
|
|
||||||
r.Query = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Query"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Queries.GetByDisplayName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.Id), nil
|
|
||||||
}
|
|
||||||
r.ServicePrincipal = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["ServicePrincipal"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.ServicePrincipals.GetByDisplayName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.ApplicationId), nil
|
|
||||||
}
|
|
||||||
r.Warehouse = func(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
|
||||||
fn, ok := lookupOverrides["Warehouse"]
|
|
||||||
if ok {
|
|
||||||
return fn(ctx, w, name)
|
|
||||||
}
|
|
||||||
entity, err := w.Warehouses.GetByName(ctx, name)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprint(entity.Id), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLookup_Coverage(t *testing.T) {
|
||||||
|
var lookup Lookup
|
||||||
|
val := reflect.ValueOf(lookup)
|
||||||
|
typ := val.Type()
|
||||||
|
|
||||||
|
for i := 0; i < val.NumField(); i++ {
|
||||||
|
field := val.Field(i)
|
||||||
|
if field.Kind() != reflect.String {
|
||||||
|
t.Fatalf("Field %s is not a string", typ.Field(i).Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldType := typ.Field(i)
|
||||||
|
t.Run(fieldType.Name, func(t *testing.T) {
|
||||||
|
// Use a fresh instance of the struct in each test
|
||||||
|
var lookup Lookup
|
||||||
|
|
||||||
|
// Set the field to a non-empty string
|
||||||
|
reflect.ValueOf(&lookup).Elem().Field(i).SetString("value")
|
||||||
|
|
||||||
|
// Test the [String] function
|
||||||
|
assert.NotEmpty(t, lookup.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookup_Empty(t *testing.T) {
|
||||||
|
var lookup Lookup
|
||||||
|
|
||||||
|
// Resolve returns an error when no fields are provided
|
||||||
|
_, err := lookup.Resolve(context.Background(), nil)
|
||||||
|
assert.ErrorContains(t, err, "no valid lookup fields provided")
|
||||||
|
|
||||||
|
// No string representation for an invalid lookup
|
||||||
|
assert.Empty(t, lookup.String())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookup_Multiple(t *testing.T) {
|
||||||
|
lookup := Lookup{
|
||||||
|
Alert: "alert",
|
||||||
|
Query: "query",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve returns an error when multiple fields are provided
|
||||||
|
_, err := lookup.Resolve(context.Background(), nil)
|
||||||
|
assert.ErrorContains(t, err, "exactly one lookup field must be provided")
|
||||||
|
|
||||||
|
// No string representation for an invalid lookup
|
||||||
|
assert.Empty(t, lookup.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveAlert struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveAlert) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Alerts.GetByDisplayName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveAlert) String() string {
|
||||||
|
return fmt.Sprintf("alert: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/sql"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveAlert_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockAlertsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByDisplayName(mock.Anything, "alert").
|
||||||
|
Return(&sql.ListAlertsResponseAlert{
|
||||||
|
Id: "1234",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveAlert{name: "alert"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "1234", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveAlert_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockAlertsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByDisplayName(mock.Anything, "alert").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveAlert{name: "alert"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveAlert_String(t *testing.T) {
|
||||||
|
l := resolveAlert{name: "name"}
|
||||||
|
assert.Equal(t, "alert: name", l.String())
|
||||||
|
}
|
|
@ -8,13 +8,13 @@ import (
|
||||||
"github.com/databricks/databricks-sdk-go/service/compute"
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
||||||
)
|
)
|
||||||
|
|
||||||
var lookupOverrides = map[string]resolverFunc{
|
type resolveCluster struct {
|
||||||
"Cluster": resolveCluster,
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
// We added a custom resolver for the cluster to add filtering for the cluster source when we list all clusters.
|
// We added a custom resolver for the cluster to add filtering for the cluster source when we list all clusters.
|
||||||
// Without the filtering listing could take a very long time (5-10 mins) which leads to lookup timeouts.
|
// Without the filtering listing could take a very long time (5-10 mins) which leads to lookup timeouts.
|
||||||
func resolveCluster(ctx context.Context, w *databricks.WorkspaceClient, name string) (string, error) {
|
func (l resolveCluster) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
result, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{
|
result, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{
|
||||||
FilterBy: &compute.ListClustersFilterBy{
|
FilterBy: &compute.ListClustersFilterBy{
|
||||||
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
|
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
|
||||||
|
@ -30,6 +30,8 @@ func resolveCluster(ctx context.Context, w *databricks.WorkspaceClient, name str
|
||||||
key := v.ClusterName
|
key := v.ClusterName
|
||||||
tmp[key] = append(tmp[key], v)
|
tmp[key] = append(tmp[key], v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name := l.name
|
||||||
alternatives, ok := tmp[name]
|
alternatives, ok := tmp[name]
|
||||||
if !ok || len(alternatives) == 0 {
|
if !ok || len(alternatives) == 0 {
|
||||||
return "", fmt.Errorf("cluster named '%s' does not exist", name)
|
return "", fmt.Errorf("cluster named '%s' does not exist", name)
|
||||||
|
@ -39,3 +41,7 @@ func resolveCluster(ctx context.Context, w *databricks.WorkspaceClient, name str
|
||||||
}
|
}
|
||||||
return alternatives[0].ClusterId, nil
|
return alternatives[0].ClusterId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l resolveCluster) String() string {
|
||||||
|
return fmt.Sprintf("cluster: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveClusterPolicy struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveClusterPolicy) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.ClusterPolicies.GetByName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.PolicyId), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveClusterPolicy) String() string {
|
||||||
|
return fmt.Sprintf("cluster-policy: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveClusterPolicy_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockClusterPoliciesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "policy").
|
||||||
|
Return(&compute.Policy{
|
||||||
|
PolicyId: "1234",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveClusterPolicy{name: "policy"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "1234", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveClusterPolicy_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockClusterPoliciesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "policy").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveClusterPolicy{name: "policy"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveClusterPolicy_String(t *testing.T) {
|
||||||
|
l := resolveClusterPolicy{name: "name"}
|
||||||
|
assert.Equal(t, "cluster-policy: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveCluster_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockClustersAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
ListAll(mock.Anything, mock.Anything).
|
||||||
|
Return([]compute.ClusterDetails{
|
||||||
|
{ClusterId: "1234", ClusterName: "cluster1"},
|
||||||
|
{ClusterId: "2345", ClusterName: "cluster2"},
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveCluster{name: "cluster2"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "2345", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveCluster_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockClustersAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
ListAll(mock.Anything, mock.Anything).
|
||||||
|
Return([]compute.ClusterDetails{}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveCluster{name: "cluster"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "cluster named 'cluster' does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveCluster_String(t *testing.T) {
|
||||||
|
l := resolveCluster{name: "name"}
|
||||||
|
assert.Equal(t, "cluster: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveDashboard struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveDashboard) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Dashboards.GetByName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveDashboard) String() string {
|
||||||
|
return fmt.Sprintf("dashboard: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/sql"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveDashboard_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockDashboardsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "dashboard").
|
||||||
|
Return(&sql.Dashboard{
|
||||||
|
Id: "1234",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveDashboard{name: "dashboard"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "1234", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveDashboard_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockDashboardsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "dashboard").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveDashboard{name: "dashboard"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveDashboard_String(t *testing.T) {
|
||||||
|
l := resolveDashboard{name: "name"}
|
||||||
|
assert.Equal(t, "dashboard: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveInstancePool struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveInstancePool) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.InstancePools.GetByInstancePoolName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.InstancePoolId), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveInstancePool) String() string {
|
||||||
|
return fmt.Sprintf("instance-pool: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveInstancePool_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockInstancePoolsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByInstancePoolName(mock.Anything, "instance_pool").
|
||||||
|
Return(&compute.InstancePoolAndStats{
|
||||||
|
InstancePoolId: "5678",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveInstancePool{name: "instance_pool"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "5678", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveInstancePool_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockInstancePoolsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByInstancePoolName(mock.Anything, "instance_pool").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveInstancePool{name: "instance_pool"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveInstancePool_String(t *testing.T) {
|
||||||
|
l := resolveInstancePool{name: "name"}
|
||||||
|
assert.Equal(t, "instance-pool: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveJob struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveJob) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Jobs.GetBySettingsName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.JobId), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveJob) String() string {
|
||||||
|
return fmt.Sprintf("job: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveJob_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockJobsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetBySettingsName(mock.Anything, "job").
|
||||||
|
Return(&jobs.BaseJob{
|
||||||
|
JobId: 5678,
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveJob{name: "job"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "5678", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveJob_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockJobsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetBySettingsName(mock.Anything, "job").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveJob{name: "job"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveJob_String(t *testing.T) {
|
||||||
|
l := resolveJob{name: "name"}
|
||||||
|
assert.Equal(t, "job: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveMetastore struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveMetastore) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Metastores.GetByName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.MetastoreId), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveMetastore) String() string {
|
||||||
|
return fmt.Sprintf("metastore: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/catalog"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveMetastore_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockMetastoresAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "metastore").
|
||||||
|
Return(&catalog.MetastoreInfo{
|
||||||
|
MetastoreId: "abcd",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveMetastore{name: "metastore"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "abcd", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveMetastore_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockMetastoresAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "metastore").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveMetastore{name: "metastore"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveMetastore_String(t *testing.T) {
|
||||||
|
l := resolveMetastore{name: "name"}
|
||||||
|
assert.Equal(t, "metastore: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolvePipeline struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolvePipeline) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Pipelines.GetByName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.PipelineId), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolvePipeline) String() string {
|
||||||
|
return fmt.Sprintf("pipeline: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/pipelines"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolvePipeline_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockPipelinesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "pipeline").
|
||||||
|
Return(&pipelines.PipelineStateInfo{
|
||||||
|
PipelineId: "abcd",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolvePipeline{name: "pipeline"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "abcd", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolvePipeline_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockPipelinesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "pipeline").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolvePipeline{name: "pipeline"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolvePipeline_String(t *testing.T) {
|
||||||
|
l := resolvePipeline{name: "name"}
|
||||||
|
assert.Equal(t, "pipeline: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveQuery struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveQuery) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Queries.GetByDisplayName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveQuery) String() string {
|
||||||
|
return fmt.Sprintf("query: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/sql"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveQuery_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockQueriesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByDisplayName(mock.Anything, "query").
|
||||||
|
Return(&sql.ListQueryObjectsResponseQuery{
|
||||||
|
Id: "1234",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveQuery{name: "query"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "1234", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveQuery_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockQueriesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByDisplayName(mock.Anything, "query").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveQuery{name: "query"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveQuery_String(t *testing.T) {
|
||||||
|
l := resolveQuery{name: "name"}
|
||||||
|
assert.Equal(t, "query: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveServicePrincipal struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveServicePrincipal) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.ServicePrincipals.GetByDisplayName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.ApplicationId), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveServicePrincipal) String() string {
|
||||||
|
return fmt.Sprintf("service-principal: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/iam"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveServicePrincipal_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockServicePrincipalsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByDisplayName(mock.Anything, "service-principal").
|
||||||
|
Return(&iam.ServicePrincipal{
|
||||||
|
ApplicationId: "5678",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveServicePrincipal{name: "service-principal"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "5678", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveServicePrincipal_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockServicePrincipalsAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByDisplayName(mock.Anything, "service-principal").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveServicePrincipal{name: "service-principal"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveServicePrincipal_String(t *testing.T) {
|
||||||
|
l := resolveServicePrincipal{name: "name"}
|
||||||
|
assert.Equal(t, "service-principal: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resolveWarehouse struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveWarehouse) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
|
||||||
|
entity, err := w.Warehouses.GetByName(ctx, l.name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprint(entity.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l resolveWarehouse) String() string {
|
||||||
|
return fmt.Sprintf("warehouse: %s", l.name)
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package variable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/sql"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveWarehouse_ResolveSuccess(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockWarehousesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "warehouse").
|
||||||
|
Return(&sql.EndpointInfo{
|
||||||
|
Id: "abcd",
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveWarehouse{name: "warehouse"}
|
||||||
|
result, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "abcd", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveWarehouse_ResolveNotFound(t *testing.T) {
|
||||||
|
m := mocks.NewMockWorkspaceClient(t)
|
||||||
|
|
||||||
|
api := m.GetMockWarehousesAPI()
|
||||||
|
api.EXPECT().
|
||||||
|
GetByName(mock.Anything, "warehouse").
|
||||||
|
Return(nil, &apierr.APIError{StatusCode: 404})
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
l := resolveWarehouse{name: "warehouse"}
|
||||||
|
_, err := l.Resolve(ctx, m.WorkspaceClient)
|
||||||
|
require.ErrorIs(t, err, apierr.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveWarehouse_String(t *testing.T) {
|
||||||
|
l := resolveWarehouse{name: "name"}
|
||||||
|
assert.Equal(t, "warehouse: name", l.String())
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
bundle:
|
||||||
|
name: python-import-dataclass-no-wheel
|
||||||
|
|
||||||
|
experimental:
|
||||||
|
pydabs:
|
||||||
|
enabled: true
|
||||||
|
import:
|
||||||
|
- "my_job"
|
||||||
|
|
||||||
|
variables:
|
||||||
|
default_cluster_spec:
|
||||||
|
type: complex
|
||||||
|
value:
|
||||||
|
num_workers: 1
|
||||||
|
spark_version: "15.4.x-scala2.12"
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Databricks Notebook Source
|
||||||
|
1+1
|
|
@ -0,0 +1,22 @@
|
||||||
|
from databricks.bundles.jobs import Job, Task, NotebookTask, JobCluster
|
||||||
|
from databricks.bundles.variables import Bundle
|
||||||
|
|
||||||
|
my_job = Job(
|
||||||
|
name="Test Job",
|
||||||
|
resource_name="my_job",
|
||||||
|
job_clusters=[
|
||||||
|
JobCluster(
|
||||||
|
job_cluster_key="my_cluster",
|
||||||
|
new_cluster=Bundle.variables.default_cluster_spec,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
tasks=[
|
||||||
|
Task(
|
||||||
|
task_key="my_notebook_task",
|
||||||
|
job_cluster_key="my_cluster",
|
||||||
|
notebook_task=NotebookTask(
|
||||||
|
notebook_path="notebooks/my_notebook.py",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
|
@ -0,0 +1,95 @@
|
||||||
|
package config_tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
pathlib "path"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/bundle/config/resources"
|
||||||
|
"github.com/databricks/cli/libs/dyn"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPythonImport_dataclass_no_wheel(t *testing.T) {
|
||||||
|
activateVEnv(t)
|
||||||
|
setPythonPath(t, "python_import/dataclass_no_wheel/src")
|
||||||
|
|
||||||
|
expected := &resources.Job{
|
||||||
|
JobSettings: &jobs.JobSettings{
|
||||||
|
Name: "Test Job",
|
||||||
|
JobClusters: []jobs.JobCluster{
|
||||||
|
{
|
||||||
|
JobClusterKey: "my_cluster",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Tasks: []jobs.Task{
|
||||||
|
{
|
||||||
|
NotebookTask: &jobs.NotebookTask{
|
||||||
|
NotebookPath: "notebooks/my_notebook.py",
|
||||||
|
},
|
||||||
|
JobClusterKey: "my_cluster",
|
||||||
|
TaskKey: "my_notebook_task",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
b := load(t, "./python_import/dataclass_no_wheel")
|
||||||
|
|
||||||
|
assert.Equal(t, []string{"my_job"}, maps.Keys(b.Config.Resources.Jobs))
|
||||||
|
|
||||||
|
myJob := b.Config.Resources.Jobs["my_job"]
|
||||||
|
assert.Equal(t, expected, myJob)
|
||||||
|
|
||||||
|
// NewCluster is reference to a variable and needs to be checked separately
|
||||||
|
err := b.Config.Mutate(func(value dyn.Value) (dyn.Value, error) {
|
||||||
|
path := dyn.MustPathFromString("resources.jobs.my_job.job_clusters[0].new_cluster")
|
||||||
|
value, err := dyn.GetByPath(value, path)
|
||||||
|
if err != nil {
|
||||||
|
return dyn.InvalidValue, err
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "${var.default_cluster_spec}", value.AsAny())
|
||||||
|
|
||||||
|
return value, nil
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setPythonPath(t *testing.T, path string) {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Setenv("PYTHONPATH", pathlib.Join(wd, path))
|
||||||
|
}
|
||||||
|
|
||||||
|
func activateVEnv(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
venvDir := pathlib.Join(dir, "venv")
|
||||||
|
|
||||||
|
err := exec.Command("python3", "-m", "venv", venvDir).Run()
|
||||||
|
require.NoError(t, err, "failed to create venv")
|
||||||
|
|
||||||
|
// we don't have shell to activate venv, updating PATH is enough
|
||||||
|
|
||||||
|
var venvBinDir string
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
venvBinDir = pathlib.Join(venvDir, "Scripts")
|
||||||
|
t.Setenv("PATH", venvBinDir+";"+os.Getenv("PATH"))
|
||||||
|
} else {
|
||||||
|
venvBinDir = pathlib.Join(venvDir, "bin")
|
||||||
|
t.Setenv("PATH", venvBinDir+":"+os.Getenv("PATH"))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = exec.Command(
|
||||||
|
pathlib.Join(venvBinDir, "pip"),
|
||||||
|
"install",
|
||||||
|
"databricks-pydabs==0.5.1",
|
||||||
|
).Run()
|
||||||
|
require.NoError(t, err, "failed to install databricks-pydabs")
|
||||||
|
}
|
|
@ -97,7 +97,7 @@ func TestAccBundleInitOnMlopsStacks(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
job, err := w.Jobs.GetByJobId(context.Background(), batchJobId)
|
job, err := w.Jobs.GetByJobId(context.Background(), batchJobId)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, fmt.Sprintf("dev-%s-batch-inference-job", projectName), job.Settings.Name)
|
assert.Contains(t, job.Settings.Name, fmt.Sprintf("dev-%s-batch-inference-job", projectName))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccBundleInitHelpers(t *testing.T) {
|
func TestAccBundleInitHelpers(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue