schema/notify: state.State

Representation of the STATE component in a keepalived notify event.

Can be one of: MASTER,BACKUP,STOP (for VRRP events)

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

View File

@ -0,0 +1,87 @@
/*
* 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 state
import "strings"
const (
// Zero value
NULL State = 0
// VRRP instance is master and owns the VIP
MASTER = 1 << iota
// VRRP instance is now a backup, and does not own the VIP
BACKUP
// VRRP instance has exited
STOP
// Unknown state was encountered, aliases NULL
UNKNOWN State = NULL
)
var (
s2s map[string]State = map[string]State{
"MASTER": MASTER,
"BACKUP": BACKUP,
"STOP": STOP,
}
)
func ParseState(i string) State {
i = strings.TrimSpace(i)
s, ok := s2s[i]
if !ok {
return UNKNOWN
}
return s
}
type State uint8
func (t State) Has(flag State) bool {
return t&flag != 0
}
func (s State) Is(other State) bool {
return s == other
}
func (s State) IsMaster() bool {
return s.Is(MASTER)
}
func (s State) IsBackup() bool {
return s.Is(BACKUP)
}
func (s State) IsStop() bool {
return s.Is(STOP)
}
func (s State) IsUnknown() bool {
return s.Is(UNKNOWN)
}
func (s State) String() string {
var o string
switch s {
case UNKNOWN:
o = "UNKNOWN"
case MASTER:
o = "MASTER"
case BACKUP:
o = "BACKUP"
case STOP:
o = "STOP"
default:
panic("missing case in notify/ty/flags.go:String()")
}
return o
}