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

Commit

Permalink
feat: add graylog_view
Browse files Browse the repository at this point in the history
issue: #182
  • Loading branch information
suzuki-shunsuke committed Oct 3, 2019
1 parent eb3b200 commit 7d52fda
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 24 deletions.
15 changes: 6 additions & 9 deletions client/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@ func (client *Client) CreateView(
if view == nil {
return nil, errors.New("view is nil")
}
ret := map[string]string{}
ei, err := client.callPost(ctx, client.Endpoints().Views(), view, &ret)
if err != nil {
return ei, err
if view.State == nil {
view.State = map[string]graylog.ViewState{}
}
if id, ok := ret["view_id"]; ok {
view.ID = id
return ei, nil
}
return ei, errors.New(`response doesn't have the field "view_id"`)
return client.callPost(ctx, client.Endpoints().Views(), view, view)
}

// UpdateView updates a view.
Expand All @@ -60,6 +54,9 @@ func (client *Client) UpdateView(
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)
Expand Down
9 changes: 9 additions & 0 deletions cmd/generate-testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var (
"email_stream_alarm_callback": StreamAlarmCallback{},
"stream_alert_conditions": StreamAlertConditions{},
"views": Views{},
"view": View{},
}
)

Expand Down Expand Up @@ -166,6 +167,10 @@ type (
Views struct {
data graylog.Views
}

View struct {
data graylog.View
}
)

func (users Users) dump(input string) error {
Expand Down Expand Up @@ -239,3 +244,7 @@ func (ac StreamAlertConditions) dump(input string) error {
func (v Views) dump(input string) error {
return dump(input, &v.data)
}

func (v View) dump(input string) error {
return dump(input, &v.data)
}
7 changes: 7 additions & 0 deletions terraform/example/v0.12/view.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# resource "graylog_view" "test" {
# title = "test"
# description = "description"
# summary = "summary"
# # set appropriate search_id
# search_id = "5d9529b275d97f58f9539279"
# }
1 change: 1 addition & 0 deletions terraform/graylog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Provider() *schema.Provider {
"graylog_stream": resourceStream(),
"graylog_stream_rule": resourceStreamRule(),
"graylog_user": resourceUser(),
"graylog_view": resourceView(),
},
DataSourcesMap: map[string]*schema.Resource{
"graylog_index_set": dataSourceIndexSet(),
Expand Down
178 changes: 178 additions & 0 deletions terraform/graylog/resource_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package graylog

import (
"context"

"github.com/hashicorp/terraform/helper/schema"
"github.com/suzuki-shunsuke/go-graylog"
)

func resourceView() *schema.Resource {
return &schema.Resource{
Create: resourceViewCreate,
Read: resourceViewRead,
Update: resourceViewUpdate,
Delete: resourceViewDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
// Required
"title": {
Type: schema.TypeString,
Required: true,
},
"search_id": {
Type: schema.TypeString,
Required: true,
},

// Optional
"state": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"selected_fields": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
"summary": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"owner": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
// outputs
"created_at": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dashboard_state": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
},
},
}
}

func newView(d *schema.ResourceData) (*graylog.View, error) {
return &graylog.View{
Title: d.Get("title").(string),
Summary: d.Get("summary").(string),
Description: d.Get("description").(string),
SearchID: d.Get("search_id").(string),
Owner: d.Get("owner").(string),
CreatedAt: d.Get("created_at").(string),
ID: d.Id(),
}, nil
}

func resourceViewCreate(d *schema.ResourceData, m interface{}) error {
ctx := context.Background()
cl, err := newClient(m)
if err != nil {
return err
}
view, err := newView(d)
if err != nil {
return err
}

if _, err := cl.CreateView(ctx, view); err != nil {
return err
}
d.SetId(view.ID)
return nil
}

func setView(d *schema.ResourceData, view *graylog.View, m interface{}) error {
if err := setStrToRD(d, "title", view.Title); err != nil {
return err
}
if err := setStrToRD(d, "summary", view.Summary); err != nil {
return err
}
if err := setStrToRD(d, "description", view.Description); err != nil {
return err
}
if err := setStrToRD(d, "search_id", view.SearchID); err != nil {
return err
}
if err := setStrToRD(d, "owner", view.Owner); err != nil {
return err
}
if err := setStrToRD(d, "created_at", view.CreatedAt); err != nil {
return err
}

return nil
}

func resourceViewRead(d *schema.ResourceData, m interface{}) error {
ctx := context.Background()
cl, err := newClient(m)
if err != nil {
return err
}
view, ei, err := cl.GetView(ctx, d.Id())
if err != nil {
return handleGetResourceError(d, ei, err)
}
return setView(d, view, m.(*Config))
}

func resourceViewUpdate(d *schema.ResourceData, m interface{}) error {
ctx := context.Background()
cl, err := newClient(m)
if err != nil {
return err
}
view, err := newView(d)
if err != nil {
return err
}
if _, err := cl.UpdateView(ctx, view); err != nil {
return err
}
return nil
}

func resourceViewDelete(d *schema.ResourceData, m interface{}) error {
ctx := context.Background()
cl, err := newClient(m)
if err != nil {
return err
}
if _, err := cl.DeleteView(ctx, d.Id()); err != nil {
return err
}
return nil
}
16 changes: 16 additions & 0 deletions testdata/view.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "5d9620e06df4af000d8ecaae",
"title": "test",
"summary": "",
"description": "",
"search_id": "5d9529b275d97f58f9539275",
"properties": [],
"requires": {},
"state": {},
"dashboard_state": {
"widgets": {},
"positions": {}
},
"owner": "admin",
"created_at": "2019-10-03T16:25:04.188Z"
}
28 changes: 13 additions & 15 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ type (
Summary string `json:"summary"`
Description string `json:"description"`
SearchID string `json:"search_id"`
Owner string `json:"owner"`
CreatedAt string `json:"created_at,omitempty"`
// Properties []interface{} `json:"properties"`
// Requires map[string]interface{} `json:"requires"`
State map[string]ViewState `json:"state"`
DashboardState DashboardState `json:"dashboard_state"`
Owner string `json:"owner"`

// ex. "2018-02-20T11:37:19.371Z"
CreatedAt string `json:"created_at,omitempty"`
State map[string]ViewState `json:"state"`
}

ViewState struct {
SelectedFields []string `json:"selected_fields"`
SelectedFields []string `json:"selected_fields"`
Titles map[string]map[string]string `json:"titles"`
WidgetMapping map[string][]string `json:"widget_mapping"`
Widgets []ViewWidget `json:"widgets"`
Positions map[string]ViewWidgetPosition `json:"positions"`
// StatecMessageListID interface{} `json:"state_message_list_id"`
Titles map[string]map[string]string `json:"titles"`
Widgets []ViewWidget `json:"widgets"`
WidgetMapping map[string][]string `json:"widget_mapping"`
Positions map[string]ViewWidgetPosition `json:"positions"`
// Formatting interface{} `json:"formatting"`
}

Expand All @@ -46,14 +44,14 @@ type (
}

AggregationViewWidgetConfig struct {
RowPivots []ViewWidgetRowPivot `json:"row_pivots"`
// ColumnPivots []interface{} `json:"column_pivots"`
Series []ViewWidgetSeries `json:"series"`
RowPivots []ViewWidgetRowPivot `json:"row_pivots"`
Series []ViewWidgetSeries `json:"series"`
Visualization string `json:"visualization"`
Rollup bool `json:"rollup"`
// Sort []interface{} `json:"sort"`
Visualization string `json:"visualization"`
// ColumnPivots []interface{} `json:"column_pivots"`
// VisualizationConfig interface{} `json:"visualization_config"`
// FormattingSettings interface{} `json:"formatting_settings"`
Rollup bool `json:"rollup"`
}

MessagesViewWidgetConfig struct {
Expand Down

0 comments on commit 7d52fda

Please sign in to comment.