2019-07-23 22:35:19 +00:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tag is k/v pair.
|
|
|
|
type Tag struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2019-07-19 09:42:01 +00:00
|
|
|
// Valid returns an error if the tag is missing fields
|
|
|
|
func (t Tag) Valid() error {
|
|
|
|
if t.Key == "" || t.Value == "" {
|
|
|
|
return &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
Msg: "tag must contain a key and a value",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 22:35:19 +00:00
|
|
|
// TagRule is the struct of tag rule.
|
|
|
|
type TagRule struct {
|
|
|
|
Tag
|
|
|
|
Operator `json:"operator"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operator is an Enum value of
|
|
|
|
type Operator string
|
|
|
|
|
|
|
|
// operators
|
|
|
|
const (
|
|
|
|
Equal Operator = "equal"
|
|
|
|
NotEqual Operator = "notequal"
|
|
|
|
RegexEqual Operator = "equalregex"
|
|
|
|
NotRegexEqual Operator = "notequalregex"
|
|
|
|
)
|
|
|
|
|
|
|
|
var availableOperator = map[Operator]bool{
|
|
|
|
Equal: false,
|
|
|
|
NotEqual: false,
|
|
|
|
RegexEqual: false,
|
|
|
|
NotRegexEqual: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns error for invalid operators.
|
|
|
|
func (tr TagRule) Valid() error {
|
2019-07-19 09:42:01 +00:00
|
|
|
if err := tr.Tag.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 22:35:19 +00:00
|
|
|
if _, ok := availableOperator[tr.Operator]; !ok {
|
|
|
|
return &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
Msg: fmt.Sprintf(`Operator %q is invalid`, tr.Operator),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|