Merge pull request #14984 from influxdata/fix/slack-rule-details
fix(notification/rule): conditionally include url and token for slack rulepull/14991/head
commit
afc65465a7
|
@ -55,13 +55,13 @@ func TestValidEndpoint(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "empty slack url",
|
||||
name: "empty slack url and token",
|
||||
src: &endpoint.Slack{
|
||||
Base: goodBase,
|
||||
},
|
||||
err: &influxdb.Error{
|
||||
Code: influxdb.EInvalid,
|
||||
Msg: "slack endpoint URL is empty",
|
||||
Msg: "slack endpoint URL and token are empty",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -45,18 +45,20 @@ func (s Slack) Valid() error {
|
|||
if err := s.Base.valid(); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.URL == "" {
|
||||
if s.URL == "" && s.Token.Key == "" {
|
||||
return &influxdb.Error{
|
||||
Code: influxdb.EInvalid,
|
||||
Msg: "slack endpoint URL is empty",
|
||||
Msg: "slack endpoint URL and token are empty",
|
||||
}
|
||||
}
|
||||
if s.URL != "" {
|
||||
if _, err := url.Parse(s.URL); err != nil {
|
||||
return &influxdb.Error{
|
||||
Code: influxdb.EInvalid,
|
||||
Msg: fmt.Sprintf("slack endpoint URL is invalid: %s", err.Error()),
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO(desa): this requirement seems odd
|
||||
if s.Token.Key != "" && s.Token.Key != s.ID.String()+slackTokenSuffix {
|
||||
return &influxdb.Error{
|
||||
|
|
|
@ -43,7 +43,9 @@ func (s *Slack) GenerateFluxAST(e *endpoint.Slack) (*ast.Package, error) {
|
|||
func (s *Slack) generateFluxASTBody(e *endpoint.Slack) []ast.Statement {
|
||||
var statements []ast.Statement
|
||||
statements = append(statements, s.generateTaskOption())
|
||||
if e.Token.Key != "" {
|
||||
statements = append(statements, s.generateFluxASTSecrets(e))
|
||||
}
|
||||
statements = append(statements, s.generateFluxASTEndpoint(e))
|
||||
statements = append(statements, s.generateFluxASTNotificationDefinition(e))
|
||||
statements = append(statements, s.generateFluxASTStatuses())
|
||||
|
@ -60,12 +62,14 @@ func (s *Slack) generateFluxASTSecrets(e *endpoint.Slack) ast.Statement {
|
|||
}
|
||||
|
||||
func (s *Slack) generateFluxASTEndpoint(e *endpoint.Slack) ast.Statement {
|
||||
call := flux.Call(flux.Member("slack", "endpoint"),
|
||||
flux.Object(
|
||||
flux.Property("token", flux.Identifier("slack_secret")),
|
||||
flux.Property("url", flux.String(e.URL)),
|
||||
),
|
||||
)
|
||||
props := []*ast.Property{}
|
||||
if e.Token.Key != "" {
|
||||
props = append(props, flux.Property("token", flux.Identifier("slack_secret")))
|
||||
}
|
||||
if e.URL != "" {
|
||||
props = append(props, flux.Property("url", flux.String(e.URL)))
|
||||
}
|
||||
call := flux.Call(flux.Member("slack", "endpoint"), flux.Object(props...))
|
||||
|
||||
return flux.DefineVariable("slack_endpoint", call)
|
||||
}
|
||||
|
|
|
@ -24,7 +24,15 @@ func statusRulePtr(r notification.CheckLevel) *notification.CheckLevel {
|
|||
}
|
||||
|
||||
func TestSlack_GenerateFlux(t *testing.T) {
|
||||
want := `package main
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
rule *rule.Slack
|
||||
endpoint *endpoint.Slack
|
||||
}{
|
||||
{
|
||||
name: "with url",
|
||||
want: `package main
|
||||
// foo
|
||||
import "influxdata/influxdb/monitor"
|
||||
import "slack"
|
||||
|
@ -33,8 +41,7 @@ import "experimental"
|
|||
|
||||
option task = {name: "foo", every: 1h}
|
||||
|
||||
slack_secret = secrets.get(key: "slack_token")
|
||||
slack_endpoint = slack.endpoint(token: slack_secret, url: "http://localhost:7777")
|
||||
slack_endpoint = slack.endpoint(url: "http://localhost:7777")
|
||||
notification = {
|
||||
_notification_rule_id: "0000000000000001",
|
||||
_notification_rule_name: "foo",
|
||||
|
@ -55,9 +62,8 @@ all_statuses = union(tables: [crit, info_to_warn])
|
|||
|
||||
all_statuses
|
||||
|> monitor.notify(data: notification, endpoint: slack_endpoint(mapFn: (r) =>
|
||||
({channel: "bar", text: "blah", color: if r._level == "crit" then "danger" else if r._level == "warn" then "warning" else "good"})))`
|
||||
|
||||
s := &rule.Slack{
|
||||
({channel: "bar", text: "blah", color: if r._level == "crit" then "danger" else if r._level == "warn" then "warning" else "good"})))`,
|
||||
rule: &rule.Slack{
|
||||
Channel: "bar",
|
||||
MessageTemplate: "blah",
|
||||
Base: rule.Base{
|
||||
|
@ -91,8 +97,164 @@ all_statuses
|
|||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
e := &endpoint.Slack{
|
||||
},
|
||||
endpoint: &endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
ID: 2,
|
||||
Name: "foo",
|
||||
},
|
||||
URL: "http://localhost:7777",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with token",
|
||||
want: `package main
|
||||
// foo
|
||||
import "influxdata/influxdb/monitor"
|
||||
import "slack"
|
||||
import "influxdata/influxdb/secrets"
|
||||
import "experimental"
|
||||
|
||||
option task = {name: "foo", every: 1h}
|
||||
|
||||
slack_secret = secrets.get(key: "slack_token")
|
||||
slack_endpoint = slack.endpoint(token: slack_secret)
|
||||
notification = {
|
||||
_notification_rule_id: "0000000000000001",
|
||||
_notification_rule_name: "foo",
|
||||
_notification_endpoint_id: "0000000000000002",
|
||||
_notification_endpoint_name: "foo",
|
||||
}
|
||||
statuses = monitor.from(start: -2h, fn: (r) =>
|
||||
(r.foo == "bar" and r.baz == "bang"))
|
||||
crit = statuses
|
||||
|> filter(fn: (r) =>
|
||||
(r._level == "crit"))
|
||||
info_to_warn = statuses
|
||||
|> monitor.stateChanges(fromLevel: "info", toLevel: "warn")
|
||||
all_statuses = union(tables: [crit, info_to_warn])
|
||||
|> sort(columns: ["_time"])
|
||||
|> filter(fn: (r) =>
|
||||
(r._time > experimental.subDuration(from: now(), d: 1h)))
|
||||
|
||||
all_statuses
|
||||
|> monitor.notify(data: notification, endpoint: slack_endpoint(mapFn: (r) =>
|
||||
({channel: "bar", text: "blah", color: if r._level == "crit" then "danger" else if r._level == "warn" then "warning" else "good"})))`,
|
||||
rule: &rule.Slack{
|
||||
Channel: "bar",
|
||||
MessageTemplate: "blah",
|
||||
Base: rule.Base{
|
||||
ID: 1,
|
||||
EndpointID: 2,
|
||||
Name: "foo",
|
||||
Every: mustDuration("1h"),
|
||||
TagRules: []notification.TagRule{
|
||||
{
|
||||
Tag: influxdb.Tag{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
},
|
||||
Operator: notification.Equal,
|
||||
},
|
||||
{
|
||||
Tag: influxdb.Tag{
|
||||
Key: "baz",
|
||||
Value: "bang",
|
||||
},
|
||||
Operator: notification.Equal,
|
||||
},
|
||||
},
|
||||
StatusRules: []notification.StatusRule{
|
||||
{
|
||||
CurrentLevel: notification.Critical,
|
||||
},
|
||||
{
|
||||
CurrentLevel: notification.Warn,
|
||||
PreviousLevel: statusRulePtr(notification.Info),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoint: &endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
ID: 2,
|
||||
Name: "foo",
|
||||
},
|
||||
Token: influxdb.SecretField{
|
||||
Key: "slack_token",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with token and url",
|
||||
want: `package main
|
||||
// foo
|
||||
import "influxdata/influxdb/monitor"
|
||||
import "slack"
|
||||
import "influxdata/influxdb/secrets"
|
||||
import "experimental"
|
||||
|
||||
option task = {name: "foo", every: 1h}
|
||||
|
||||
slack_secret = secrets.get(key: "slack_token")
|
||||
slack_endpoint = slack.endpoint(token: slack_secret, url: "http://localhost:7777")
|
||||
notification = {
|
||||
_notification_rule_id: "0000000000000001",
|
||||
_notification_rule_name: "foo",
|
||||
_notification_endpoint_id: "0000000000000002",
|
||||
_notification_endpoint_name: "foo",
|
||||
}
|
||||
statuses = monitor.from(start: -2h, fn: (r) =>
|
||||
(r.foo == "bar" and r.baz == "bang"))
|
||||
crit = statuses
|
||||
|> filter(fn: (r) =>
|
||||
(r._level == "crit"))
|
||||
info_to_warn = statuses
|
||||
|> monitor.stateChanges(fromLevel: "info", toLevel: "warn")
|
||||
all_statuses = union(tables: [crit, info_to_warn])
|
||||
|> sort(columns: ["_time"])
|
||||
|> filter(fn: (r) =>
|
||||
(r._time > experimental.subDuration(from: now(), d: 1h)))
|
||||
|
||||
all_statuses
|
||||
|> monitor.notify(data: notification, endpoint: slack_endpoint(mapFn: (r) =>
|
||||
({channel: "bar", text: "blah", color: if r._level == "crit" then "danger" else if r._level == "warn" then "warning" else "good"})))`,
|
||||
rule: &rule.Slack{
|
||||
Channel: "bar",
|
||||
MessageTemplate: "blah",
|
||||
Base: rule.Base{
|
||||
ID: 1,
|
||||
EndpointID: 2,
|
||||
Name: "foo",
|
||||
Every: mustDuration("1h"),
|
||||
TagRules: []notification.TagRule{
|
||||
{
|
||||
Tag: influxdb.Tag{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
},
|
||||
Operator: notification.Equal,
|
||||
},
|
||||
{
|
||||
Tag: influxdb.Tag{
|
||||
Key: "baz",
|
||||
Value: "bang",
|
||||
},
|
||||
Operator: notification.Equal,
|
||||
},
|
||||
},
|
||||
StatusRules: []notification.StatusRule{
|
||||
{
|
||||
CurrentLevel: notification.Critical,
|
||||
},
|
||||
{
|
||||
CurrentLevel: notification.Warn,
|
||||
PreviousLevel: statusRulePtr(notification.Info),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
endpoint: &endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
ID: 2,
|
||||
Name: "foo",
|
||||
|
@ -101,14 +263,20 @@ all_statuses
|
|||
Token: influxdb.SecretField{
|
||||
Key: "slack_token",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
f, err := s.GenerateFlux(e)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f, err := tt.rule.GenerateFlux(tt.endpoint)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if f != want {
|
||||
t.Errorf("scripts did not match. want:\n%v\n\ngot:\n%v", want, f)
|
||||
if f != tt.want {
|
||||
t.Errorf("scripts did not match. want:\n%v\n\ngot:\n%v", tt.want, f)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue