-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
104 lines (93 loc) · 2.18 KB
/
notifier.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package config
import (
"errors"
"os"
"github.com/awlsring/texit/internal/pkg/config"
)
type NotifierType string
const (
NotifierTypeMqtt NotifierType = "mqtt"
NotifierTypeSns NotifierType = "sns"
)
var (
ErrMissingNotifierBroker = errors.New("missing notifier broker")
ErrMissingNotifierTopic = errors.New("missing notifier topic")
ErrMissiningNotifierRegion = errors.New("missing notifier region")
)
const (
SnsAccessKeyEnvVar = "SNS_AWS_ACCESS_KEY_ID"
SnsSecretKeyEnvVar = "SNS_AWS_SECRET_ACCESS_KEY"
SnsRegionEnvVar = "SNS_AWS_REGION"
)
// Configuration for the notifier
type NotifierConfig struct {
// the notifier type
Type NotifierType `yaml:"type"`
// the notifier name
Name string `yaml:"name"`
// the topic to publish to
Topic string `yaml:"topic"`
// username for the notifier
Username string `yaml:"username"`
// password for the notifier
Password string `yaml:"password"`
// Access key for the notifier
AccessKey string `yaml:"accessKey"`
// Secret key for the notifier
SecretKey string `yaml:"secretKey"`
// the aws region for the notifier
Region string `yaml:"region"`
// The broker for the notifier
Broker string `yaml:"broker"`
}
func (c *NotifierConfig) Validate() error {
if c.Name == "" {
c.Name = string(c.Type)
}
switch c.Type {
case NotifierTypeMqtt:
return c.validateMqtt()
case NotifierTypeSns:
return c.validateSns()
default:
return errors.New("unknown notifier type")
}
}
func (c *NotifierConfig) validateSns() error {
if c.Topic == "" {
topic := os.Getenv("SNS_NOTIFIER_ARN")
if topic == "" {
return ErrMissingNotifierTopic
}
c.Topic = topic
}
if c.Region == "" {
val, err := config.RegionFromEnv(SnsRegionEnvVar)
if err != nil {
return err
}
c.Region = val
}
if c.AccessKey == "" {
val, err := config.AwsAccessKeyFromEnv(SnsAccessKeyEnvVar)
if err == nil {
c.AccessKey = val
}
}
if c.SecretKey == "" {
val, err := config.AwsAccessKeyFromEnv(SnsSecretKeyEnvVar)
if err == nil {
c.AccessKey = val
}
}
return nil
}
func (c *NotifierConfig) validateMqtt() error {
if c.Broker == "" {
return ErrMissingNotifierBroker
}
if c.Topic == "" {
return ErrMissingNotifierTopic
}
return nil
}