Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
feat: support create and delete and get dashboard widget API
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Nov 3, 2018
1 parent 8df27ed commit 38670a6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
83 changes: 83 additions & 0 deletions client/dashboard_widget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package client

import (
"context"
"fmt"

"github.com/suzuki-shunsuke/go-graylog"
)

// CreateDashboardWidget creates a new dashboard widget.
func (client *Client) CreateDashboardWidget(dashboardID string, widget graylog.Widget) (graylog.Widget, *ErrorInfo, error) {
return client.CreateDashboardWidgetContext(context.Background(), dashboardID, widget)
}

// CreateDashboardWidgetContext creates a new dashboard widget with a context.
func (client *Client) CreateDashboardWidgetContext(
ctx context.Context, dashboardID string, widget graylog.Widget,
) (graylog.Widget, *ErrorInfo, error) {
if dashboardID == "" {
return widget, nil, fmt.Errorf("dashboard id is required")
}

ret := map[string]string{}
u, err := client.Endpoints().DashboardWidgets(dashboardID)
if err != nil {
return widget, nil, err
}
ei, err := client.callPost(ctx, u.String(), widget, &ret)
if err != nil {
return widget, ei, err
}
if id, ok := ret["widget_id"]; ok {
widget.ID = id
return widget, ei, nil
}
return widget, ei, fmt.Errorf(`response doesn't have the field "widget_id"`)
}

// DeleteDashboardWidget deletes a given dashboard widget.
func (client *Client) DeleteDashboardWidget(dashboardID, widgetID string) (*ErrorInfo, error) {
return client.DeleteDashboardWidgetContext(context.Background(), dashboardID, widgetID)
}

// DeleteDashboardWidgetContext deletes a given dashboard widget with a context.
func (client *Client) DeleteDashboardWidgetContext(
ctx context.Context, dashboardID, widgetID string,
) (*ErrorInfo, error) {
if dashboardID == "" {
return nil, fmt.Errorf("dashboard id is required")
}
if widgetID == "" {
return nil, fmt.Errorf("widget id is required")
}
u, err := client.Endpoints().DashboardWidget(dashboardID, widgetID)
if err != nil {
return nil, err
}
return client.callDelete(ctx, u.String(), nil, nil)
}

// GetDashboardWidget gets a dashboard widget.
func (client *Client) GetDashboardWidget(dashboardID, widgetID string) (graylog.Widget, *ErrorInfo, error) {
return client.GetDashboardWidgetContext(context.Background(), dashboardID, widgetID)
}

// GetDashboardWidgetContext gets a dashboard widget with a context.
func (client *Client) GetDashboardWidgetContext(
ctx context.Context, dashboardID, widgetID string,
) (graylog.Widget, *ErrorInfo, error) {
widget := graylog.Widget{}
if dashboardID == "" {
return widget, nil, fmt.Errorf("dashboard id is required")
}
if widgetID == "" {
return widget, nil, fmt.Errorf("widget id is required")
}
u, err := client.Endpoints().DashboardWidget(dashboardID, widgetID)
if err != nil {
return widget, nil, err
}
ei, err := client.callGet(ctx, u.String(), nil, &widget)
return widget, ei, err
}
16 changes: 16 additions & 0 deletions client/endpoint/dashboard_widget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package endpoint

import (
"net/url"
"path"
)

// DashboardWidgets returns an Dashboard Widget API's endpoint url.
func (ep *Endpoints) DashboardWidgets(dashboardID string) (*url.URL, error) {
return urlJoin(ep.dashboards, path.Join(dashboardID, "widgets"))
}

// DashboardWidget returns an Dashboard Widget API's endpoint url.
func (ep *Endpoints) DashboardWidget(dashboardID, widgetID string) (*url.URL, error) {
return urlJoin(ep.dashboards, path.Join(dashboardID, "widgets", widgetID))
}
5 changes: 3 additions & 2 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ type Dashboard struct {
// Widget represents a Graylog's Dashboard Widget.
type Widget struct {
// ex. "STREAM_SEARCH_RESULT_COUNT"
Type string `json:"type,omitempty"`
Type string `json:"type,omitempty" v-create:"required"`
Description string `json:"description,omitempty" v-create:"required"`
CreatorUserID string `json:"creator_user_id,omitempty" v-create:"isdefault"`
ID string `json:"id,omitempty" v-create:"isdefault"`
CacheTime int `json:"cache_time,omitempty" v-create:"isdefault"`
Config *WidgetConfig `json:"config,omitempty"`
Config *WidgetConfig `json:"config,omitempty" v-create:"required"`
}

// WidgetConfig represents a Graylog's Dashboard Widget configuration.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "bash script/test.sh",
"golint": " go list ./... | xargs golint -set_exit_status",
"lint": "bash script/lint.sh",
"vet": "go vet ./...",
"metalint": "gometalinter ./...",
"fmt": "bash script/fmt.sh",
"coverage": "bash script/coverage.sh",
Expand Down

0 comments on commit 38670a6

Please sign in to comment.