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

feat: support collector configuration #35

Merged
merged 10 commits into from
Sep 9, 2018
123 changes: 123 additions & 0 deletions client/collector_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package client

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/suzuki-shunsuke/go-set"

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

// CreateCollectorConfiguration creates a collector configuration.
func (client *Client) CreateCollectorConfiguration(cfg *graylog.CollectorConfiguration) (*ErrorInfo, error) {
return client.CreateCollectorConfigurationContext(context.Background(), cfg)
}

// CreateCollectorConfigurationContext creates a collector configuration with a context.
func (client *Client) CreateCollectorConfigurationContext(
ctx context.Context, cfg *graylog.CollectorConfiguration,
) (*ErrorInfo, error) {
// POST /plugins/org.graylog.plugins.collector/configurations Create new collector configuration
if cfg == nil {
return nil, fmt.Errorf("collector configuration is nil")
}
if cfg.Inputs == nil {
cfg.Inputs = []graylog.CollectorConfigurationInput{}
}
if cfg.Outputs == nil {
cfg.Outputs = []graylog.CollectorConfigurationOutput{}
}
if cfg.Snippets == nil {
cfg.Snippets = []graylog.CollectorConfigurationSnippet{}
}
if cfg.Tags == nil {
cfg.Tags = set.StrSet{}
}
return client.callPost(ctx, client.Endpoints().CollectorConfigurations(), cfg, cfg)
}

// GetCollectorConfigurations returns all collector configurations.
func (client *Client) GetCollectorConfigurations() ([]graylog.CollectorConfiguration, int, *ErrorInfo, error) {
return client.GetCollectorConfigurationsContext(context.Background())
}

// GetCollectorConfigurationsContext returns all collector configurations with a context.
func (client *Client) GetCollectorConfigurationsContext(ctx context.Context) ([]graylog.CollectorConfiguration, int, *ErrorInfo, error) {
cfgs := &graylog.CollectorConfigurationsBody{}
ei, err := client.callGet(
ctx, client.Endpoints().CollectorConfigurations(), nil, cfgs)
return cfgs.Configurations, cfgs.Total, ei, err
}

// GetCollectorConfiguration returns a collector configuration.
func (client *Client) GetCollectorConfiguration(id string) (*graylog.CollectorConfiguration, *ErrorInfo, error) {
return client.GetCollectorConfigurationContext(context.Background(), id)
}

// GetCollectorConfigurationContext returns a given user with a context.
func (client *Client) GetCollectorConfigurationContext(
ctx context.Context, id string,
) (*graylog.CollectorConfiguration, *ErrorInfo, error) {
// GET /api/plugins/org.graylog.plugins.collector/configurations/:id
if id == "" {
return nil, nil, errors.New("id is empty")
}
u, err := client.Endpoints().CollectorConfiguration(id)
if err != nil {
return nil, nil, err
}
cfg := &graylog.CollectorConfiguration{}
ei, err := client.callGet(ctx, u.String(), nil, cfg)
return cfg, ei, err
}

// RenameCollectorConfiguration renames a collector configuration.
func (client *Client) RenameCollectorConfiguration(id, name string) (*graylog.CollectorConfiguration, *ErrorInfo, error) {
return client.RenameCollectorConfigurationContext(context.Background(), id, name)
}

// RenameCollectorConfigurationContext renames a collector configuration with a context.
func (client *Client) RenameCollectorConfigurationContext(
ctx context.Context, id, name string,
) (*graylog.CollectorConfiguration, *ErrorInfo, error) {
if id == "" {
return nil, nil, fmt.Errorf("id is nil")
}
if name == "" {
return nil, nil, fmt.Errorf("name is nil")
}
input := graylog.CollectorConfiguration{
Name: name,
Inputs: []graylog.CollectorConfigurationInput{},
Outputs: []graylog.CollectorConfigurationOutput{},
Snippets: []graylog.CollectorConfigurationSnippet{},
}
u, err := client.Endpoints().CollectorConfigurationName(id)
if err != nil {
return nil, nil, err
}
cfg := graylog.CollectorConfiguration{Name: name}
ei, err := client.callPut(ctx, u.String(), &input, &cfg)
return &cfg, ei, err
}

// DeleteCollectorConfiguration deletes a collector configuration.
func (client *Client) DeleteCollectorConfiguration(id string) (*ErrorInfo, error) {
return client.DeleteCollectorConfigurationContext(context.Background(), id)
}

// DeleteCollectorConfigurationContext deletes a collector configuration with a context.
func (client *Client) DeleteCollectorConfigurationContext(
ctx context.Context, id string,
) (*ErrorInfo, error) {
if id == "" {
return nil, errors.New("id is empty")
}
u, err := client.Endpoints().CollectorConfiguration(id)
if err != nil {
return nil, err
}
return client.callDelete(ctx, u.String(), nil, nil)
}
91 changes: 91 additions & 0 deletions client/collector_configuration_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package client

import (
"context"
"fmt"

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

// CreateCollectorConfigurationInput creates a collector configuration input.
func (client *Client) CreateCollectorConfigurationInput(
id string, input *graylog.CollectorConfigurationInput,
) (*ErrorInfo, error) {
return client.CreateCollectorConfigurationInputContext(
context.Background(), id, input)
}

// CreateCollectorConfigurationInputContext creates a collector configuration input with a context.
func (client *Client) CreateCollectorConfigurationInputContext(
ctx context.Context, id string, input *graylog.CollectorConfigurationInput,
) (*ErrorInfo, error) {
// POST /plugins/org.graylog.plugins.collector/configurations/{id}/inputs Create a configuration input
if id == "" {
return nil, fmt.Errorf("id is required")
}
if input == nil {
return nil, fmt.Errorf("collector configuration is nil")
}
u, err := client.Endpoints().CollectorConfigurationInputs(id)
if err != nil {
return nil, err
}
// 202 no content
return client.callPost(ctx, u.String(), input, nil)
}

// DeleteCollectorConfigurationInput deletes a collector configuration input.
func (client *Client) DeleteCollectorConfigurationInput(id, inputID string) (*ErrorInfo, error) {
return client.DeleteCollectorConfigurationInputContext(
context.Background(), id, inputID)
}

// DeleteCollectorConfigurationInputContext deletes a collector configuration input with a context.
func (client *Client) DeleteCollectorConfigurationInputContext(
ctx context.Context, id, inputID string,
) (*ErrorInfo, error) {
// DELETE /plugins/org.graylog.plugins.collector/configurations/{id}/inputs/{inputId} Delete input form configuration
if id == "" {
return nil, fmt.Errorf("id is required")
}
if inputID == "" {
return nil, fmt.Errorf("input id is required")
}
u, err := client.Endpoints().CollectorConfigurationInput(id, inputID)
if err != nil {
return nil, err
}
return client.callDelete(
ctx, u.String(), nil, nil)
}

// UpdateCollectorConfigurationInput updates a collector configuration input.
func (client *Client) UpdateCollectorConfigurationInput(
id, inputID string, input *graylog.CollectorConfigurationInput,
) (*ErrorInfo, error) {
return client.UpdateCollectorConfigurationInputContext(
context.Background(), id, inputID, input)
}

// UpdateCollectorConfigurationInputContext updates a collector configuration input with a context.
func (client *Client) UpdateCollectorConfigurationInputContext(
ctx context.Context, id, inputID string,
input *graylog.CollectorConfigurationInput,
) (*ErrorInfo, error) {
// PUT /plugins/org.graylog.plugins.collector/configurations/{id}/inputs/{input_id} Update a configuration input
if id == "" {
return nil, fmt.Errorf("id is required")
}
if inputID == "" {
return nil, fmt.Errorf("input id is required")
}
if input == nil {
return nil, fmt.Errorf("input is nil")
}
u, err := client.Endpoints().CollectorConfigurationInput(id, inputID)
if err != nil {
return nil, err
}
return client.callPut(
ctx, u.String(), input, nil)
}
91 changes: 91 additions & 0 deletions client/collector_configuration_output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package client

import (
"context"
"fmt"

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

// CreateCollectorConfigurationOutput creates a collector configuration output.
func (client *Client) CreateCollectorConfigurationOutput(
id string, output *graylog.CollectorConfigurationOutput,
) (*ErrorInfo, error) {
return client.CreateCollectorConfigurationOutputContext(
context.Background(), id, output)
}

// CreateCollectorConfigurationOutputContext creates a collector configuration output with a context.
func (client *Client) CreateCollectorConfigurationOutputContext(
ctx context.Context, id string, output *graylog.CollectorConfigurationOutput,
) (*ErrorInfo, error) {
// POST /plugins/org.graylog.plugins.collector/configurations/{id}/outputs Create a configuration output
if id == "" {
return nil, fmt.Errorf("id is required")
}
if output == nil {
return nil, fmt.Errorf("collector configuration is nil")
}
u, err := client.Endpoints().CollectorConfigurationOutputs(id)
if err != nil {
return nil, err
}
// 202 no content
return client.callPost(ctx, u.String(), output, nil)
}

// DeleteCollectorConfigurationOutput deletes a collector configuration output.
func (client *Client) DeleteCollectorConfigurationOutput(id, outputID string) (*ErrorInfo, error) {
return client.DeleteCollectorConfigurationOutputContext(
context.Background(), id, outputID)
}

// DeleteCollectorConfigurationOutputContext deletes a collector configuration output with a context.
func (client *Client) DeleteCollectorConfigurationOutputContext(
ctx context.Context, id, outputID string,
) (*ErrorInfo, error) {
// DELETE /plugins/org.graylog.plugins.collector/configurations/{id}/outputs/{outputId} Delete output form configuration
if id == "" {
return nil, fmt.Errorf("id is required")
}
if outputID == "" {
return nil, fmt.Errorf("output id is required")
}
u, err := client.Endpoints().CollectorConfigurationOutput(id, outputID)
if err != nil {
return nil, err
}
return client.callDelete(
ctx, u.String(), nil, nil)
}

// UpdateCollectorConfigurationOutput updates a collector configuration output.
func (client *Client) UpdateCollectorConfigurationOutput(
id, outputID string, output *graylog.CollectorConfigurationOutput,
) (*ErrorInfo, error) {
return client.UpdateCollectorConfigurationOutputContext(
context.Background(), id, outputID, output)
}

// UpdateCollectorConfigurationOutputContext updates a collector configuration output with a context.
func (client *Client) UpdateCollectorConfigurationOutputContext(
ctx context.Context, id, outputID string,
output *graylog.CollectorConfigurationOutput,
) (*ErrorInfo, error) {
// PUT /plugins/org.graylog.plugins.collector/configurations/{id}/outputs/{output_id} Update a configuration output
if id == "" {
return nil, fmt.Errorf("id is required")
}
if outputID == "" {
return nil, fmt.Errorf("output id is required")
}
if output == nil {
return nil, fmt.Errorf("output is nil")
}
u, err := client.Endpoints().CollectorConfigurationOutput(id, outputID)
if err != nil {
return nil, err
}
return client.callPut(
ctx, u.String(), output, nil)
}
90 changes: 90 additions & 0 deletions client/collector_configuration_snippet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package client

import (
"context"
"fmt"

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

// CreateCollectorConfigurationSnippet creates a collector configuration snippet.
func (client *Client) CreateCollectorConfigurationSnippet(
id string, snippet *graylog.CollectorConfigurationSnippet,
) (*ErrorInfo, error) {
return client.CreateCollectorConfigurationSnippetContext(
context.Background(), id, snippet)
}

// CreateCollectorConfigurationSnippetContext creates a collector configuration snippet with a context.
func (client *Client) CreateCollectorConfigurationSnippetContext(
ctx context.Context, id string, snippet *graylog.CollectorConfigurationSnippet,
) (*ErrorInfo, error) {
// POST /plugins/org.graylog.plugins.collector/configurations/{id}/snippets Create a configuration snippet
if id == "" {
return nil, fmt.Errorf("id is required")
}
if snippet == nil {
return nil, fmt.Errorf("collector configuration is nil")
}
u, err := client.Endpoints().CollectorConfigurationSnippets(id)
if err != nil {
return nil, err
}
return client.callPost(ctx, u.String(), snippet, nil)
}

// DeleteCollectorConfigurationSnippet deletes a collector configuration snippet.
func (client *Client) DeleteCollectorConfigurationSnippet(id, snippetID string) (*ErrorInfo, error) {
return client.DeleteCollectorConfigurationSnippetContext(
context.Background(), id, snippetID)
}

// DeleteCollectorConfigurationSnippetContext deletes a collector configuration snippet with a context.
func (client *Client) DeleteCollectorConfigurationSnippetContext(
ctx context.Context, id, snippetID string,
) (*ErrorInfo, error) {
// DELETE /plugins/org.graylog.plugins.collector/configurations/{id}/snippets/{snippetId} Delete snippet form configuration
if id == "" {
return nil, fmt.Errorf("id is required")
}
if snippetID == "" {
return nil, fmt.Errorf("snippet id is required")
}
u, err := client.Endpoints().CollectorConfigurationSnippet(id, snippetID)
if err != nil {
return nil, err
}
return client.callDelete(
ctx, u.String(), nil, nil)
}

// UpdateCollectorConfigurationSnippet updates a collector configuration snippet.
func (client *Client) UpdateCollectorConfigurationSnippet(
id, snippetID string, snippet *graylog.CollectorConfigurationSnippet,
) (*ErrorInfo, error) {
return client.UpdateCollectorConfigurationSnippetContext(
context.Background(), id, snippetID, snippet)
}

// UpdateCollectorConfigurationSnippetContext updates a collector configuration snippet with a context.
func (client *Client) UpdateCollectorConfigurationSnippetContext(
ctx context.Context, id, snippetID string,
snippet *graylog.CollectorConfigurationSnippet,
) (*ErrorInfo, error) {
// PUT /plugins/org.graylog.plugins.collector/configurations/{id}/snippets/{snippet_id} Update a configuration snippet
if id == "" {
return nil, fmt.Errorf("id is required")
}
if snippetID == "" {
return nil, fmt.Errorf("snippet id is required")
}
if snippet == nil {
return nil, fmt.Errorf("snippet is nil")
}
u, err := client.Endpoints().CollectorConfigurationSnippet(id, snippetID)
if err != nil {
return nil, err
}
return client.callPut(
ctx, u.String(), snippet, nil)
}
Loading