From 93155f1c77ef2093187eb81c22d30b680f03e91e Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 24 Oct 2024 16:24:06 +0200 Subject: [PATCH] Inline SDK struct --- bundle/config/mutator/apply_presets.go | 2 +- .../configure_dashboard_defaults_test.go | 9 +++++-- bundle/config/mutator/initialize_urls_test.go | 7 +++-- .../mutator/process_target_mode_test.go | 7 ++++- bundle/config/resources/dashboard.go | 27 +++++-------------- .../check_modified_dashboards_test.go | 4 ++- bundle/deploy/terraform/convert_test.go | 13 ++++++--- .../terraform/tfdyn/convert_dashboard_test.go | 10 ++++--- 8 files changed, 46 insertions(+), 33 deletions(-) diff --git a/bundle/config/mutator/apply_presets.go b/bundle/config/mutator/apply_presets.go index 3d1f3d766..d2a1d0c7d 100644 --- a/bundle/config/mutator/apply_presets.go +++ b/bundle/config/mutator/apply_presets.go @@ -214,7 +214,7 @@ func (m *applyPresets) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos // Dashboards: Prefix for key, dashboard := range r.Dashboards { - if dashboard == nil { + if dashboard == nil || dashboard.CreateDashboardRequest == nil { diags = diags.Extend(diag.Errorf("dashboard %s s is not defined", key)) continue } diff --git a/bundle/config/mutator/configure_dashboard_defaults_test.go b/bundle/config/mutator/configure_dashboard_defaults_test.go index 98b0e37e1..4804b7159 100644 --- a/bundle/config/mutator/configure_dashboard_defaults_test.go +++ b/bundle/config/mutator/configure_dashboard_defaults_test.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/bundle/config/resources" "github.com/databricks/cli/bundle/internal/bundletest" "github.com/databricks/cli/libs/dyn" + "github.com/databricks/databricks-sdk-go/service/dashboards" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -25,11 +26,15 @@ func TestConfigureDashboardDefaultsParentPath(t *testing.T) { "d1": { // Empty string is skipped. // See below for how it is set. - ParentPath: "", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + ParentPath: "", + }, }, "d2": { // Non-empty string is skipped. - ParentPath: "already-set", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + ParentPath: "already-set", + }, }, "d3": { // No parent path set. diff --git a/bundle/config/mutator/initialize_urls_test.go b/bundle/config/mutator/initialize_urls_test.go index bfc0b5649..61103de80 100644 --- a/bundle/config/mutator/initialize_urls_test.go +++ b/bundle/config/mutator/initialize_urls_test.go @@ -8,6 +8,7 @@ import ( "github.com/databricks/cli/bundle/config/resources" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/databricks/databricks-sdk-go/service/dashboards" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/databricks/databricks-sdk-go/service/ml" "github.com/databricks/databricks-sdk-go/service/pipelines" @@ -87,8 +88,10 @@ func TestInitializeURLs(t *testing.T) { }, Dashboards: map[string]*resources.Dashboard{ "dashboard1": { - ID: "01ef8d56871e1d50ae30ce7375e42478", - DisplayName: "My special dashboard", + ID: "01ef8d56871e1d50ae30ce7375e42478", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "My special dashboard", + }, }, }, }, diff --git a/bundle/config/mutator/process_target_mode_test.go b/bundle/config/mutator/process_target_mode_test.go index 3b2aff00e..4346e88fe 100644 --- a/bundle/config/mutator/process_target_mode_test.go +++ b/bundle/config/mutator/process_target_mode_test.go @@ -14,6 +14,7 @@ import ( sdkconfig "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/databricks/databricks-sdk-go/service/dashboards" "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/databricks/databricks-sdk-go/service/ml" @@ -124,7 +125,11 @@ func mockBundle(mode config.Mode) *bundle.Bundle { "cluster1": {ClusterSpec: &compute.ClusterSpec{ClusterName: "cluster1", SparkVersion: "13.2.x", NumWorkers: 1}}, }, Dashboards: map[string]*resources.Dashboard{ - "dashboard1": {DisplayName: "dashboard1"}, + "dashboard1": { + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "dashboard1", + }, + }, }, }, }, diff --git a/bundle/config/resources/dashboard.go b/bundle/config/resources/dashboard.go index 0eb104c6a..462dbc564 100644 --- a/bundle/config/resources/dashboard.go +++ b/bundle/config/resources/dashboard.go @@ -17,27 +17,18 @@ type Dashboard struct { ModifiedStatus ModifiedStatus `json:"modified_status,omitempty" bundle:"internal"` URL string `json:"url,omitempty" bundle:"internal"` - // =========================== - // === BEGIN OF API FIELDS === - // =========================== + *dashboards.CreateDashboardRequest - // DisplayName is the display name of the dashboard (both as title and as basename in the workspace). - DisplayName string `json:"display_name"` - - // WarehouseID is the ID of the SQL Warehouse used to run the dashboard's queries. - WarehouseID string `json:"warehouse_id"` + // ========================= + // === Additional fields === + // ========================= // SerializedDashboard holds the contents of the dashboard in serialized JSON form. - // Note: its type is any and not string such that it can be inlined as YAML. - // If it is not a string, its contents will be marshalled as JSON. + // We override the field's type from the SDK struct here to allow for inlining as YAML. + // If the value is a string, it is used as is. + // If it is not a string, its contents is marshalled as JSON. SerializedDashboard any `json:"serialized_dashboard,omitempty"` - // ParentPath is the workspace path of the folder containing the dashboard. - // Includes leading slash and no trailing slash. - // - // Defaults to ${workspace.resource_path} if not set. - ParentPath string `json:"parent_path,omitempty"` - // EmbedCredentials is a flag to indicate if the publisher's credentials should // be embedded in the published dashboard. These embedded credentials will be used // to execute the published dashboard's queries. @@ -45,10 +36,6 @@ type Dashboard struct { // Defaults to false if not set. EmbedCredentials bool `json:"embed_credentials,omitempty"` - // =========================== - // ==== END OF API FIELDS ==== - // =========================== - // FilePath points to the local `.lvdash.json` file containing the dashboard definition. FilePath string `json:"file_path,omitempty"` } diff --git a/bundle/deploy/terraform/check_modified_dashboards_test.go b/bundle/deploy/terraform/check_modified_dashboards_test.go index ff6f19b83..c5e4c40d3 100644 --- a/bundle/deploy/terraform/check_modified_dashboards_test.go +++ b/bundle/deploy/terraform/check_modified_dashboards_test.go @@ -29,7 +29,9 @@ func mockDashboardBundle(t *testing.T) *bundle.Bundle { Resources: config.Resources{ Dashboards: map[string]*resources.Dashboard{ "dash1": { - DisplayName: "My Special Dashboard", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "My Special Dashboard", + }, }, }, }, diff --git a/bundle/deploy/terraform/convert_test.go b/bundle/deploy/terraform/convert_test.go index dab8890b3..3f69bbed4 100644 --- a/bundle/deploy/terraform/convert_test.go +++ b/bundle/deploy/terraform/convert_test.go @@ -12,6 +12,7 @@ import ( "github.com/databricks/cli/libs/dyn/convert" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/databricks/databricks-sdk-go/service/dashboards" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/databricks/databricks-sdk-go/service/ml" "github.com/databricks/databricks-sdk-go/service/pipelines" @@ -791,7 +792,9 @@ func TestTerraformToBundleEmptyRemoteResources(t *testing.T) { }, Dashboards: map[string]*resources.Dashboard{ "test_dashboard": { - DisplayName: "test_dashboard", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "test_dashboard", + }, }, }, }, @@ -948,10 +951,14 @@ func TestTerraformToBundleModifiedResources(t *testing.T) { }, Dashboards: map[string]*resources.Dashboard{ "test_dashboard": { - DisplayName: "test_dashboard", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "test_dashboard", + }, }, "test_dashboard_new": { - DisplayName: "test_dashboard_new", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "test_dashboard_new", + }, }, }, }, diff --git a/bundle/deploy/terraform/tfdyn/convert_dashboard_test.go b/bundle/deploy/terraform/tfdyn/convert_dashboard_test.go index dfb2ffa44..0c4866524 100644 --- a/bundle/deploy/terraform/tfdyn/convert_dashboard_test.go +++ b/bundle/deploy/terraform/tfdyn/convert_dashboard_test.go @@ -8,15 +8,19 @@ import ( "github.com/databricks/cli/bundle/internal/tf/schema" "github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/dyn/convert" + "github.com/databricks/databricks-sdk-go/service/dashboards" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestConvertDashboard(t *testing.T) { var src = resources.Dashboard{ - DisplayName: "my dashboard", - WarehouseID: "f00dcafe", - ParentPath: "/some/path", + CreateDashboardRequest: &dashboards.CreateDashboardRequest{ + DisplayName: "my dashboard", + WarehouseId: "f00dcafe", + ParentPath: "/some/path", + }, + EmbedCredentials: true, Permissions: []resources.Permission{