From 2502434a7cc4d81d7d9f547d53487fccae718ed0 Mon Sep 17 00:00:00 2001 From: Jason Anderson Date: Fri, 13 Sep 2024 15:25:06 -0700 Subject: [PATCH] expose annotations to linter these are structured similarly to the 'templating' field, but in general do not reliably have a 'type' field exposed. --- lint/lint.go | 12 ++++++++++++ lint/model_test.go | 5 +++++ lint/testdata/dashboard.json | 15 +++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/lint/lint.go b/lint/lint.go index 59f0332..7350b9a 100644 --- a/lint/lint.go +++ b/lint/lint.go @@ -181,6 +181,15 @@ func (t *Target) GetDataSource() (Datasource, error) { return GetDataSource(t.Datasource) } +type Annotation struct { + Name string `json:"name"` + Datasource interface{} `json:"datasource,omitempty"` +} + +func (a *Annotation) GetDataSource() (Datasource, error) { + return GetDataSource(a.Datasource) +} + // Panel is a deliberately incomplete representation of the Dashboard -> Panel type in grafana. // The properties which are extracted from JSON are only those used for linting purposes. type Panel struct { @@ -271,6 +280,9 @@ type Dashboard struct { Templating struct { List []Template `json:"list"` } `json:"templating"` + Annotations struct { + List []Annotation `json:"list"` + } `json:"annotations"` Rows []Row `json:"rows,omitempty"` Panels []Panel `json:"panels,omitempty"` Editable bool `json:"editable,omitempty"` diff --git a/lint/model_test.go b/lint/model_test.go index f40bd0c..324c1fe 100644 --- a/lint/model_test.go +++ b/lint/model_test.go @@ -68,6 +68,11 @@ func TestParseDashboard(t *testing.T) { assert.NoError(t, err) assert.Len(t, dashboard.GetPanels(), 4) }) + t.Run("Annotations", func(t *testing.T) { + dashboard, err := NewDashboard(sampleDashboard) + assert.NoError(t, err) + assert.Len(t, dashboard.Annotations.List, 1) + }) } func TestParseTemplateValue(t *testing.T) { diff --git a/lint/testdata/dashboard.json b/lint/testdata/dashboard.json index 5f15cf9..36e0405 100644 --- a/lint/testdata/dashboard.json +++ b/lint/testdata/dashboard.json @@ -8,6 +8,21 @@ "pluginId": "prom" } ], + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, "rows": [ { "panels": [