Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

add Annotation method to get an annotation #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `{
Expand Down Expand Up @@ -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()
Expand Down