From d7a8666be976896084522354dba1aa4711463633 Mon Sep 17 00:00:00 2001 From: Kuzminykh Aleksandr Olegovich Date: Tue, 19 Oct 2021 12:12:48 +0300 Subject: [PATCH] Fix templating init with options field and add integration test (#173) --- board.go | 2 +- rest-dashboard_integration_test.go | 79 +++++++++++++++++++ ...pty-dashboard-with-uid-templating-8.2.json | 56 +++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 testdata/empty-dashboard-with-uid-templating-8.2.json diff --git a/board.go b/board.go index da8edf1f..e3aaa1fd 100644 --- a/board.go +++ b/board.go @@ -85,7 +85,7 @@ type ( AutoCount *int `json:"auto_count,omitempty"` Datasource *string `json:"datasource"` Refresh BoolInt `json:"refresh"` - Options []Option `json:"options"` + Options []Option `json:"options,omitempty"` IncludeAll bool `json:"includeAll"` AllFormat string `json:"allFormat"` AllValue string `json:"allValue"` diff --git a/rest-dashboard_integration_test.go b/rest-dashboard_integration_test.go index dd2eef9d..0f4ed975 100644 --- a/rest-dashboard_integration_test.go +++ b/rest-dashboard_integration_test.go @@ -116,3 +116,82 @@ func Test_Dashboard_CRUD_By_UID(t *testing.T) { } } + +func Test_Dashboard_CRUD_Templating(t *testing.T) { + var ( + err error + boardResult sdk.Board + properties sdk.BoardProperties + ) + + shouldSkip(t) + ctx := context.Background() + client := getClient(t) + + var board sdk.Board + + raw, _ := ioutil.ReadFile("testdata/empty-dashboard-with-uid-templating-8.2.json") + + if err = json.Unmarshal(raw, &board); err != nil { + t.Fatal(err) + } + + //Cleanup if Already exists + if _, err = client.DeleteDashboardByUID(ctx, board.UID); err != nil { + t.Fatal(err) + } + + if _, err = client.SetRawDashboard(ctx, raw); err != nil { + t.Fatal(err) + } + + if boardResult, properties, err = client.GetDashboardByUID(ctx, board.UID); err != nil { + t.Fatal(err) + } + + params := sdk.SetDashboardParams{ + FolderID: properties.FolderID, + Overwrite: true, + } + if _, err = client.SetDashboard(ctx, boardResult, params); err != nil { + t.Fatal(err) + } + + if raw, _, err = client.GetRawDashboardByUID(ctx, board.UID); err != nil { + t.Fatal(err) + } + + { + var boardMap map[string]interface{} + + err = json.Unmarshal(raw, &boardMap) + if err != nil { + t.Fatal(err) + } + + templatingMap, found := boardMap["templating"].(map[string]interface{}) + if !found { + t.Fatal() + } + + list, found := templatingMap["list"].([]interface{}) + if !found { + t.Fatal() + } + + _, found = list[0].(map[string]interface{})["options"] + if found { + t.Fatal() + } + } + + //Remove the dashboard that was created. + if _, err = client.DeleteDashboardByUID(ctx, board.UID); err != nil { + t.Fatal(err) + } + + //Verify that it has been deleted + if boardResult, _, err = client.GetDashboardByUID(ctx, board.UID); err == nil { + t.Fatal("Failed to delete dashboard, it can still be retrieved") + } +} diff --git a/testdata/empty-dashboard-with-uid-templating-8.2.json b/testdata/empty-dashboard-with-uid-templating-8.2.json new file mode 100644 index 00000000..3a00f32a --- /dev/null +++ b/testdata/empty-dashboard-with-uid-templating-8.2.json @@ -0,0 +1,56 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": "-- Grafana --", + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "target": { + "limit": 100, + "matchAny": false, + "tags": [], + "type": "dashboard" + }, + "type": "dashboard" + } + ] + }, + "editable": true, + "fiscalYearStartMonth": 0, + "gnetId": null, + "graphTooltip": 0, + "id": 15, + "iteration": 1634566582776, + "links": [], + "liveNow": false, + "panels": [], + "schemaVersion": 31, + "style": "dark", + "tags": [], + "templating": { + "list": [ + { + "description": null, + "error": null, + "hide": 2, + "label": null, + "name": "period", + "query": "1d", + "skipUrlSync": false, + "type": "constant" + } + ] + }, + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": {}, + "timezone": "", + "title": "broken", + "uid": "ytYpBcOnz", + "version": 1 +}