Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint in config, dispatcher #461

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -43,6 +42,7 @@ type Config struct {
PluginInfos PluginInfo `yaml:"plugins_infos"`
}

// VatzProtocolInfo is VATZ information.
type VatzProtocolInfo struct {
ProtocolIdentifier string `yaml:"protocol_identifier"`
Port int `yaml:"port"`
Expand All @@ -53,6 +53,7 @@ type VatzProtocolInfo struct {
HomePath string `yaml:"home_path"`
}

// AbsoluteHomePath is the default home path
func (i VatzProtocolInfo) AbsoluteHomePath() (string, error) {
if strings.HasPrefix(i.HomePath, "~") {
homePath := os.Getenv("HOME")
Expand Down Expand Up @@ -121,7 +122,6 @@ type Plugin struct {
}

type parser struct {
rawConfig map[string]interface{}
}

func (p *parser) loadConfigFile(path string) ([]byte, error) {
Expand All @@ -140,8 +140,14 @@ func (p *parser) loadConfigFile(path string) ([]byte, error) {
}

rawYAML, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
} else {
rawYAML, err = ioutil.ReadFile(path)
rawYAML, err = os.ReadFile(path)
if err != nil {
return nil, err
}
}

if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions manager/dispatcher/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/rs/zerolog/log"
)

// DiscordColor is type for discord message color.
type DiscordColor int

const (
Expand All @@ -39,7 +40,11 @@ func (d *discord) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyIn
pUnique := deliverMessage.Options["pUnique"].(string)

if reqToNotify {
d.SendNotification(deliverMessage)
err := d.SendNotification(deliverMessage)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Discord): Send notification error: %s", err)
return err
}
}

if reminderState == tp.ON {
Expand All @@ -57,7 +62,10 @@ func (d *discord) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyIn
}
for _, schedule := range d.reminderSchedule {
id, _ := d.reminderCron.AddFunc(schedule, func() {
d.SendNotification(deliverMessage)
err := d.SendNotification(deliverMessage)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Discord): Send notification error: %s", err)
}
})
newEntries = append(newEntries, id)
}
Expand Down
6 changes: 3 additions & 3 deletions manager/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package dispatcher

import (
"errors"
"github.com/dsrvlabs/vatz/utils"
"strings"
"sync"
"time"

pb "github.com/dsrvlabs/vatz-proto/plugin/v1"
"github.com/dsrvlabs/vatz/manager/config"
tp "github.com/dsrvlabs/vatz/manager/types"
"github.com/dsrvlabs/vatz/utils"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -38,9 +38,10 @@ type Dispatcher interface {
SendNotification(request tp.ReqMsg) error
}

// GetDispatchers gets the registered alert channel.
func GetDispatchers(cfg config.NotificationInfo) []Dispatcher {
if len(cfg.DispatchChannels) == 0 {
dpError := errors.New("Error: No Dispatcher has set.")
dpError := errors.New("error: No Dispatcher has set")
log.Error().Str("module", "dispatcher").Msg("Please, Set at least a single channel for dispatcher, e.g.) Discord or Telegram")
panic(dpError)
}
Expand Down Expand Up @@ -80,7 +81,6 @@ func GetDispatchers(cfg config.NotificationInfo) []Dispatcher {
}
}
})

return dispatcherSingletons
}

Expand Down
2 changes: 1 addition & 1 deletion manager/dispatcher/mock_Dispatcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions manager/dispatcher/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package dispatcher
import (
"context"
"fmt"
"sync"
"time"

pd "github.com/PagerDuty/go-pagerduty"
pb "github.com/dsrvlabs/vatz-proto/plugin/v1"
tp "github.com/dsrvlabs/vatz/manager/types"
"github.com/rs/zerolog/log"
"sync"
"time"
)

// SUCCESS is string for delivering success.
const SUCCESS = "success"

type pagerdutyMSGEvent struct {
Expand All @@ -28,7 +30,11 @@ type pagerduty struct {
func (p *pagerduty) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyInfo tp.NotifyInfo) error {
reqToNotify, _, deliverMessage := messageHandler(firstRunMsg, preStat, notifyInfo)
if reqToNotify {
p.SendNotification(deliverMessage)
err := p.SendNotification(deliverMessage)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Pagerduty): Send notification error: %s", err)
return err
}
}
return nil
}
Expand Down
26 changes: 19 additions & 7 deletions manager/dispatcher/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"sync"

Expand All @@ -31,7 +31,12 @@ func (t *telegram) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyI
pUnique := deliverMessage.Options["pUnique"].(string)

if reqToNotify {
t.SendNotification(deliverMessage)
err := t.SendNotification(deliverMessage)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): Send notification error: %s", err)
return err
}

}

if reminderState == tp.ON {
Expand All @@ -49,7 +54,10 @@ func (t *telegram) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyI
}
for _, schedule := range t.reminderSchedule {
id, _ := t.reminderCron.AddFunc(schedule, func() {
t.SendNotification(deliverMessage)
err := t.SendNotification(deliverMessage)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): Send notification error: %s", err)
}
})
newEntries = append(newEntries, id)
}
Expand Down Expand Up @@ -83,7 +91,7 @@ func (t *telegram) SendNotification(msg tp.ReqMsg) error {
}
}

url := fmt.Sprintf("%s/sendMessage", getUrl(t.secret))
url := fmt.Sprintf("%s/sendMessage", getURL(t.secret))
sendingText := fmt.Sprintf(`
%s<strong>%s</strong>%s
<strong>(%s)</strong>
Expand All @@ -102,19 +110,23 @@ Plugin Name: <em>%s</em>
return err
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
body, err = io.ReadAll(response.Body)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): body parsing Error: %s", err)
return err
}
respJSON := make(map[string]interface{})
json.Unmarshal(body, &respJSON)
if !respJSON["ok"].(bool) {
log.Error().Str("module", "dispatcher").Msg("Channel(Telegram): Connection failed due to Invalid telegram token.")
}
err = json.Unmarshal(body, &respJSON)
if err != nil {
log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): Unmarshalling JSON Error: %s", err)
return err
}
return nil
}

func getUrl(token string) string {
func getURL(token string) string {
return fmt.Sprintf("https://api.telegram.org/bot%s", token)
}
17 changes: 15 additions & 2 deletions manager/types/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"github.com/robfig/cron/v3"
)

// DiscordColor is color for a discord alert.
type DiscordColor int

// Let's Setup this message into GRPC Type
// ReqMsg is Setup message into GRPC Type.
type ReqMsg struct {
FuncName string `json:"func_name"`
State pluginpb.STATE `json:"state"`
Expand All @@ -19,25 +20,30 @@ type ReqMsg struct {
Options map[string]interface{} `json:"options"`
}

// UpdateState is to uptade the state of pluginpb.
func (r *ReqMsg) UpdateState(stat pluginpb.STATE) {
r.State = stat
}

// UpdateSeverity is to uptade the severity of pluginpb.
func (r *ReqMsg) UpdateSeverity(sev pluginpb.SEVERITY) {
r.Severity = sev
}

// UpdateMSG is to update message
func (r *ReqMsg) UpdateMSG(message string) {
r.Msg = message
}

// DiscordMsg is type for sending messages to a discord.
type DiscordMsg struct {
Username string `json:"username,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Content string `json:"content,omitempty"`
Embeds []Embed `json:"embeds"`
}

// Embed is imformation for detail message.
gnongs marked this conversation as resolved.
Show resolved Hide resolved
type Embed struct {
Author struct {
Name string `json:"name,omitempty"`
Expand All @@ -62,20 +68,24 @@ type Embed struct {
} `json:"footer,omitempty"`
}

// StateFlag is type that indicates the status of the plugins.
type StateFlag struct {
State pluginpb.STATE `json:"state"`
Severity pluginpb.SEVERITY `json:"severity"`
}

// CronTabSt is crontab structure.
type CronTabSt struct {
Crontab *cron.Cron `json:"crontab"`
EntityID int `json:"entity_id"`
}

// Update is to update CronTabSt.
func (in *CronTabSt) Update(entity int) {
in.EntityID = entity
}

// Field is a structure for embeds that can be omitted.
type Field struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
Expand All @@ -93,17 +103,20 @@ type NotifyInfo struct {
ExecuteMsg string `json:"execute_msg"`
}

// Channel types for dispatchers
// Channel types for dispatchers.
type Channel string

// the type of channel.
const (
Discord Channel = "DISCORD"
Telegram Channel = "TELEGRAM"
PagerDuty Channel = "PAGERDUTY"
)

// Reminder is for reminnig alert
gnongs marked this conversation as resolved.
Show resolved Hide resolved
type Reminder string

// The type of Reminder.
const (
ON Reminder = "ON"
HANG Reminder = "HANG"
Expand Down