From b61e5181ff2e39bdee09845b0c81307e82da6498 Mon Sep 17 00:00:00 2001 From: monalisa Date: Tue, 14 Nov 2023 17:39:10 +0100 Subject: [PATCH 1/5] Add better error message when path is not a bundle template --- libs/template/helpers.go | 5 ++++- libs/template/materialize.go | 5 +++++ libs/template/materialize_test.go | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 libs/template/materialize_test.go diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 7f306a3aa5..f3d927f510 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -31,7 +31,6 @@ var cachedUser *iam.User var cachedIsServicePrincipal *bool func loadHelpers(ctx context.Context) template.FuncMap { - w := root.WorkspaceClient(ctx) return template.FuncMap{ "fail": func(format string, args ...any) (any, error) { return nil, ErrFail{fmt.Sprintf(format, args...)} @@ -65,6 +64,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { }, // Get smallest node type (follows Terraform's GetSmallestNodeType) "smallest_node_type": func() (string, error) { + w := root.WorkspaceClient(ctx) if w.Config.Host == "" { return "", errors.New("cannot determine target workspace, please first setup a configuration profile using 'databricks configure'") } @@ -79,12 +79,14 @@ func loadHelpers(ctx context.Context) template.FuncMap { return string(os.PathSeparator) }, "workspace_host": func() (string, error) { + w := root.WorkspaceClient(ctx) if w.Config.Host == "" { return "", errors.New("cannot determine target workspace, please first setup a configuration profile using 'databricks configure'") } return w.Config.Host, nil }, "user_name": func() (string, error) { + w := root.WorkspaceClient(ctx) if cachedUser == nil { var err error cachedUser, err = w.CurrentUser.Me(ctx) @@ -99,6 +101,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { return result, nil }, "is_service_principal": func() (bool, error) { + w := root.WorkspaceClient(ctx) if cachedIsServicePrincipal != nil { return *cachedIsServicePrincipal, nil } diff --git a/libs/template/materialize.go b/libs/template/materialize.go index 7c9105b732..de936247bc 100644 --- a/libs/template/materialize.go +++ b/libs/template/materialize.go @@ -3,6 +3,7 @@ package template import ( "context" "embed" + "fmt" "io/fs" "os" "path" @@ -43,6 +44,10 @@ func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir st schemaPath := filepath.Join(templateRoot, schemaFileName) helpers := loadHelpers(ctx) + if _, err := os.Stat(schemaPath); os.IsNotExist(err) { + return fmt.Errorf("Expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", schemaPath) + } + config, err := newConfig(ctx, schemaPath) if err != nil { return err diff --git a/libs/template/materialize_test.go b/libs/template/materialize_test.go new file mode 100644 index 0000000000..830c4acc23 --- /dev/null +++ b/libs/template/materialize_test.go @@ -0,0 +1,16 @@ +package template + +import ( + "context" + "fmt" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMaterializeForNonTemplateDirectory(t *testing.T) { + tmpDir := t.TempDir() + err := Materialize(context.Background(), "", tmpDir, "") + assert.EqualError(t, err, fmt.Sprintf("Expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", filepath.Join(tmpDir, schemaFileName))) +} From 99c31471d03808730d45993d3d3fda14ac386498 Mon Sep 17 00:00:00 2001 From: monalisa Date: Tue, 14 Nov 2023 17:40:54 +0100 Subject: [PATCH 2/5] - --- libs/template/materialize.go | 2 +- libs/template/materialize_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/template/materialize.go b/libs/template/materialize.go index de936247bc..c0be37b0ff 100644 --- a/libs/template/materialize.go +++ b/libs/template/materialize.go @@ -45,7 +45,7 @@ func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir st helpers := loadHelpers(ctx) if _, err := os.Stat(schemaPath); os.IsNotExist(err) { - return fmt.Errorf("Expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", schemaPath) + return fmt.Errorf("expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", schemaPath) } config, err := newConfig(ctx, schemaPath) diff --git a/libs/template/materialize_test.go b/libs/template/materialize_test.go index 830c4acc23..8f55dbe583 100644 --- a/libs/template/materialize_test.go +++ b/libs/template/materialize_test.go @@ -12,5 +12,5 @@ import ( func TestMaterializeForNonTemplateDirectory(t *testing.T) { tmpDir := t.TempDir() err := Materialize(context.Background(), "", tmpDir, "") - assert.EqualError(t, err, fmt.Sprintf("Expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", filepath.Join(tmpDir, schemaFileName))) + assert.EqualError(t, err, fmt.Sprintf("expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", filepath.Join(tmpDir, schemaFileName))) } From bbfdce629419ad4e3c27ba8366e47ab71af1aa0d Mon Sep 17 00:00:00 2001 From: monalisa Date: Tue, 14 Nov 2023 17:48:12 +0100 Subject: [PATCH 3/5] make error message shorter --- libs/template/materialize.go | 2 +- libs/template/materialize_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/template/materialize.go b/libs/template/materialize.go index c0be37b0ff..401df14cd1 100644 --- a/libs/template/materialize.go +++ b/libs/template/materialize.go @@ -45,7 +45,7 @@ func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir st helpers := loadHelpers(ctx) if _, err := os.Stat(schemaPath); os.IsNotExist(err) { - return fmt.Errorf("expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", schemaPath) + return fmt.Errorf("expected to find a template schema file at %s", schemaPath) } config, err := newConfig(ctx, schemaPath) diff --git a/libs/template/materialize_test.go b/libs/template/materialize_test.go index 8f55dbe583..aa19de1c47 100644 --- a/libs/template/materialize_test.go +++ b/libs/template/materialize_test.go @@ -12,5 +12,5 @@ import ( func TestMaterializeForNonTemplateDirectory(t *testing.T) { tmpDir := t.TempDir() err := Materialize(context.Background(), "", tmpDir, "") - assert.EqualError(t, err, fmt.Sprintf("expected to find a template schema file at %s. Valid bundle templates are expected to contain a schema file", filepath.Join(tmpDir, schemaFileName))) + assert.EqualError(t, err, fmt.Sprintf("expected to find a template schema file at %s", filepath.Join(tmpDir, schemaFileName))) } From b37332ce93b1595c7819e7e12d40948927aec86f Mon Sep 17 00:00:00 2001 From: monalisa Date: Wed, 15 Nov 2023 00:10:59 +0100 Subject: [PATCH 4/5] better error message --- libs/template/materialize.go | 2 +- libs/template/materialize_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/template/materialize.go b/libs/template/materialize.go index 052339e611..811ef9259b 100644 --- a/libs/template/materialize.go +++ b/libs/template/materialize.go @@ -45,7 +45,7 @@ func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir st helpers := loadHelpers(ctx) if _, err := os.Stat(schemaPath); os.IsNotExist(err) { - return fmt.Errorf("expected to find a template schema file at %s", schemaPath) + return fmt.Errorf("not a bundle template: expected to find a template schema file at %s", schemaPath) } config, err := newConfig(ctx, schemaPath) diff --git a/libs/template/materialize_test.go b/libs/template/materialize_test.go index aa19de1c47..f334cfd977 100644 --- a/libs/template/materialize_test.go +++ b/libs/template/materialize_test.go @@ -12,5 +12,5 @@ import ( func TestMaterializeForNonTemplateDirectory(t *testing.T) { tmpDir := t.TempDir() err := Materialize(context.Background(), "", tmpDir, "") - assert.EqualError(t, err, fmt.Sprintf("expected to find a template schema file at %s", filepath.Join(tmpDir, schemaFileName))) + assert.EqualError(t, err, fmt.Sprintf("not a bundle template: expected to find a template schema file at %s", filepath.Join(tmpDir, schemaFileName))) } From 0cdf78413de91a2e98a6f44a8dd671bb9849f17d Mon Sep 17 00:00:00 2001 From: monalisa Date: Wed, 22 Nov 2023 01:13:31 +0100 Subject: [PATCH 5/5] remove unrelated code --- libs/template/helpers.go | 5 +---- libs/template/materialize_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/libs/template/helpers.go b/libs/template/helpers.go index f3d927f510..7f306a3aa5 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -31,6 +31,7 @@ var cachedUser *iam.User var cachedIsServicePrincipal *bool func loadHelpers(ctx context.Context) template.FuncMap { + w := root.WorkspaceClient(ctx) return template.FuncMap{ "fail": func(format string, args ...any) (any, error) { return nil, ErrFail{fmt.Sprintf(format, args...)} @@ -64,7 +65,6 @@ func loadHelpers(ctx context.Context) template.FuncMap { }, // Get smallest node type (follows Terraform's GetSmallestNodeType) "smallest_node_type": func() (string, error) { - w := root.WorkspaceClient(ctx) if w.Config.Host == "" { return "", errors.New("cannot determine target workspace, please first setup a configuration profile using 'databricks configure'") } @@ -79,14 +79,12 @@ func loadHelpers(ctx context.Context) template.FuncMap { return string(os.PathSeparator) }, "workspace_host": func() (string, error) { - w := root.WorkspaceClient(ctx) if w.Config.Host == "" { return "", errors.New("cannot determine target workspace, please first setup a configuration profile using 'databricks configure'") } return w.Config.Host, nil }, "user_name": func() (string, error) { - w := root.WorkspaceClient(ctx) if cachedUser == nil { var err error cachedUser, err = w.CurrentUser.Me(ctx) @@ -101,7 +99,6 @@ func loadHelpers(ctx context.Context) template.FuncMap { return result, nil }, "is_service_principal": func() (bool, error) { - w := root.WorkspaceClient(ctx) if cachedIsServicePrincipal != nil { return *cachedIsServicePrincipal, nil } diff --git a/libs/template/materialize_test.go b/libs/template/materialize_test.go index f334cfd977..b4be3fe980 100644 --- a/libs/template/materialize_test.go +++ b/libs/template/materialize_test.go @@ -6,11 +6,19 @@ import ( "path/filepath" "testing" + "github.com/databricks/cli/cmd/root" + "github.com/databricks/databricks-sdk-go" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMaterializeForNonTemplateDirectory(t *testing.T) { tmpDir := t.TempDir() - err := Materialize(context.Background(), "", tmpDir, "") + w, err := databricks.NewWorkspaceClient(&databricks.Config{}) + require.NoError(t, err) + ctx := root.SetWorkspaceClient(context.Background(), w) + + // Try to materialize a non-template directory. + err = Materialize(ctx, "", tmpDir, "") assert.EqualError(t, err, fmt.Sprintf("not a bundle template: expected to find a template schema file at %s", filepath.Join(tmpDir, schemaFileName))) }