forked from raystack/siren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
88 lines (70 loc) · 2.48 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pagerduty
import (
"context"
"fmt"
"github.com/goto/siren/core/notification"
"github.com/goto/siren/pkg/errors"
"github.com/goto/siren/pkg/httpclient"
"github.com/goto/siren/pkg/retry"
"github.com/goto/siren/plugins/receivers/base"
"github.com/mitchellh/mapstructure"
)
type PluginService struct {
base.UnimplementedService
client PagerDutyCaller
httpClient *httpclient.Client
retrier retry.Runner
}
func NewPluginService(cfg AppConfig, opts ...ServiceOption) *PluginService {
s := &PluginService{}
for _, opt := range opts {
opt(s)
}
if s.client == nil {
s.client = NewClient(cfg, ClientWithHTTPClient(s.httpClient), ClientWithRetrier(s.retrier))
}
return s
}
// TODO validation could be done by default and using validator library but we need to use generic for it
func (s *PluginService) PreHookDBTransformConfigs(ctx context.Context, configurations map[string]any) (map[string]any, error) {
receiverConfig := &ReceiverConfig{}
if err := mapstructure.Decode(configurations, receiverConfig); err != nil {
return nil, fmt.Errorf("failed to transform configurations to receiver config: %w", err)
}
if err := receiverConfig.Validate(); err != nil {
return nil, errors.ErrInvalid.WithMsgf(err.Error())
}
return configurations, nil
}
func (s *PluginService) PreHookQueueTransformConfigs(ctx context.Context, notificationConfigMap map[string]any) (map[string]any, error) {
notificationConfig := &NotificationConfig{}
if err := mapstructure.Decode(notificationConfigMap, notificationConfig); err != nil {
return nil, fmt.Errorf("failed to transform configurations to slack notification config: %w", err)
}
if err := notificationConfig.Validate(); err != nil {
return nil, err
}
return notificationConfig.AsMap(), nil
}
func (s *PluginService) Send(ctx context.Context, notificationMessage notification.Message) (bool, error) {
notificationConfig := &NotificationConfig{}
if err := mapstructure.Decode(notificationMessage.Configs, notificationConfig); err != nil {
return false, err
}
pgMessageV1 := &MessageV1{}
if err := mapstructure.Decode(notificationMessage.Details, &pgMessageV1); err != nil {
return false, err
}
pgMessageV1.ServiceKey = notificationConfig.ServiceKey
if err := s.client.NotifyV1(ctx, *pgMessageV1); err != nil {
if errors.As(err, new(retry.RetryableError)) {
return true, err
} else {
return false, err
}
}
return false, nil
}
func (s *PluginService) GetSystemDefaultTemplate() string {
return defaultAlertTemplateBodyV1
}