File size: 3,144 Bytes
1e92f2d |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
package nfydest
import (
"context"
"errors"
"fmt"
"github.com/target/goalert/gadb"
"github.com/target/goalert/validation"
)
var (
ErrUnknownType = validation.NewGenericError("unknown destination type")
ErrUnsupported = errors.New("unsupported operation")
ErrNotEnabled = validation.NewGenericError("destination type is not enabled")
)
type Registry struct {
providers map[string]Provider
ids []string
stubSender bool
}
func NewRegistry() *Registry {
return &Registry{
providers: make(map[string]Provider),
}
}
// StubNotifiers will cause all notifications senders to be stubbed out.
//
// This causes all notifications to be marked as delivered, but not actually sent.
func (r *Registry) StubNotifiers() {
r.stubSender = true
}
func (r *Registry) Provider(id string) Provider { return r.providers[id] }
func (r *Registry) TypeInfo(ctx context.Context, typeID string) (*TypeInfo, error) {
p := r.Provider(typeID)
if p == nil {
return nil, ErrUnknownType
}
return p.TypeInfo(ctx)
}
func (r *Registry) IsDynamicAction(ctx context.Context, typeID string) (bool, error) {
info, err := r.TypeInfo(ctx, typeID)
if err != nil {
return false, err
}
return info.IsDynamicAction(), nil
}
func (r *Registry) LookupTypeName(ctx context.Context, typeID string) (string, error) {
info, err := r.TypeInfo(ctx, typeID)
if err != nil {
return "", err
}
return info.Name, nil
}
func (r *Registry) RegisterProvider(ctx context.Context, p Provider) {
if r.Provider(p.ID()) != nil {
panic(fmt.Sprintf("provider with ID %s already registered", p.ID()))
}
id := p.ID()
r.providers[id] = p
r.ids = append(r.ids, id)
}
func (r *Registry) DisplayInfo(ctx context.Context, d gadb.DestV1) (*DisplayInfo, error) {
p := r.Provider(d.Type)
if p == nil {
return nil, ErrUnknownType
}
return p.DisplayInfo(ctx, d.Args)
}
func (r *Registry) ValidateField(ctx context.Context, typeID, fieldID, value string) error {
p := r.Provider(typeID)
if p == nil {
return ErrUnknownType
}
return p.ValidateField(ctx, fieldID, value)
}
func (r *Registry) Types(ctx context.Context) ([]TypeInfo, error) {
var out []TypeInfo
for _, id := range r.ids {
ti, err := r.providers[id].TypeInfo(ctx)
if err != nil {
return nil, fmt.Errorf("get type info for %s: %w", id, err)
}
ti.Type = id // ensure ID is set
out = append(out, *ti)
}
return out, nil
}
func (r *Registry) SearchField(ctx context.Context, typeID, fieldID string, options SearchOptions) (*SearchResult, error) {
p := r.Provider(typeID)
if p == nil {
return nil, ErrUnknownType
}
s, ok := p.(FieldSearcher)
if !ok {
return nil, fmt.Errorf("provider %s does not support field searching: %w", typeID, ErrUnsupported)
}
return s.SearchField(ctx, fieldID, options)
}
func (r *Registry) FieldLabel(ctx context.Context, typeID, fieldID, value string) (string, error) {
p := r.Provider(typeID)
if p == nil {
return "", ErrUnknownType
}
s, ok := p.(FieldSearcher)
if !ok {
return "", fmt.Errorf("provider %s does not support field searching: %w", typeID, ErrUnsupported)
}
return s.FieldLabel(ctx, fieldID, value)
}
|