generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
100 lines (83 loc) · 3.12 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
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"context"
"errors"
"fmt"
"time"
"github.com/xmidt-org/ancla/chrysom"
)
const errFmt = "%w: %v"
var (
errNonSuccessPushResult = errors.New("got a push result but was not of success type")
errFailedWebhookPush = errors.New("failed to add webhook to registry")
errFailedWebhookConversion = errors.New("failed to convert webhook to argus item")
errFailedItemConversion = errors.New("failed to convert argus item to webhook")
errFailedWebhooksFetch = errors.New("failed to fetch webhooks")
)
// Service describes the core operations around webhook subscriptions.
type Service interface {
// Add adds the given owned webhook to the current list of webhooks. If the operation
// succeeds, a non-nil error is returned.
Add(ctx context.Context, owner string, iw InternalWebhook) error
// GetAll lists all the current registered webhooks.
GetAll(ctx context.Context) ([]InternalWebhook, error)
}
// Config contains information needed to initialize the Argus database client.
type Config struct {
// DisablePartnerIDs, if true, will allow webhooks to register without
// checking the validity of the partnerIDs in the request.
DisablePartnerIDs bool
// Validation provides options for validating the webhook's URL and TTL
// related fields. Some validation happens regardless of the configuration:
// URLs must be a valid URL structure, the Matcher.DeviceID values must
// compile into regular expressions, and the Events field must have at
// least one value and all values must compile into regular expressions.
Validation ValidatorConfig
}
type service struct {
// argus is the Argus database client.
argus chrysom.PushReader
now func() time.Time
}
// Add adds the given owned webhook to the current list of webhooks. If the operation
// succeeds, a non-nil error is returned.
func (s *service) Add(ctx context.Context, owner string, iw InternalWebhook) error {
item, err := InternalWebhookToItem(s.now, iw)
if err != nil {
return fmt.Errorf(errFmt, errFailedWebhookConversion, err)
}
result, err := s.argus.PushItem(ctx, owner, item)
if err != nil {
return fmt.Errorf(errFmt, errFailedWebhookPush, err)
}
if result == chrysom.CreatedPushResult || result == chrysom.UpdatedPushResult {
return nil
}
return fmt.Errorf("%w: %s", errNonSuccessPushResult, result)
}
// GetAll returns all webhooks found on the configured webhooks partition
// of Argus.
func (s *service) GetAll(ctx context.Context) ([]InternalWebhook, error) {
items, err := s.argus.GetItems(ctx, "")
if err != nil {
return nil, fmt.Errorf(errFmt, errFailedWebhooksFetch, err)
}
iws := make([]InternalWebhook, len(items))
for i, item := range items {
webhook, err := ItemToInternalWebhook(item)
if err != nil {
return nil, fmt.Errorf(errFmt, errFailedItemConversion, err)
}
iws[i] = webhook
}
return iws, nil
}
// NewService returns an ancla client used to interact with an Argus database.
func NewService(client chrysom.PushReader) *service {
return &service{
argus: client,
now: time.Now,
}
}