2019-08-16 19:43:15 +00:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-08-27 17:29:49 +00:00
|
|
|
"fmt"
|
2019-08-16 19:43:15 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/flux/ast"
|
|
|
|
"github.com/influxdata/influxdb"
|
2019-08-27 17:29:49 +00:00
|
|
|
"github.com/influxdata/influxdb/notification/endpoint"
|
2019-08-16 19:43:15 +00:00
|
|
|
"github.com/influxdata/influxdb/notification/flux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HTTP is the notification rule config of http.
|
|
|
|
type HTTP struct {
|
|
|
|
Base
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateFlux generates a flux script for the http notification rule.
|
|
|
|
func (s *HTTP) GenerateFlux(e influxdb.NotificationEndpoint) (string, error) {
|
2019-08-27 17:29:49 +00:00
|
|
|
httpEndpoint, ok := e.(*endpoint.HTTP)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("endpoint provided is a %s, not an HTTP endpoint", e.Type())
|
|
|
|
}
|
|
|
|
p, err := s.GenerateFluxAST(httpEndpoint)
|
2019-08-16 19:43:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ast.Format(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateFluxAST generates a flux AST for the http notification rule.
|
2019-08-27 17:29:49 +00:00
|
|
|
func (s *HTTP) GenerateFluxAST(e *endpoint.HTTP) (*ast.Package, error) {
|
2019-08-16 19:43:15 +00:00
|
|
|
f := flux.File(
|
|
|
|
s.Name,
|
2019-09-05 13:52:05 +00:00
|
|
|
flux.Imports("influxdata/influxdb/monitor", "http", "json", "experimental"),
|
2019-08-27 17:29:49 +00:00
|
|
|
s.generateFluxASTBody(e),
|
2019-08-16 19:43:15 +00:00
|
|
|
)
|
|
|
|
return &ast.Package{Package: "main", Files: []*ast.File{f}}, nil
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
func (s *HTTP) generateFluxASTBody(e *endpoint.HTTP) []ast.Statement {
|
2019-08-16 19:43:15 +00:00
|
|
|
var statements []ast.Statement
|
|
|
|
statements = append(statements, s.generateTaskOption())
|
2019-09-05 22:32:28 +00:00
|
|
|
statements = append(statements, s.generateHeaders(e))
|
2019-08-27 17:29:49 +00:00
|
|
|
statements = append(statements, s.generateFluxASTEndpoint(e))
|
|
|
|
statements = append(statements, s.generateFluxASTNotificationDefinition(e))
|
2019-08-19 11:31:46 +00:00
|
|
|
statements = append(statements, s.generateFluxASTStatuses())
|
2019-09-03 20:58:50 +00:00
|
|
|
statements = append(statements, s.generateAllStateChanges()...)
|
2019-08-16 19:43:15 +00:00
|
|
|
statements = append(statements, s.generateFluxASTNotifyPipe())
|
|
|
|
|
|
|
|
return statements
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s *HTTP) generateHeaders(e *endpoint.HTTP) ast.Statement {
|
|
|
|
props := []*ast.Property{
|
|
|
|
flux.Dictionary(
|
|
|
|
"Content-Type", flux.String("application/json"),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.AuthMethod {
|
|
|
|
case "bearer":
|
|
|
|
token := flux.Call(
|
|
|
|
flux.Member("secrets", "get"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("key", flux.String(e.Token.Key)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
bearer := flux.Add(
|
|
|
|
flux.String("Bearer "),
|
|
|
|
token,
|
|
|
|
)
|
|
|
|
auth := flux.Dictionary("Authorization", bearer)
|
|
|
|
props = append(props, auth)
|
|
|
|
case "basic":
|
|
|
|
username := flux.Call(
|
|
|
|
flux.Member("secrets", "get"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("key", flux.String(e.Username.Key)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
passwd := flux.Call(
|
|
|
|
flux.Member("secrets", "get"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("key", flux.String(e.Password.Key)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
basic := flux.Call(
|
|
|
|
flux.Member("http", "basicAuth"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("u", username),
|
|
|
|
flux.Property("p", passwd),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
auth := flux.Dictionary("Authorization", basic)
|
|
|
|
props = append(props, auth)
|
|
|
|
}
|
|
|
|
return flux.DefineVariable("headers", flux.Object(props...))
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
func (s *HTTP) generateFluxASTEndpoint(e *endpoint.HTTP) ast.Statement {
|
2019-08-16 19:43:15 +00:00
|
|
|
// TODO(desa): where does <some key> come from
|
2019-08-27 17:29:49 +00:00
|
|
|
call := flux.Call(flux.Member("http", "endpoint"), flux.Object(flux.Property("url", flux.String(e.URL))))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-19 11:31:46 +00:00
|
|
|
return flux.DefineVariable("endpoint", call)
|
2019-08-16 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTP) generateFluxASTNotifyPipe() ast.Statement {
|
2019-09-05 22:32:28 +00:00
|
|
|
endpointBody := flux.Call(
|
|
|
|
flux.Member("json", "encode"),
|
|
|
|
flux.Object(flux.Property("v", flux.Identifier("r"))),
|
|
|
|
)
|
|
|
|
headers := flux.Property("headers", flux.Identifier("headers"))
|
|
|
|
|
|
|
|
endpointProps := []*ast.Property{
|
|
|
|
headers,
|
|
|
|
flux.Property("data", endpointBody),
|
|
|
|
}
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
endpointFn := flux.FuncBlock(flux.FunctionParams("r"),
|
|
|
|
s.generateBody(),
|
|
|
|
&ast.ReturnStatement{
|
|
|
|
Argument: flux.Object(endpointProps...),
|
|
|
|
},
|
|
|
|
)
|
2019-08-16 19:43:15 +00:00
|
|
|
|
|
|
|
props := []*ast.Property{}
|
2019-08-19 11:31:46 +00:00
|
|
|
props = append(props, flux.Property("data", flux.Identifier("notification")))
|
2019-08-16 19:43:15 +00:00
|
|
|
props = append(props, flux.Property("endpoint",
|
2019-08-19 11:31:46 +00:00
|
|
|
flux.Call(flux.Identifier("endpoint"), flux.Object(flux.Property("mapFn", endpointFn)))))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-23 21:46:03 +00:00
|
|
|
call := flux.Call(flux.Member("monitor", "notify"), flux.Object(props...))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
return flux.ExpressionStatement(flux.Pipe(flux.Identifier("all_statuses"), call))
|
2019-08-16 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
func (s *HTTP) generateBody() ast.Statement {
|
|
|
|
props := []*ast.Property{
|
|
|
|
flux.Dictionary(
|
|
|
|
"version", flux.Integer(1),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"rule_name", flux.Member("notification", "_notification_rule_name"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"rule_id", flux.Member("notification", "_notification_rule_id"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"endpoint_name", flux.Member("notification", "_notification_endpoint_name"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"endpoint_id", flux.Member("notification", "_notification_endpoint_id"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"check_name", flux.Member("r", "_check_name"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"check_id", flux.Member("r", "_check_id"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"check_type", flux.Member("r", "_type"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"source_measurement", flux.Member("r", "_source_measurement"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"source_timestamp", flux.Member("r", "_source_timestamp"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"level", flux.Member("r", "_level"),
|
|
|
|
),
|
|
|
|
flux.Dictionary(
|
|
|
|
"message", flux.Member("r", "_message"),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return flux.DefineVariable("body", flux.Object(props...))
|
|
|
|
}
|
|
|
|
|
2019-08-16 19:43:15 +00:00
|
|
|
type httpAlias HTTP
|
|
|
|
|
|
|
|
// MarshalJSON implement json.Marshaler interface.
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s HTTP) MarshalJSON() ([]byte, error) {
|
2019-08-16 19:43:15 +00:00
|
|
|
return json.Marshal(
|
|
|
|
struct {
|
|
|
|
httpAlias
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{
|
2019-09-05 22:32:28 +00:00
|
|
|
httpAlias: httpAlias(s),
|
|
|
|
Type: s.Type(),
|
2019-08-16 19:43:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns where the config is valid.
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s HTTP) Valid() error {
|
|
|
|
if err := s.Base.valid(); err != nil {
|
2019-08-16 19:43:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of the rule config.
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s HTTP) Type() string {
|
2019-08-16 19:43:15 +00:00
|
|
|
return "http"
|
|
|
|
}
|