From 89fd95fa8a505da8cf42a26770bbec78bf7e9309 Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Fri, 5 Jun 2020 07:30:47 -0400 Subject: [PATCH 1/2] add Annotation method to get an annotation This adds a convenience method for fetching an individual annotation. Note that the [Grafana API](https://grafana.com/docs/grafana/latest/http_api/annotations/) documentation documents no method for paging through results. --- annotation.go | 23 +++++++++++++++++++++ annotation_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/annotation.go b/annotation.go index fb5eb4e..296d935 100644 --- a/annotation.go +++ b/annotation.go @@ -62,6 +62,29 @@ func (c *Client) Annotations(params url.Values) ([]Annotation, error) { return result, err } +// Annotation fetches the annotation queried with the ID and params it's passed. +// It returns an error if no annotation with a matching ID is found. +func (c *Client) Annotation(id int64, params url.Values) (Annotation, error) { + anno := Annotation{} + as, err := c.Annotations(params) + if err != nil { + return Annotation{}, err + } + + for _, a := range as { + if a.ID == id { + anno = a + break + } + } + + if anno.ID == 0 { + return anno, fmt.Errorf("annotation %v not found", id) + } + + return anno, nil +} + // NewAnnotation creates a new annotation with the Annotation it is passed func (c *Client) NewAnnotation(a *Annotation) (int64, error) { data, err := json.Marshal(a) diff --git a/annotation_test.go b/annotation_test.go index 2e53322..2da8b33 100644 --- a/annotation_test.go +++ b/annotation_test.go @@ -27,6 +27,25 @@ const ( "tag2" ], "data": {} + }, { + "id": 1125, + "alertId": 0, + "dashboardId": 468, + "panelId": 2, + "userId": 1, + "userName": "", + "newState": "", + "prevState": "", + "time": 1507266395000, + "text": "test", + "metric": "", + "regionId": 1123, + "type": "event", + "tags": [ + "tag1", + "tag2" + ], + "data": {} }]` newAnnotationJSON = `{ @@ -68,6 +87,38 @@ func TestAnnotations(t *testing.T) { } } +func TestAnnotation(t *testing.T) { + server, client := gapiTestTools(200, annotationsJSON) + defer server.Close() + + a, err := client.Annotation(1124, url.Values{}) + if err != nil { + t.Error(err) + } + + t.Log(pretty.PrettyFormat(a)) + + if a.ID != 1124 { + t.Error("annotation response should contain the annotation with the correct ID") + } +} + +func TestAnnotation_noneFound(t *testing.T) { + server, client := gapiTestTools(200, annotationsJSON) + defer server.Close() + + a, err := client.Annotation(1, url.Values{}) + if err.Error() != "annotation 1 not found" { + t.Error(err) + } + + t.Log(pretty.PrettyFormat(a)) + + if a.ID != 0 { + t.Error("annotation response should contain an empty annotation when no annotation with a matching ID was found") + } +} + func TestNewAnnotation(t *testing.T) { server, client := gapiTestTools(200, newAnnotationJSON) defer server.Close() From c22e22f640f9873482e6d8973c0defc09bb1d518 Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Sat, 6 Jun 2020 07:21:15 -0400 Subject: [PATCH 2/2] simplify Annotation method --- annotation.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/annotation.go b/annotation.go index 296d935..21f7172 100644 --- a/annotation.go +++ b/annotation.go @@ -65,7 +65,6 @@ func (c *Client) Annotations(params url.Values) ([]Annotation, error) { // Annotation fetches the annotation queried with the ID and params it's passed. // It returns an error if no annotation with a matching ID is found. func (c *Client) Annotation(id int64, params url.Values) (Annotation, error) { - anno := Annotation{} as, err := c.Annotations(params) if err != nil { return Annotation{}, err @@ -73,16 +72,11 @@ func (c *Client) Annotation(id int64, params url.Values) (Annotation, error) { for _, a := range as { if a.ID == id { - anno = a - break + return a, nil } } - if anno.ID == 0 { - return anno, fmt.Errorf("annotation %v not found", id) - } - - return anno, nil + return Annotation{}, fmt.Errorf("annotation %v not found", id) } // NewAnnotation creates a new annotation with the Annotation it is passed