diff --git a/annotation.go b/annotation.go index fb5eb4e..21f7172 100644 --- a/annotation.go +++ b/annotation.go @@ -62,6 +62,23 @@ 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) { + as, err := c.Annotations(params) + if err != nil { + return Annotation{}, err + } + + for _, a := range as { + if a.ID == id { + return a, nil + } + } + + return Annotation{}, fmt.Errorf("annotation %v not found", id) +} + // 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()