Skip to content

Commit

Permalink
Channel default value by changing default logic
Browse files Browse the repository at this point in the history
In a nutshell, the newly introduced plugin.PopulateDefaults function
populates all fields of a Plugin-implementing struct with those fields
from Info.ConfigAttributes where ConfigOption.Default is set. Thus, a
single function call before parsing the user-submitted configuration
within the Plugin.SetConfig method sets default values.

As a result of the discussion between the Go and the Web team,
summarized in #205, Web does not store key-value pairs to be omitted.

Prior, an already JSON-encoded version of the ConfigOption slice was
present in plugin.Info. Thus, this data wasn't easily available anymore.
As the new code now needs to access this field, it was changed and a
custom sql driver.Valuer was implemented for a slice type.

While working on this, all ConfigOptions were put in the same order as
the struct fields.

Requires <Icinga/icinga-notifications-web#230>.

Closes #205.
  • Loading branch information
oxzi committed Jul 15, 2024
1 parent 860ea0c commit 53448cb
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 44 deletions.
49 changes: 25 additions & 24 deletions cmd/channels/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ func (ch *Email) Send(reversePath string, recipients []string, msg []byte) error
}

func (ch *Email) SetConfig(jsonStr json.RawMessage) error {
err := json.Unmarshal(jsonStr, ch)
err := plugin.PopulateDefaults(ch)
if err != nil {
return err
}

err = json.Unmarshal(jsonStr, ch)
if err != nil {
return fmt.Errorf("failed to load config: %s %w", jsonStr, err)
}
Expand All @@ -108,24 +113,7 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error {
}

func (ch *Email) GetInfo() *plugin.Info {
elements := []*plugin.ConfigOption{
{
Name: "sender_name",
Type: "string",
Label: map[string]string{
"en_US": "Sender Name",
"de_DE": "Absendername",
},
},
{
Name: "sender_mail",
Type: "string",
Label: map[string]string{
"en_US": "Sender Address",
"de_DE": "Absenderadresse",
},
Default: "icinga@example.com",
},
configAttrs := plugin.ConfigOptions{
{
Name: "host",
Type: "string",
Expand All @@ -146,6 +134,24 @@ func (ch *Email) GetInfo() *plugin.Info {
Min: types.Int{NullInt64: sql.NullInt64{Int64: 1, Valid: true}},
Max: types.Int{NullInt64: sql.NullInt64{Int64: 65535, Valid: true}},
},
{
Name: "sender_name",
Type: "string",
Label: map[string]string{
"en_US": "Sender Name",
"de_DE": "Absendername",
},
Default: "Icinga",
},
{
Name: "sender_mail",
Type: "string",
Label: map[string]string{
"en_US": "Sender Address",
"de_DE": "Absenderadresse",
},
Default: "icinga@example.com",
},
{
Name: "user",
Type: "string",
Expand Down Expand Up @@ -178,11 +184,6 @@ func (ch *Email) GetInfo() *plugin.Info {
},
}

configAttrs, err := json.Marshal(elements)
if err != nil {
panic(err)
}

return &plugin.Info{
Name: "Email",
Version: internal.Version.Version,
Expand Down
72 changes: 72 additions & 0 deletions cmd/channels/email/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)

func TestEmail_SetConfig(t *testing.T) {
tests := []struct {
name string
jsonMsg string
want *Email
wantErr bool
}{
{
name: "empty-string",
jsonMsg: ``,
wantErr: true,
},
{
name: "empty-json-obj-use-defaults",
jsonMsg: `{}`,
want: &Email{SenderName: "Icinga", SenderMail: "icinga@example.com"},
},
{
name: "sender-mail-null-equals-defaults",
jsonMsg: `{"sender_mail": null}`,
want: &Email{SenderName: "Icinga", SenderMail: "icinga@example.com"},
},
{
name: "sender-mail-overwrite",
jsonMsg: `{"sender_mail": "foo@bar"}`,
want: &Email{SenderName: "Icinga", SenderMail: "foo@bar"},
},
{
name: "sender-mail-overwrite-empty",
jsonMsg: `{"sender_mail": ""}`,
want: &Email{SenderName: "Icinga", SenderMail: ""},
},
{
name: "full-example-config",
jsonMsg: `{"sender_name":"icinga","sender_mail":"icinga@example.com","host":"smtp.example.com","port":"25","encryption":"none"}`,
want: &Email{
Host: "smtp.example.com",
Port: "25",
SenderName: "icinga",
SenderMail: "icinga@example.com",
User: "",
Password: "",
Encryption: "none",
},
},
{
name: "user-but-missing-pass",
jsonMsg: `{"user": "foo"}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
email := &Email{}
err := email.SetConfig(json.RawMessage(tt.jsonMsg))
assert.Equal(t, tt.wantErr, err != nil, "SetConfig() error = %v, wantErr = %t", err, tt.wantErr)
if err != nil {
return
}

assert.Equal(t, tt.want, email, "Email differs")
})
}
}
13 changes: 6 additions & 7 deletions cmd/channels/rocketchat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ func (ch *RocketChat) SendNotification(req *plugin.NotificationRequest) error {
}

func (ch *RocketChat) SetConfig(jsonStr json.RawMessage) error {
err := plugin.PopulateDefaults(ch)
if err != nil {
return err
}

return json.Unmarshal(jsonStr, ch)
}

func (ch *RocketChat) GetInfo() *plugin.Info {

elements := []*plugin.ConfigOption{
configAttrs := plugin.ConfigOptions{
{
Name: "url",
Type: "string",
Expand Down Expand Up @@ -112,11 +116,6 @@ func (ch *RocketChat) GetInfo() *plugin.Info {
},
}

configAttrs, err := json.Marshal(elements)
if err != nil {
panic(err)
}

return &plugin.Info{
Name: "Rocket.Chat",
Version: internal.Version.Version,
Expand Down
16 changes: 8 additions & 8 deletions cmd/channels/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Webhook struct {
}

func (ch *Webhook) GetInfo() *plugin.Info {
elements := []*plugin.ConfigOption{
configAttrs := plugin.ConfigOptions{
{
Name: "method",
Type: "string",
Expand Down Expand Up @@ -65,7 +65,7 @@ func (ch *Webhook) GetInfo() *plugin.Info {
"en_US": "Go template applied to the current plugin.NotificationRequest to create an request body.",
"de_DE": "Go-Template über das zu verarbeitende plugin.NotificationRequest zum Erzeugen der mitgesendeten Anfragedaten.",
},
Default: `{{json .}}`,
Default: "{{json .}}",
},
{
Name: "response_status_codes",
Expand All @@ -82,11 +82,6 @@ func (ch *Webhook) GetInfo() *plugin.Info {
},
}

configAttrs, err := json.Marshal(elements)
if err != nil {
panic(err)
}

return &plugin.Info{
Name: "Webhook",
Version: internal.Version.Version,
Expand All @@ -96,7 +91,12 @@ func (ch *Webhook) GetInfo() *plugin.Info {
}

func (ch *Webhook) SetConfig(jsonStr json.RawMessage) error {
err := json.Unmarshal(jsonStr, ch)
err := plugin.PopulateDefaults(ch)
if err != nil {
return err
}

err = json.Unmarshal(jsonStr, ch)
if err != nil {
return err
}
Expand Down
40 changes: 35 additions & 5 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugin

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -69,13 +70,23 @@ type ConfigOption struct {
Max types.Int `json:"max,omitempty"`
}

// ConfigOptions describes all ConfigOption entries.
//
// This type became necessary to implement the database.sql.driver.Valuer to marshal it into JSON.
type ConfigOptions []ConfigOption

// Value implements database.sql's driver.Valuer to represent all ConfigOptions as a JSON array.
func (c ConfigOptions) Value() (driver.Value, error) {
return json.Marshal(c)
}

// Info contains plugin information.
type Info struct {
Type string `db:"type" json:"-"`
Name string `db:"name" json:"name"`
Version string `db:"version" json:"version"`
Author string `db:"author" json:"author"`
ConfigAttributes json.RawMessage `db:"config_attrs" json:"config_attrs"` // ConfigOption(s) as json-encoded list
Type string `db:"type" json:"-"`
Name string `db:"name" json:"name"`
Version string `db:"version" json:"version"`
Author string `db:"author" json:"author"`
ConfigAttributes ConfigOptions `db:"config_attrs" json:"config_attrs"`
}

// TableName implements the contracts.TableNamer interface.
Expand Down Expand Up @@ -131,6 +142,25 @@ type Plugin interface {
SendNotification(req *NotificationRequest) error
}

// PopulateDefaults sets the struct fields from Info.ConfigAttributes where ConfigOption.Default is set.
//
// It should be called from each channel plugin within its Plugin.SetConfig before doing any further configuration.
func PopulateDefaults(typePtr Plugin) error {
defaults := make(map[string]any)
for _, confAttr := range typePtr.GetInfo().ConfigAttributes {
if confAttr.Default != nil {
defaults[confAttr.Name] = confAttr.Default
}
}

defaultConf, err := json.Marshal(defaults)
if err != nil {
return err
}

return json.Unmarshal(defaultConf, typePtr)
}

// RunPlugin reads the incoming stdin requests, processes and writes the responses to stdout
func RunPlugin(plugin Plugin) {
encoder := json.NewEncoder(os.Stdout)
Expand Down

0 comments on commit 53448cb

Please sign in to comment.