/
external_initiators.go
90 lines (81 loc) · 2.44 KB
/
external_initiators.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
package web
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/pkg/errors"
)
// JobSpecNotice is sent to the External Initiator when JobSpecs are created.
type JobSpecNotice struct {
JobID *models.ID `json:"jobId"`
Type string `json:"type"`
Params models.JSON `json:"params,omitempty"`
}
// NewJobSpecNotice returns a new JobSpec.
func NewJobSpecNotice(initiator models.Initiator, js models.JobSpec) (*JobSpecNotice, error) {
if initiator.Body == nil {
return nil, errors.New("body must be defined")
}
return &JobSpecNotice{
JobID: js.ID,
Type: initiator.Type,
Params: *initiator.Body,
}, nil
}
func newNotifyHTTPRequest(jsn JobSpecNotice, ei models.ExternalInitiator) (*http.Request, error) {
buf, err := json.Marshal(jsn)
if err != nil {
return nil, errors.Wrap(err, "new Job Spec notification")
}
req, err := http.NewRequest(http.MethodPost, ei.URL.String(), bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(ExternalInitiatorAccessKeyHeader, ei.OutgoingToken)
req.Header.Set(ExternalInitiatorSecretHeader, ei.OutgoingSecret)
return req, nil
}
// NotifyExternalInitiator sends a POST notification to the External Initiator
// responsible for initiating the Job Spec.
func NotifyExternalInitiator(
js models.JobSpec,
store *store.Store,
) error {
initrs := js.InitiatorsFor(models.InitiatorExternal)
if len(initrs) > 1 {
return errors.New("must have one or less External Initiators")
}
if len(initrs) == 0 {
return nil
}
initr := initrs[0]
ei, err := store.FindExternalInitiatorByName(initr.Name)
if err != nil {
return errors.Wrap(err, "external initiator")
}
if ei.URL == nil {
return nil
}
notice, err := NewJobSpecNotice(initr, js)
if err != nil {
return errors.Wrap(err, "new Job Spec notification")
}
req, err := newNotifyHTTPRequest(*notice, ei)
if err != nil {
return errors.Wrap(err, "creating notify HTTP request")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "could not notify '%s' (%s)")
}
defer logger.ErrorIfCalling(resp.Body.Close)
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return fmt.Errorf(" notify '%s' (%s) received bad response '%s'", ei.Name, ei.URL, resp.Status)
}
return nil
}