diff --git a/dashboards/resource_dashboard.go b/dashboards/resource_dashboard.go index b9bdc5e15e..3cfcbb2ec1 100644 --- a/dashboards/resource_dashboard.go +++ b/dashboards/resource_dashboard.go @@ -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" @@ -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) @@ -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.") +} diff --git a/docs/resources/dashboard.md b/docs/resources/dashboard.md index b18ce755d3..e1215bd515 100644 --- a/docs/resources/dashboard.md +++ b/docs/resources/dashboard.md @@ -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