This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
dashboard.go
113 lines (100 loc) · 3.32 KB
/
dashboard.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
package client
import (
"context"
"fmt"
"github.com/suzuki-shunsuke/go-graylog"
)
// CreateDashboard creates a new dashboard account.
func (client *Client) CreateDashboard(dashboard *graylog.Dashboard) (*ErrorInfo, error) {
return client.CreateDashboardContext(context.Background(), dashboard)
}
// CreateDashboardContext creates a new dashboard account with a context.
func (client *Client) CreateDashboardContext(
ctx context.Context, dashboard *graylog.Dashboard,
) (*ErrorInfo, error) {
if dashboard == nil {
return nil, fmt.Errorf("dashboard is nil")
}
ret := map[string]string{}
ei, err := client.callPost(ctx, client.Endpoints().Dashboards(), dashboard, &ret)
if err != nil {
return ei, err
}
if id, ok := ret["dashboard_id"]; ok {
dashboard.ID = id
return ei, nil
}
return ei, fmt.Errorf(`response doesn't have the field "dashboard_id"`)
}
// DeleteDashboard deletes a given dashboard.
func (client *Client) DeleteDashboard(id string) (*ErrorInfo, error) {
return client.DeleteDashboardContext(context.Background(), id)
}
// DeleteDashboardContext deletes a given dashboard with a context.
func (client *Client) DeleteDashboardContext(
ctx context.Context, id string,
) (*ErrorInfo, error) {
if id == "" {
return nil, fmt.Errorf("id is empty")
}
u, err := client.Endpoints().Dashboard(id)
if err != nil {
return nil, err
}
return client.callDelete(ctx, u.String(), nil, nil)
}
// GetDashboard returns a given dashboard.
func (client *Client) GetDashboard(id string) (*graylog.Dashboard, *ErrorInfo, error) {
return client.GetDashboardContext(context.Background(), id)
}
// GetDashboardContext returns a given dashboard with a context.
func (client *Client) GetDashboardContext(
ctx context.Context, id string,
) (*graylog.Dashboard, *ErrorInfo, error) {
if id == "" {
return nil, nil, fmt.Errorf("id is empty")
}
u, err := client.Endpoints().Dashboard(id)
if err != nil {
return nil, nil, err
}
dashboard := &graylog.Dashboard{}
ei, err := client.callGet(ctx, u.String(), nil, dashboard)
return dashboard, ei, err
}
// GetDashboards returns all dashboards.
func (client *Client) GetDashboards() ([]graylog.Dashboard, int, *ErrorInfo, error) {
return client.GetDashboardsContext(context.Background())
}
// GetDashboardsContext returns all dashboards with a context.
func (client *Client) GetDashboardsContext(ctx context.Context) ([]graylog.Dashboard, int, *ErrorInfo, error) {
dashboards := &graylog.DashboardsBody{}
ei, err := client.callGet(
ctx, client.Endpoints().Dashboards(), nil, dashboards)
return dashboards.Dashboards, dashboards.Total, ei, err
}
// UpdateDashboard updates a given dashboard.
func (client *Client) UpdateDashboard(prms *graylog.Dashboard) (*ErrorInfo, error) {
return client.UpdateDashboardContext(context.Background(), prms)
}
// UpdateDashboardContext updates a given dashboard with a context.
func (client *Client) UpdateDashboardContext(
ctx context.Context, dashboard *graylog.Dashboard,
) (*ErrorInfo, error) {
if dashboard == nil {
return nil, fmt.Errorf("dashboard is nil")
}
id := dashboard.ID
if id == "" {
return nil, fmt.Errorf("id is empty")
}
u, err := client.Endpoints().Dashboard(id)
if err != nil {
return nil, err
}
d := &graylog.Dashboard{
Title: dashboard.Title,
Description: dashboard.Description,
}
return client.callPut(ctx, u.String(), d, nil)
}