-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Channel default value by changing default logic
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
1 parent
37fb724
commit 3989f71
Showing
5 changed files
with
146 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
@@ -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: "[email protected]", | ||
}, | ||
configAttrs := plugin.ConfigOptions{ | ||
{ | ||
Name: "host", | ||
Type: "string", | ||
|
@@ -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: "[email protected]", | ||
}, | ||
{ | ||
Name: "user", | ||
Type: "string", | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]"}, | ||
}, | ||
{ | ||
name: "sender-mail-null-equals-defaults", | ||
jsonMsg: `{"sender_mail": null}`, | ||
want: &Email{SenderName: "Icinga", SenderMail: "[email protected]"}, | ||
}, | ||
{ | ||
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":"[email protected]","host":"smtp.example.com","port":"25","encryption":"none"}`, | ||
want: &Email{ | ||
Host: "smtp.example.com", | ||
Port: "25", | ||
SenderName: "icinga", | ||
SenderMail: "[email protected]", | ||
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") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters