forked from enuan/grafana-tools-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-library-element.go
147 lines (137 loc) · 3.89 KB
/
rest-library-element.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sdk
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
)
type Element struct {
ID uint `json:"id"`
OrgId uint `json:"orgId"`
FolderId uint `json:"folderId"`
UID string `json:"uid"`
Name string `json:"name"`
Kind uint `json:"kind"`
Type string `json:"type"`
Description string `json:"description"`
//Model string `json:"model"` //TODO verificare cosa ritorna, doc non chiara
Version uint `json:"version"`
Meta Meta `json:"meta"`
}
type Result struct {
TotalCount uint `json:"totalCount"`
Page uint `json:"page"`
PerPage uint `json:"perPage"`
Elements []Element `json:"elements"`
}
// FoundBoard keeps result of search with metadata of a dashboard.
type FoundLibraryElement struct {
Result Result `json:"result"`
}
// SearchLibraryElements gets all library element.
// Reflects GET /api/library-elements API call.
func (r *Client) SearchLibraryElements(ctx context.Context) (FoundLibraryElement, error) {
var (
raw []byte
libElems FoundLibraryElement
code int
err error
)
u := url.URL{}
q := u.Query()
q.Set("perpage", strconv.FormatUint(uint64(1000), 10))
if raw, code, err = r.get(ctx, "api/library-elements", q); err != nil {
return libElems, err
}
if code != 200 {
return libElems, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
err = json.Unmarshal(raw, &libElems)
return libElems, err
}
// GetLibraryElementByUID gets library element by uid.
// Reflects GET /api/library-elements/:uid API call.
func (r *Client) GetLibraryElementByUID(ctx context.Context, UID string) (LibraryElement, error) {
var (
raw []byte
libElem LibraryElement
err error
)
/*if raw, code, err = r.get(ctx, fmt.Sprintf("/api/library-elements/%s", UID), nil); err != nil {
return libElem, err
}
if code != 200 {
return libElem, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}*/
if raw, err = r.GetRawLibraryElementByUID(ctx, UID); err != nil {
return libElem, err
}
err = json.Unmarshal(raw, &libElem)
return libElem, err
}
func (r *Client) GetRawLibraryElementByUID(ctx context.Context, UID string) ([]byte, error) {
var (
raw []byte
code int
err error
)
if raw, code, err = r.get(ctx, fmt.Sprintf("/api/library-elements/%s", UID), nil); err != nil {
return nil, err
}
if code != 200 {
return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
return raw, err
}
// GetLibraryElementByName gets library element by uid.
// Reflects GET /api/library-elements/name/:uid API call.
func (r *Client) GetLibraryElementByName(ctx context.Context, Name string) (LibraryElement, error) {
var (
raw []byte
libElem LibraryElement
code int
err error
)
if raw, code, err = r.get(ctx, fmt.Sprintf("/api/library-elements/name/%s", Name), nil); err != nil {
return libElem, err
}
if code != 200 {
return libElem, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
//fmt.Printf("raw: >>>\n%s\n<<<\n", string(raw[:]))
err = json.Unmarshal(raw, &libElem)
return libElem, err
}
func (r *Client) GetRawLibraryElementByName(ctx context.Context, name string) ([]byte, error) {
var (
raw []byte
code int
err error
)
if raw, code, err = r.get(ctx, fmt.Sprintf("/api/library-elements/name/%s", name), nil); err != nil {
return nil, err
}
if code != 200 {
return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
return raw, err
}
// UpdateFolderByUID update folder by uid
// Reflects PATCH /api/library-elements/:uid API call.
func (r *Client) UpdateRawLibraryElementByUID(ctx context.Context, uid string, rawLibElem []byte) error {
var (
rf Folder
code int
err error
)
raw, code, err := r.patch(ctx, fmt.Sprintf("api/library-elements/%s", uid), nil, rawLibElem)
if err != nil {
return err
}
if code != 200 {
return fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
err = json.Unmarshal(raw, &rf)
return err
}