schema/notify: ty.Type

These correspond to the TYPE produced by keepalived's notify events.

It may be one of: INSTANCE,GROUP,VS,RS as of this commit.

Our focus is only on supporting VRRP event types at present, but we do
recognize all the current event types.

  TYPE INSTANCE STATE PRIORITY
  ^^^^
This commit is contained in:
Paul Stemmet 2022-12-09 11:43:00 +00:00
parent f0bacb8000
commit 97be796dfc
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
1 changed files with 84 additions and 0 deletions

84
schema/notify/ty/flags.go Normal file
View File

@ -0,0 +1,84 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ty
import "strings"
const (
// Zero value
NULL Type = 0
// VRRP Instance event
INSTANCE = 1 << iota
// VRRP Group event
GROUP
// IPVS Virtual Server event (unused)
VS
// IPVS Real Server event (unused)
RS
VRRP = INSTANCE | GROUP
IPVS = VS | RS
UNKNOWN = NULL
)
var (
s2ty map[string]Type = map[string]Type{
"INSTANCE": INSTANCE,
"GROUP": GROUP,
"VS": VS,
"RS": RS,
}
)
func ParseType(i string) Type {
i = strings.TrimSpace(i)
t, ok := s2ty[i]
if !ok {
return UNKNOWN
}
return t
}
type Type uint8
func (t Type) Has(flag Type) bool {
return t&flag != 0
}
func (t Type) IsVRRP() bool {
return t.Has(INSTANCE) || t.Has(GROUP)
}
func (t Type) IsIPVS() bool {
return t.Has(VS) || t.Has(RS)
}
func (t Type) String() string {
if t.IsVRRP() {
if t.Has(INSTANCE) {
return "INSTANCE"
}
if t.Has(GROUP) {
return "GROUP"
}
} else if t.IsIPVS() {
if t.Has(VS) {
return "VS"
}
if t.Has(RS) {
return "RS"
}
} else if t == UNKNOWN {
return "UNKNOWN"
}
panic("missing case in notify/state/flags.go:String()")
}