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

[WIP] feat: support Views API #183

Open
wants to merge 5 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
18 changes: 18 additions & 0 deletions cmd/generate-testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ var (
"stream_alert_conditions": StreamAlertConditions{},
"outputs": Outputs{},
"stdout_output": Output{},
"views": Views{},
"view": View{},
}
)

Expand Down Expand Up @@ -199,6 +201,14 @@ type (
EventDefinition struct {
data graylog.EventDefinition
}

Views struct {
data graylog.Views
}

View struct {
data graylog.View
}
)

func (users Users) dump(input string) error {
Expand Down Expand Up @@ -292,3 +302,11 @@ func (definitions EventDefinitions) dump(input string) error {
func (definition EventDefinition) dump(input string) error {
return dump(input, &definition.data)
}

func (v Views) dump(input string) error {
return dump(input, &v.data)
}

func (v View) dump(input string) error {
return dump(input, &v.data)
}
12 changes: 12 additions & 0 deletions examples/v0.12/view.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "graylog_view" "test" {
title = "test"
description = "description"
summary = "summary"
# set appropriate search_id
# search_id = "5d9529b275d97f58f9539279"
# state = {
# "6971d00a-e605-43fb-b873-e4bca773d286" = {
# selected_fields = ["source", "message"]
# }
# }
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ require (
github.com/hashicorp/terraform v0.12.19
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/pkg/errors v0.8.1
github.com/sanity-io/litter v1.2.0
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
github.com/suzuki-shunsuke/flute v0.7.0
github.com/suzuki-shunsuke/go-jsoneq v0.1.2
github.com/suzuki-shunsuke/go-ptr v1.0.0
github.com/suzuki-shunsuke/go-set v6.0.0+incompatible // indirect
github.com/suzuki-shunsuke/go-set/v6 v6.0.1
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,16 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -219,7 +215,6 @@ github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lusis/go-artifactory v0.0.0-20160115162124-7e4ce345df82/go.mod h1:y54tfGmO3NKssKveTEFFzH8C/akrSOy/iW9qEAUDV84=
Expand Down Expand Up @@ -300,7 +295,6 @@ github.com/scylladb/go-set v1.0.2/go.mod h1:DkpGd78rljTxKAnTDPFqXSGxvETQnJyuSOQw
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
Expand All @@ -320,12 +314,13 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/suzuki-shunsuke/flute v0.7.0 h1:DvDSCMIMiLlRj4AQPMeJ1NfHE3lG5yfs2LU0Dnf1+oc=
github.com/suzuki-shunsuke/flute v0.7.0/go.mod h1:UZOMr3GyEuYSr7/zf0nHgaLP9ZhKDB+2pBeV1WFkohE=
github.com/suzuki-shunsuke/go-cliutil v0.0.0-20181211154308-176f852d9bca/go.mod h1:Vq3NkhgmA9DT/2UZ08x/3A34xxvzQ/vTMABnTWKoMbY=
github.com/suzuki-shunsuke/go-graylog v2.5.0+incompatible h1:6G5hjnyxyLjc1GGksED+wcbQSTNmVzVhkthhVll6HyY=
github.com/suzuki-shunsuke/go-jsoneq v0.1.1/go.mod h1:vbOEb6bPf8nD+QASKzxtQ/vfVGgAyfWHyItUaUTwJWk=
github.com/suzuki-shunsuke/go-jsoneq v0.1.2 h1:A4czEbmFqSELTbrEtXVo4dSgfz2e2Z0y6G3OpExUML8=
github.com/suzuki-shunsuke/go-jsoneq v0.1.2/go.mod h1:ETXAwfruZTqMMKDxc9CYoS34CNSsnzcdcVIAW3+RujI=
github.com/suzuki-shunsuke/go-ptr v1.0.0 h1:sVR6ICMJbdCyOpxzrflHErkO8KmItPFDu0E0PoQR0W8=
github.com/suzuki-shunsuke/go-ptr v1.0.0/go.mod h1:4WKv+CJynv3Veutqvmt7yPUqEsZp0vezqFxVRukjga0=
github.com/suzuki-shunsuke/go-set v6.0.0+incompatible h1:APxAhd/XeHGrrp3vR6a9Yiwgak+HlM+CVREvDAULM8w=
github.com/suzuki-shunsuke/go-set v6.0.0+incompatible/go.mod h1:CZob7MsjSLZlWDHWKeRvDX/Q2eWA6qqbx+FXdy2FgnM=
github.com/suzuki-shunsuke/go-set/v6 v6.0.1 h1:JxqxB+UYnkMqskPZhLrOFLas6SY/Z6yMNOm9kOvwSDY=
github.com/suzuki-shunsuke/go-set/v6 v6.0.1/go.mod h1:3dlTSl52oLMev8ZwgkNkGl3euc+Gm0ehBpfR4g+MdXY=
github.com/suzuki-shunsuke/gomic v0.5.6/go.mod h1:GEDQnxOB07p3mTZG/MiuclfyfcqnNqp0rt9AHgIzs7Q=
Expand Down Expand Up @@ -463,7 +458,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.31.0 h1:bmXmP2RSNtFES+bn4uYuHT7iJFJv7Vj+an+ZQdDaD1M=
gopkg.in/go-playground/validator.v9 v9.31.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
Expand Down
2 changes: 2 additions & 0 deletions graylog/client/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Endpoints struct {
ldapGroupRoleMapping string
connectStreamsToPipeline string
connectPipelinesToStream string
views string
apiVersion string
}

Expand Down Expand Up @@ -96,6 +97,7 @@ func newEndpoints(endpoint, version string) (*Endpoints, error) {
users: endpoint + "/users",
grokPatterns: endpoint + "/system/grok",
grokPatternsTest: endpoint + "/system/grok/test",
views: endpoint + "/views",
apiVersion: version,
}, nil
}
16 changes: 16 additions & 0 deletions graylog/client/endpoint/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package endpoint

// Views returns a View API's endpoint url.
func (ep *Endpoints) Views() string {
return ep.views
}

// View returns a View API's endpoint url.
func (ep *Endpoints) View(id string) string {
return ep.views + "/" + id
}

// ViewDefault returns a View API's endpoint url.
func (ep *Endpoints) ViewDefault(id string) string {
return ep.views + "/" + id + "/default"
}
73 changes: 73 additions & 0 deletions graylog/client/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package client

import (
"context"
"errors"

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

// GetViews returns all views.
func (client *Client) GetViews(
ctx context.Context,
) (*graylog.Views, *ErrorInfo, error) {
viewsBody := &graylog.Views{}
ei, err := client.callGet(
ctx, client.Endpoints().Views(), nil, viewsBody)
return viewsBody, ei, err
}

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

// CreateView creates a view.
func (client *Client) CreateView(
ctx context.Context, view *graylog.View,
) (*ErrorInfo, error) {
// required: title search_id state
// allowed: state, search_id, owner, summary, title, created_at, id, description, requires, properties, dashboard_state
if view == nil {
return nil, errors.New("view is nil")
}
if view.State == nil {
view.State = map[string]graylog.ViewState{}
}
return client.callPost(ctx, client.Endpoints().Views(), view, view)
}

// UpdateView updates a view.
func (client *Client) UpdateView(
ctx context.Context, view *graylog.View,
) (*ErrorInfo, error) {
if view == nil {
return nil, errors.New("view is nil")
}
if view.ID == "" {
return nil, errors.New("id is empty")
}
if view.State == nil {
view.State = map[string]graylog.ViewState{}
}
body := *view
body.ID = ""
return client.callPut(ctx, client.Endpoints().View(view.ID), &body, view)
}

// DeleteView deletes a view.
func (client *Client) DeleteView(
ctx context.Context, id string,
) (*ErrorInfo, error) {
if id == "" {
return nil, errors.New("id is empty")
}
return client.callDelete(ctx, client.Endpoints().View(id), nil, nil)
}
58 changes: 58 additions & 0 deletions graylog/client/view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package client

import (
"context"
"io/ioutil"
"net/http"
"testing"

"github.com/stretchr/testify/require"
"github.com/suzuki-shunsuke/flute/flute"

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

func TestClient_GetViews(t *testing.T) {
ctx := context.Background()

cl, err := NewClient("http://example.com/api", "admin", "admin")
require.Nil(t, err)

buf, err := ioutil.ReadFile("../testdata/views.json")
require.Nil(t, err)
bodyStr := string(buf)

cl.SetHTTPClient(&http.Client{
Transport: &flute.Transport{
T: t,
Services: []flute.Service{
{
Endpoint: "http://example.com",
Routes: []flute.Route{
{
Tester: &flute.Tester{
Method: "GET",
Path: "/api/views",
PartOfHeader: http.Header{
"Content-Type": []string{"application/json"},
"X-Requested-By": []string{"go-graylog"},
"Authorization": nil,
},
},
Response: &flute.Response{
Base: http.Response{
StatusCode: 200,
},
BodyString: bodyStr,
},
},
},
},
},
},
})

views, _, err := cl.GetViews(ctx)
require.Nil(t, err)
require.Equal(t, testdata.Views, views)
}
Loading