Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: httpPost url templating #2388

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,20 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, d NodeDiagnostic) (a
}

for _, p := range n.HTTPPostHandlers {
if p.Endpoint == "" && p.URL == "" {
return nil, errors.New("Either URL or endpoint must be non-empty")
}
c := httppost.HandlerConfig{
URL: p.URL,
Endpoint: p.Endpoint,
Headers: p.Headers,
CaptureResponse: p.CaptureResponseFlag,
Timeout: p.Timeout,
}
h := et.tm.HTTPPostService.Handler(c, ctx...)
h, err := et.tm.HTTPPostService.Handler(c, ctx...)
if err != nil {
return nil, errors.Wrap(err, "failed to create HTTPPostService.Handler")
}
an.handlers = append(an.handlers, h)
}

Expand Down
15 changes: 11 additions & 4 deletions http_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func newHTTPPostNode(et *ExecutingTask, n *pipeline.HTTPPostNode, d NodeDiagnost

// Should only ever be 0 or 1 from validation of n
if len(n.URLs) == 1 {
e := httppost.NewEndpoint(n.URLs[0], nil, httppost.BasicAuth{}, nil, nil)
temp, err := httppost.GetTemplate(n.URLs[0], "")
if err != nil {
return nil, errors.Wrap(err, "error in url templating")
}

e := httppost.NewEndpoint(temp, nil, httppost.BasicAuth{}, nil, nil)
hn.endpoint = e
}

Expand Down Expand Up @@ -167,8 +172,11 @@ func (n *HTTPPostNode) postRow(row *models.Row) (*http.Response, error) {
body := new(bytes.Buffer)

var contentType string
var mr *mappedRow
if n.endpoint.RowTemplate() != nil || n.endpoint.URL() != nil {
mr = newMappedRow(row)
}
if n.endpoint.RowTemplate() != nil {
mr := newMappedRow(row)
err := n.endpoint.RowTemplate().Execute(body, mr)
if err != nil {
return nil, errors.Wrap(err, "failed to execute template")
Expand All @@ -182,8 +190,7 @@ func (n *HTTPPostNode) postRow(row *models.Row) (*http.Response, error) {
}
contentType = "application/json"
}

req, err := n.endpoint.NewHTTPRequest(body)
req, err := n.endpoint.NewHTTPRequest(body, mr)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal row data json")
}
Expand Down
Loading