Skip to content

Commit

Permalink
Merge branch 'dashboards-generate' into demo-dashboards
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Oct 22, 2024
2 parents e448ed2 + ef6b2ea commit e62e666
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
34 changes: 31 additions & 3 deletions cmd/bundle/generate/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generate
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/databricks/cli/bundle/config/generate"
"github.com/databricks/cli/bundle/deploy/terraform"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/bundle/render"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
Expand Down Expand Up @@ -64,8 +66,24 @@ func (d *dashboard) resolveFromPath(ctx context.Context, b *bundle.Bundle) (stri
obj, err := w.Workspace.GetStatusByPath(ctx, d.dashboardPath)
if err != nil {
if apierr.IsMissing(err) {
return "", diag.Errorf("dashboard at path %q not found", d.dashboardPath)
return "", diag.Errorf("dashboard %q not found", path.Base(d.dashboardPath))
}

// Emit a more descriptive error message for legacy dashboards.
if errors.Is(err, apierr.ErrBadRequest) && strings.HasPrefix(err.Error(), "dbsqlDashboard ") {
return "", diag.Diagnostics{
{
Severity: diag.Error,
Summary: fmt.Sprintf("dashboard %q is a legacy dashboard", path.Base(d.dashboardPath)),
Detail: "" +
"Databricks Asset Bundles work exclusively with AI/BI dashboards.\n" +
"\n" +
"Instructions on how to convert a legacy dashboard to an AI/BI dashboard\n" +
"can be found at: https://docs.databricks.com/en/dashboards/clone-legacy-to-aibi.html.",
},
}
}

return "", diag.FromErr(err)
}

Expand Down Expand Up @@ -184,7 +202,7 @@ func (d *dashboard) saveConfiguration(ctx context.Context, b *bundle.Bundle, das
}

// Save the configuration to the resource directory.
resourcePath := filepath.Join(d.resourceDir, fmt.Sprintf("%s.yml", key))
resourcePath := filepath.Join(d.resourceDir, fmt.Sprintf("%s.dashboard.yml", key))
saver := yamlsaver.NewSaverWithStyle(map[string]yaml.Style{
"display_name": yaml.DoubleQuotedStyle,
})
Expand Down Expand Up @@ -347,7 +365,17 @@ func (d *dashboard) RunE(cmd *cobra.Command, args []string) error {
diags = d.runForExisting(ctx, b)
}

return diags.Error()
renderOpts := render.RenderOptions{RenderSummaryTable: false}
err := render.RenderDiagnostics(cmd.OutOrStdout(), b, diags, renderOpts)
if err != nil {
return fmt.Errorf("failed to render output: %w", err)
}

if diags.HasError() {
return root.ErrAlreadyPrinted
}

return nil
}

func NewGenerateDashboardCommand() *cobra.Command {
Expand Down
42 changes: 42 additions & 0 deletions cmd/bundle/generate/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
package generate

import (
"context"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestDashboard_ErrorOnLegacyDashboard(t *testing.T) {
// Response to a GetStatus request on a path pointing to a legacy dashboard.
//
// < HTTP/2.0 400 Bad Request
// < {
// < "error_code": "BAD_REQUEST",
// < "message": "dbsqlDashboard is not user-facing."
// < }

d := dashboard{
dashboardPath: "/path/to/legacy dashboard",
}

m := mocks.NewMockWorkspaceClient(t)
w := m.GetMockWorkspaceAPI()
w.On("GetStatusByPath", mock.Anything, "/path/to/legacy dashboard").Return(nil, &apierr.APIError{
StatusCode: 400,
ErrorCode: "BAD_REQUEST",
Message: "dbsqlDashboard is not user-facing.",
})

ctx := context.Background()
b := &bundle.Bundle{}
b.SetWorkpaceClient(m.WorkspaceClient)

_, diags := d.resolveID(ctx, b)
require.Len(t, diags, 1)
assert.Equal(t, diags[0].Summary, "dashboard \"legacy dashboard\" is a legacy dashboard")
}

0 comments on commit e62e666

Please sign in to comment.