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

Commit

Permalink
Merge pull request #199 from suzuki-shunsuke/feat/support-event-defin…
Browse files Browse the repository at this point in the history
…ition

feat: support Event Definition
  • Loading branch information
suzuki-shunsuke authored Dec 7, 2019
2 parents fa47fff + b1ce2d4 commit 6ebdde5
Show file tree
Hide file tree
Showing 28 changed files with 1,902 additions and 10 deletions.
20 changes: 12 additions & 8 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ volumes:
temp: {}
steps:
- name: download go modules
image: &image_go golang:1.13.3
image: &image_go golang:1.13.5
commands:
- go mod download
volumes: &volumes
Expand All @@ -23,7 +23,7 @@ steps:
environment:
GOPATH: /go
- name: golangci-lint
image: golangci/golangci-lint:v1.21.0
image: golangci/golangci-lint:v1.21.0-alpine
commands:
- golangci-lint run
volumes: *volumes
Expand All @@ -41,17 +41,21 @@ steps:
GOPATH: /go
CODECOV_TOKEN:
from_secret: codecov_token
- name: remove changes
image: &image_git plugins/git
commands:
# Sometimes it is failed to release by goreleaser due to changes of go.sum
- git checkout -- .

- name: fetch tags
image: plugins/git
- name: fetch tags to release
image: *image_git
commands:
- git fetch --tags
- git checkout -- .
when:
event:
- tag
- name: release
image: &goreleaser goreleaser/goreleaser:v0.119.0
image: &goreleaser goreleaser/goreleaser:v0.123.3
commands:
- goreleaser release
environment:
Expand All @@ -63,8 +67,8 @@ steps:
event:
- tag

- name: tag dummy
image: plugins/git
- name: create a dummy tag to test releasing
image: *image_git
commands:
- git tag v0.1.0-alpha
when:
Expand Down
2 changes: 2 additions & 0 deletions client/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Endpoints struct {
collectorConfigurations string
dashboards string
enabledStreams string
eventDefinitions string
eventNotifications string
indexSets string
indexSetStats string
Expand Down Expand Up @@ -73,6 +74,7 @@ func newEndpoints(endpoint, version string) (*Endpoints, error) {
collectorConfigurations: endpoint + "/plugins/org.graylog.plugins.collector/configurations",
dashboards: endpoint + "/dashboards",
enabledStreams: endpoint + "/streams/enabled",
eventDefinitions: endpoint + "/events/definitions",
eventNotifications: endpoint + "/events/notifications",
indexSets: endpoint + "/system/indices/index_sets",
indexSetStats: endpoint + "/system/indices/index_sets/stats",
Expand Down
11 changes: 11 additions & 0 deletions client/endpoint/event_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package endpoint

// EventDefinitions returns a EventDefinition API's endpoint url.
func (ep *Endpoints) EventDefinitions() string {
return ep.eventDefinitions
}

// EventDefinition returns a EventDefinition API's endpoint url.
func (ep *Endpoints) EventDefinition(id string) string {
return ep.eventDefinitions + "/" + id
}
106 changes: 106 additions & 0 deletions client/event_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package client

import (
"context"
"errors"

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

// CreateEventDefinition creates a new event definition.
func (client *Client) CreateEventDefinition(
ctx context.Context, definition *graylog.EventDefinition,
) (*ErrorInfo, error) {
// required: title, description, priority, alert, config, key_spec, notification_settings

if definition == nil {
return nil, errors.New("event definition is nil")
}
data := map[string]interface{}{
"title": definition.Title,
"description": definition.Description,
"priority": definition.Priority,
"alert": definition.Alert,
"key_spec": []struct{}{},
"notification_settings": definition.NotificationSettings,
"config": definition.Config,
}
if len(definition.FieldSpec) != 0 {
data["field_spec"] = definition.FieldSpec
}
if len(definition.Storage) != 0 {
data["storage"] = definition.Storage
}
if len(definition.Notifications) != 0 {
data["notifications"] = definition.Notifications
}
return client.callPost(
ctx, client.Endpoints().EventDefinitions(),
data, definition)
}

// GetEventDefinitions returns all event definitions.
func (client *Client) GetEventDefinitions(ctx context.Context) (
*graylog.EventDefinitionsBody, *ErrorInfo, error,
) {
definitions := &graylog.EventDefinitionsBody{}
ei, err := client.callGet(ctx, client.Endpoints().EventDefinitions(), nil, definitions)
return definitions, ei, err
}

// GetEventDefinition returns a given event definition.
func (client *Client) GetEventDefinition(
ctx context.Context, id string,
) (*graylog.EventDefinition, *ErrorInfo, error) {
if id == "" {
return nil, nil, errors.New("id is empty")
}
definition := &graylog.EventDefinition{}
ei, err := client.callGet(ctx, client.Endpoints().EventDefinition(id), nil, definition)
return definition, ei, err
}

// UpdateEventDefinition updates a given event definition.
func (client *Client) UpdateEventDefinition(
ctx context.Context, definition *graylog.EventDefinition,
) (*ErrorInfo, error) {
// required title, description, priority, alert, config, key_spec notification_settings
if definition == nil {
return nil, errors.New("event definition is nil")
}
if definition.ID == "" {
return nil, errors.New("id is empty")
}
data := map[string]interface{}{
"id": definition.ID,
"title": definition.Title,
"description": definition.Description,
"priority": definition.Priority,
"alert": definition.Alert,
"key_spec": []struct{}{},
"notification_settings": definition.NotificationSettings,
"config": definition.Config,
}
if len(definition.FieldSpec) != 0 {
data["field_spec"] = definition.FieldSpec
}
if len(definition.Storage) != 0 {
data["storage"] = definition.Storage
}
if len(definition.Notifications) != 0 {
data["notifications"] = definition.Notifications
}
ei, err := client.callPut(
ctx, client.Endpoints().EventDefinition(definition.ID), data, definition)
return ei, err
}

// DeleteEventDefinition deletes a given event definition.
func (client *Client) DeleteEventDefinition(
ctx context.Context, id string,
) (*ErrorInfo, error) {
if id == "" {
return nil, errors.New("id is empty")
}
return client.callDelete(ctx, client.Endpoints().EventDefinition(id), nil, nil)
}
Loading

0 comments on commit 6ebdde5

Please sign in to comment.