opsgenie: Revise visible_to after code review

Signed-off-by: Carl Henrik Lunde <chlunde@ifi.uio.no>
This commit is contained in:
Carl Henrik Lunde 2024-09-10 21:39:34 +02:00
parent db0f43f6e9
commit ff034bc793
4 changed files with 38 additions and 16 deletions

View File

@ -592,9 +592,15 @@ type OpsGenieConfig struct {
UpdateAlerts bool `yaml:"update_alerts,omitempty" json:"update_alerts,omitempty"`
}
const opsgenieValidTypesRe = `^(team|teams|user|escalation|schedule)$`
const (
opsgenieValidResponderTypesRe = `^(team|teams|user|escalation|schedule)$`
opsgenieValidVisibleToTypesRe = `^(team|teams|user)$`
)
var opsgenieTypeMatcher = regexp.MustCompile(opsgenieValidTypesRe)
var (
opsgenieResponderTypeMatcher = regexp.MustCompile(opsgenieValidResponderTypesRe)
opsgenieVisibleToTypeMatcher = regexp.MustCompile(opsgenieValidVisibleToTypesRe)
)
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
@ -620,8 +626,8 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
}
} else {
r.Type = strings.ToLower(r.Type)
if !opsgenieTypeMatcher.MatchString(r.Type) {
return fmt.Errorf("opsGenieConfig responder %v type does not match valid options %s", r, opsgenieValidTypesRe)
if !opsgenieResponderTypeMatcher.MatchString(r.Type) {
return fmt.Errorf("opsGenieConfig responder %v type does not match valid options %s", r, opsgenieValidResponderTypesRe)
}
}
}
@ -638,8 +644,8 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
}
} else {
v.Type = strings.ToLower(v.Type)
if !opsgenieTypeMatcher.MatchString(v.Type) {
return fmt.Errorf("opsGenieConfig visible_to %v type does not match valid options %s", v, opsgenieValidTypesRe)
if !opsgenieVisibleToTypeMatcher.MatchString(v.Type) {
return fmt.Errorf("opsGenieConfig visible_to %v type does not match valid options %s", v, opsgenieValidVisibleToTypesRe)
}
}
}
@ -653,7 +659,7 @@ type OpsGenieConfigResponder struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
// team, user, escalation, schedule etc.
// team, user, escalation, schedule, teams etc.
Type string `yaml:"type,omitempty" json:"type,omitempty"`
}
@ -663,7 +669,7 @@ type OpsGenieConfigVisibleTo struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
// team, user
// team, user, teams
Type string `yaml:"type,omitempty" json:"type,omitempty"`
}

View File

@ -782,16 +782,31 @@ actions:
}
}
func TestOpsgenieTypeMatcher(t *testing.T) {
good := []string{"team", "user", "escalation", "schedule"}
func TestOpsgenieResponderTypeMatcher(t *testing.T) {
good := []string{"team", "user", "teams", "escalation", "schedule"}
for _, g := range good {
if !opsgenieTypeMatcher.MatchString(g) {
if !opsgenieResponderTypeMatcher.MatchString(g) {
t.Fatalf("failed to match with %s", g)
}
}
bad := []string{"0user", "team1", "2escalation3", "sche4dule", "User", "TEAM"}
for _, b := range bad {
if opsgenieTypeMatcher.MatchString(b) {
if opsgenieResponderTypeMatcher.MatchString(b) {
t.Errorf("mistakenly match with %s", b)
}
}
}
func TestOpsgenieVisibleToTypeMatcher(t *testing.T) {
good := []string{"team", "user", "teams"}
for _, g := range good {
if !opsgenieVisibleToTypeMatcher.MatchString(g) {
t.Fatalf("failed to match with %s", g)
}
}
bad := []string{"0user", "team1", "2escalation3", "sche4dule", "User", "TEAM", "escalation", "schedule"}
for _, b := range bad {
if opsgenieVisibleToTypeMatcher.MatchString(b) {
t.Errorf("mistakenly match with %s", b)
}
}

View File

@ -1121,14 +1121,15 @@ type: <tmpl_string>
#### `<visible_to>`
```yaml
# Exactly one of these fields should be defined.
# Exactly one of these fields must have a value after templating.
# If none of the fields, including type, are set, the array item will be ignored.
[ id: <tmpl_string> ]
[ name: <tmpl_string> ]
[ username: <tmpl_string> ]
# One of `team`, `teams` or `user`.
#
# The `teams` responder is configured using the `name` field above.
# The `teams` type is configured using the `name` field above.
# This field can contain a comma-separated list of team names.
# If the list is empty, no additional visibility will be configured.
type: <tmpl_string>

View File

@ -238,8 +238,8 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
teams := safeSplit(visibleTo.Name, ",")
for _, team := range teams {
newVisibleTo := opsGenieCreateMessageVisibleTo{
Name: tmpl(team),
Type: tmpl("team"),
Name: team,
Type: "team",
}
visibleTos = append(visibleTos, newVisibleTo)
}