From 7ba7df983331a97b81c5e5a521c8520b5b1cfabc Mon Sep 17 00:00:00 2001 From: pranav-new-relic <127438038+pranav-new-relic@users.noreply.github.com> Date: Mon, 19 Jun 2023 09:21:29 +0530 Subject: [PATCH] fix(dashboards): remove omitempty on widgets to allow creating pages with no widgets (#1033) --- .../dashboards_widgets_integration_test.go | 84 +++++++++++++++++++ pkg/dashboards/types.go | 14 +++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/pkg/dashboards/dashboards_widgets_integration_test.go b/pkg/dashboards/dashboards_widgets_integration_test.go index 069164d53..74a3a45e7 100644 --- a/pkg/dashboards/dashboards_widgets_integration_test.go +++ b/pkg/dashboards/dashboards_widgets_integration_test.go @@ -149,3 +149,87 @@ func TestIntegrationDashboard_Billboard(t *testing.T) { assert.Equal(t, 0, len(delRes.Errors)) assert.Equal(t, DashboardDeleteResultStatusTypes.SUCCESS, delRes.Status) } + +// TestIntegrationDashboard_EmptyPage tests creating a dashboard with a page comprising no widgets +func TestIntegrationDashboard_EmptyPage(t *testing.T) { + t.Parallel() + + testAccountID, err := mock.GetTestAccountID() + if err != nil { + t.Skipf("%s", err) + } + + client := newIntegrationTestClient(t) + + dashboardName := "newrelic-client-go-test-dashboard-empty-pages" + mock.RandSeq(5) + dashboardInput := DashboardInput{ + Description: "newrelic-client-go-test-dashboard-description", + Name: dashboardName, + Permissions: entities.DashboardPermissionsTypes.PUBLIC_READ_WRITE, + Pages: []DashboardPageInput{{ + Name: "Test Page", + Widgets: []DashboardWidgetInput{}, + }}, + } + + // Test: Create Dashboard + result, err := client.DashboardCreate(testAccountID, dashboardInput) + + require.NoError(t, err) + require.NotNil(t, result) + assert.Equal(t, 0, len(result.Errors)) + require.NotNil(t, result.EntityResult.GUID) + + dashGUID := result.EntityResult.GUID + + // Test: Get Dashboard + dash, err := client.GetDashboardEntity(dashGUID) + require.NoError(t, err) + require.NotNil(t, dash) + + assert.Equal(t, dashGUID, dash.GUID) + assert.Equal(t, dashboardInput.Description, dash.Description) + assert.Equal(t, dashboardInput.Name, dash.Name) + assert.Equal(t, dashboardInput.Permissions, dash.Permissions) + + // Test: Update Dashboard + updatedDashboard := DashboardInput{ + Name: dash.Name, + Permissions: dash.Permissions, + Pages: []DashboardPageInput{ + { + Name: dash.Pages[0].Name, + Widgets: []DashboardWidgetInput{ + { + Title: "Test BillboardText Widget", + Configuration: DashboardWidgetConfigurationInput{ + Billboard: &DashboardBillboardWidgetConfigurationInput{ + NRQLQueries: []DashboardWidgetNRQLQueryInput{ + { + AccountID: testAccountID, + Query: "FROM Metric SELECT 1", + }, + }, + }, + }, + }, + }, + }, + { + Name: "Test Page Two", + Widgets: []DashboardWidgetInput{}, + }, + }, + } + + upDash, err := client.DashboardUpdate(updatedDashboard, dashGUID) + require.NoError(t, err) + require.NotNil(t, upDash) + + //// Test: Delete Dashboard + delRes, err := client.DashboardDelete(dashGUID) + require.NoError(t, err) + require.NotNil(t, delRes) + assert.Equal(t, 0, len(delRes.Errors)) + assert.Equal(t, DashboardDeleteResultStatusTypes.SUCCESS, delRes.Status) +} diff --git a/pkg/dashboards/types.go b/pkg/dashboards/types.go index e9256a7a7..56070cd42 100644 --- a/pkg/dashboards/types.go +++ b/pkg/dashboards/types.go @@ -258,8 +258,13 @@ type DashboardPageInput struct { GUID common.EntityGUID `json:"guid,omitempty"` // The name of the page. Name string `json:"name"` + + // NOTE: The JSON description of the following attribute, "Widgets" has been modified manually + // (removal of "omitempty") to facilitate creating pages with no widgets (empty pages). + // Please DO NOT regenerate/modify this attribute and its datatype via Tutone (which would add "omitempty" back). + // A nested block of all widgets belonging to the page. - Widgets []DashboardWidgetInput `json:"widgets,omitempty"` + Widgets []DashboardWidgetInput `json:"widgets"` } // DashboardPieWidgetConfigurationInput - Configuration for visualization type 'viz.pie'. Learn more about [pie](https://docs.newrelic.com/docs/apis/nerdgraph/examples/create-widgets-dashboards-api/#pie) widget. @@ -312,8 +317,13 @@ type DashboardUpdatePageInput struct { Description string `json:"description,omitempty"` // Page name. Name string `json:"name"` + + // NOTE: The JSON description of the following attribute, "Widgets" has been modified manually + // (removal of "omitempty") to facilitate creating pages with no widgets (empty pages). + // Please DO NOT regenerate/modify this attribute and its datatype via Tutone (which would add "omitempty" back). + // Page widgets. - Widgets []DashboardWidgetInput `json:"widgets,omitempty"` + Widgets []DashboardWidgetInput `json:"widgets"` } // DashboardUpdatePageResult - Result of updatePage operation.