Skip to content

Commit

Permalink
[Feature] Automatically create parent_path folder if it doesn't exist
Browse files Browse the repository at this point in the history
This makes behavior consistent with other workspace objects - notebooks, workspace files,
etc.

I have the skeleton of the test for it, but it doesn't work because I can't mock the same
API call two times with different return results
  • Loading branch information
alexott committed Jul 16, 2024
1 parent 9e8fd30 commit cc14761
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion dashboards/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dashboards

import (
"context"
"log"
"strings"

"github.com/databricks/databricks-sdk-go/service/dashboards"
"github.com/databricks/terraform-provider-databricks/common"
Expand Down Expand Up @@ -76,7 +78,17 @@ func ResourceDashboard() common.Resource {
newDashboardRequest.SerializedDashboard = content
createdDashboard, err := w.Lakeview.Create(ctx, newDashboardRequest)
if err != nil {
return err
if isParentDoesntExistError(err) {
log.Printf("[DEBUG] Parent folder '%s' doesn't exist, creating...", newDashboardRequest.ParentPath)
err = w.Workspace.MkdirsByPath(ctx, newDashboardRequest.ParentPath)
if err != nil {
return err
}
createdDashboard, err = w.Lakeview.Create(ctx, newDashboardRequest)
}
if err != nil {
return err
}
}

d.Set("etag", createdDashboard.Etag)
Expand Down Expand Up @@ -168,3 +180,8 @@ func ResourceDashboard() common.Resource {
},
}
}

func isParentDoesntExistError(err error) bool {
errStr := err.Error()
return strings.HasPrefix(errStr, "Path (") && strings.HasSuffix(errStr, ") doesn't exist.")
}
2 changes: 1 addition & 1 deletion docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The following arguments are supported:
* `serialized_dashboard` - (Optional) The contents of the dashboard in serialized string form. Conflicts with `file_path`.
* `file_path` - (Optional) The path to the dashboard JSON file. Conflicts with `serialized_dashboard`.
* `embed_credentials` - (Optional) Whether to embed credentials in the dashboard. Default is `true`.
* `parent_path` - (Required) The workspace path of the folder containing the dashboard. Includes leading slash and no trailing slash.
* `parent_path` - (Required) The workspace path of the folder containing the dashboard. Includes leading slash and no trailing slash. If folder doesn't exist, it will be created.

## Attribute Reference

Expand Down

0 comments on commit cc14761

Please sign in to comment.