-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
validate.go
65 lines (55 loc) · 1.66 KB
/
validate.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
package webhook
import (
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
)
type TOMLWebhookSpecExternalInitiator struct {
Name string `toml:"name"`
Spec models.JSON `toml:"spec"`
}
type TOMLWebhookSpec struct {
ExternalInitiators []TOMLWebhookSpecExternalInitiator `toml:"externalInitiators"`
}
func ValidatedWebhookSpec(tomlString string, externalInitiatorManager ExternalInitiatorManager) (jb job.Job, err error) {
var tree *toml.Tree
tree, err = toml.Load(tomlString)
if err != nil {
return
}
err = tree.Unmarshal(&jb)
if err != nil {
return
}
if jb.Type != job.Webhook {
return jb, errors.Errorf("unsupported type %s", jb.Type)
}
var tomlSpec TOMLWebhookSpec
err = tree.Unmarshal(&tomlSpec)
if err != nil {
return jb, err
}
var externalInitiatorWebhookSpecs []job.ExternalInitiatorWebhookSpec
for _, eiSpec := range tomlSpec.ExternalInitiators {
ei, findErr := externalInitiatorManager.FindExternalInitiatorByName(eiSpec.Name)
if findErr != nil {
err = multierr.Combine(err, errors.Wrapf(findErr, "unable to find external initiator named %s", eiSpec.Name))
continue
}
eiWS := job.ExternalInitiatorWebhookSpec{
ExternalInitiatorID: ei.ID,
WebhookSpecID: 0, // It will be populated later, on save
Spec: eiSpec.Spec,
}
externalInitiatorWebhookSpecs = append(externalInitiatorWebhookSpecs, eiWS)
}
if err != nil {
return jb, err
}
jb.WebhookSpec = &job.WebhookSpec{
ExternalInitiatorWebhookSpecs: externalInitiatorWebhookSpecs,
}
return jb, nil
}