Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Oct 18, 2023
1 parent 51a8064 commit 4e50f91
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 155 deletions.
75 changes: 74 additions & 1 deletion cmd/channel/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/icinga/icinga-notifications/pkg/plugin"
"net"
Expand Down Expand Up @@ -54,6 +55,10 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error {
return fmt.Errorf("failed to load config: %s %w", jsonStr, err)
}

if ch.From == "fail" {
return errors.New("dummy fail")
}

if ch.Host == "" {
ch.Host = "localhost"
}
Expand All @@ -80,7 +85,75 @@ func (ch *Email) SetConfig(jsonStr json.RawMessage) error {
}

func (ch *Email) GetInfo() *plugin.Info {
return &plugin.Info{Name: "Email"}
var elements []*plugin.FormElement
elements = append(elements,
&plugin.FormElement{
Name: "host",
Type: "text",
Options: map[string]string{
"label": "SMTP Host",
"placeholder": "localhost",
},
},
&plugin.FormElement{
Name: "port",
Type: "select",
Options: map[string]string{
"label": "SMTP Port",
"options": `{"25":25, "465":465, "587":587, "2525":2525}`,
},
},
&plugin.FormElement{
Name: "from",
Type: "text",
Options: map[string]string{
"auto-complete": "off",
"label": "From",
},
},
&plugin.FormElement{
Name: "password",
Type: "password",
Options: map[string]string{
"auto-complete": "off",
"label": "Password",
},
},
&plugin.FormElement{
Name: "tls",
Type: "checkbox",
Options: map[string]string{
"label": "TLS / SSL",
"class": "autosubmit",
"checkedValue": "1",
"uncheckedValue": "0",
"value": "1",
},
},
&plugin.FormElement{
Name: "tls_certcheck",
Type: "checkbox",
Options: map[string]string{
"label": "Certificate Check",
"class": "autosubmit",
"checkedValue": "1",
"uncheckedValue": "0",
"value": "0",
},
},
)

marshal, err := json.Marshal(elements)
if err != nil {
return nil
}

return &plugin.Info{
Name: "Email",
Version: "0.0",
AuthorName: "",
ConfigAttributes: marshal,
}
}

func (ch *Email) GetServer() string {
Expand Down
42 changes: 41 additions & 1 deletion cmd/channel/rocketchat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,45 @@ func (ch *RocketChat) SetConfig(jsonStr json.RawMessage) error {
}

func (ch *RocketChat) GetInfo() *plugin.Info {
return &plugin.Info{Name: "Rocket.Chat"}
var elements []*plugin.FormElement
elements = append(elements,
&plugin.FormElement{
Name: "url",
Type: "text",
Options: map[string]string{
"required": "true",
"label": "Rocket.Chat URL",
},
},
&plugin.FormElement{
Name: "user_id",
Type: "text",
Options: map[string]string{
"required": "true",
"auto-complete": "off",
"label": "User ID",
},
},
&plugin.FormElement{
Name: "token",
Type: "password",
Options: map[string]string{
"required": "true",
"auto-complete": "off",
"label": "Personal Access Token",
},
},
)

marshal, err := json.Marshal(elements)
if err != nil {
return nil
}

return &plugin.Info{
Name: "Rocket.Chat",
Version: "0.0",
AuthorName: "",
ConfigAttributes: marshal,
}
}
62 changes: 62 additions & 0 deletions cmd/icinga-notifications-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"flag"
"fmt"
"github.com/icinga/icinga-notifications/internal"
"github.com/icinga/icinga-notifications/internal/channel"
"github.com/icinga/icinga-notifications/internal/config"
"github.com/icinga/icinga-notifications/internal/daemonConfig"
"github.com/icinga/icinga-notifications/internal/listener"
"github.com/icinga/icinga-notifications/pkg/plugin"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/icingadb/pkg/utils"
"go.uber.org/zap"
Expand Down Expand Up @@ -76,6 +79,8 @@ func main() {
}
}

syncPlugins(conf.ChannelPluginDir, logs, db)

runtimeConfig := config.NewRuntimeConfig(db, logs)
if err := runtimeConfig.UpdateFromDatabase(context.TODO()); err != nil {
logger.Fatalw("failed to load config from database", zap.Error(err))
Expand All @@ -87,3 +92,60 @@ func main() {
panic(err)
}
}

func syncPlugins(channelPluginDir string, logs *logging.Logging, db *icingadb.DB) {
logger := logs.GetChildLogger("channel")
files, err := os.ReadDir(channelPluginDir)
if err != nil {
logger.Error(err)
}

var PluginInfos []*plugin.Info

for _, file := range files {
p := channel.Plugin{
Logger: logger.With(zap.String("name", file.Name())),
}
err := p.Start(file.Name())
if err != nil {
p.Logger.Error(err)
continue
}

info, err := p.GetInfo()
if err != nil {
p.Logger.Error(err)
p.Stop()
continue
}
p.Stop()

PluginInfos = append(PluginInfos, info)
}

if len(PluginInfos) == 0 {
logger.Info("No working plugin found")
return
}

ctx := context.Background()
tx, err := db.BeginTxx(ctx, nil)
defer func() { _ = tx.Rollback() }()
if err != nil {
logger.Error("failed to start channel plugin database transaction: ", err)
return
}

stmt, _ := db.BuildUpsertStmt(&plugin.Info{})
_, err = tx.NamedExecContext(ctx, stmt, PluginInfos)
if err != nil {
logger.Error("failed to upsert channel plugin: ", err)
return
}
if err = tx.Commit(); err != nil {
logger.Error("can't commit channel plugin transaction: ", err)
return
}

logger.Infof("Successfully upsert %d plugins", len(PluginInfos))
}
Loading

0 comments on commit 4e50f91

Please sign in to comment.