-
Notifications
You must be signed in to change notification settings - Fork 459
/
notifier.go
59 lines (48 loc) · 1.7 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
package fakenotifier
import (
"context"
"github.com/spiffe/spire/pkg/server/plugin/notifier"
"github.com/spiffe/spire/proto/spire/common/plugin"
)
type Config struct {
OnNotify func(*notifier.NotifyRequest) (*notifier.NotifyResponse, error)
OnNotifyAndAdvise func(*notifier.NotifyAndAdviseRequest) (*notifier.NotifyAndAdviseResponse, error)
}
type Notifier struct {
config Config
}
func New(config Config) *Notifier {
return &Notifier{
config: config,
}
}
func (n *Notifier) Notify(ctx context.Context, req *notifier.NotifyRequest) (*notifier.NotifyResponse, error) {
if n.config.OnNotify != nil {
return n.config.OnNotify(req)
}
return ¬ifier.NotifyResponse{}, nil
}
func (n *Notifier) NotifyAndAdvise(ctx context.Context, req *notifier.NotifyAndAdviseRequest) (*notifier.NotifyAndAdviseResponse, error) {
if n.config.OnNotifyAndAdvise != nil {
return n.config.OnNotifyAndAdvise(req)
}
return ¬ifier.NotifyAndAdviseResponse{}, nil
}
func (n *Notifier) Configure(ctx context.Context, req *plugin.ConfigureRequest) (*plugin.ConfigureResponse, error) {
return &plugin.ConfigureResponse{}, nil
}
func (n *Notifier) GetPluginInfo(ctx context.Context, req *plugin.GetPluginInfoRequest) (*plugin.GetPluginInfoResponse, error) {
return &plugin.GetPluginInfoResponse{}, nil
}
func NotifyWaiter() (*Notifier, <-chan *notifier.NotifyRequest) {
ch := make(chan *notifier.NotifyRequest)
return New(Config{
OnNotify: SendOnNotify(ch),
}), ch
}
func SendOnNotify(ch chan<- *notifier.NotifyRequest) func(req *notifier.NotifyRequest) (*notifier.NotifyResponse, error) {
return func(req *notifier.NotifyRequest) (*notifier.NotifyResponse, error) {
ch <- req
return ¬ifier.NotifyResponse{}, nil
}
}