api/v2: Add Matcher.IsEqual to support all types

Signed-off-by: Kiril Vladimirov <kiril@vladimiroff.org>
This commit is contained in:
Kiril Vladimirov 2021-01-29 23:37:13 +02:00
parent a7ca7b1d29
commit 2b6315f399
4 changed files with 41 additions and 4 deletions

View File

@ -723,12 +723,20 @@ func gettableSilenceFromProto(s *silencepb.Silence) (open_api_models.GettableSil
Name: &m.Name,
Value: &m.Pattern,
}
f := false
t := true
switch m.Type {
case silencepb.Matcher_EQUAL:
f := false
matcher.IsEqual = &t
matcher.IsRegex = &f
case silencepb.Matcher_NOT_EQUAL:
matcher.IsEqual = &f
matcher.IsRegex = &f
case silencepb.Matcher_REGEXP:
t := true
matcher.IsEqual = &t
matcher.IsRegex = &t
case silencepb.Matcher_NOT_REGEXP:
matcher.IsEqual = &f
matcher.IsRegex = &t
default:
return sil, fmt.Errorf(
@ -792,10 +800,25 @@ func postableSilenceToProto(s *open_api_models.PostableSilence) (*silencepb.Sile
matcher := &silencepb.Matcher{
Name: *m.Name,
Pattern: *m.Value,
Type: silencepb.Matcher_EQUAL,
}
if *m.IsRegex {
isEqual := true
if m.IsEqual != nil {
isEqual = *m.IsEqual
}
isRegex := false
if m.IsRegex != nil {
isRegex = *m.IsRegex
}
switch {
case isEqual && !isRegex:
matcher.Type = silencepb.Matcher_EQUAL
case !isEqual && !isRegex:
matcher.Type = silencepb.Matcher_NOT_EQUAL
case isEqual && isRegex:
matcher.Type = silencepb.Matcher_REGEXP
case !isEqual && isRegex:
matcher.Type = silencepb.Matcher_NOT_REGEXP
}
sil.Matchers = append(sil.Matchers, matcher)
}

View File

@ -31,6 +31,9 @@ import (
// swagger:model matcher
type Matcher struct {
// is equal
IsEqual *bool `json:"isEqual,omitempty"`
// is regex
// Required: true
IsRegex *bool `json:"isRegex"`

View File

@ -392,6 +392,9 @@ definitions:
type: string
isRegex:
type: boolean
isEqual:
type: boolean
default: true
required:
- name
- value

View File

@ -612,6 +612,10 @@ func init() {
"isRegex"
],
"properties": {
"isEqual": {
"type": "boolean",
"default": true
},
"isRegex": {
"type": "boolean"
},
@ -1421,6 +1425,10 @@ func init() {
"isRegex"
],
"properties": {
"isEqual": {
"type": "boolean",
"default": true
},
"isRegex": {
"type": "boolean"
},