From 97be796dfcbb9a62382a542689350ac38839d117 Mon Sep 17 00:00:00 2001 From: Bazaah Date: Fri, 9 Dec 2022 11:43:00 +0000 Subject: [PATCH] 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 ^^^^ --- schema/notify/ty/flags.go | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 schema/notify/ty/flags.go diff --git a/schema/notify/ty/flags.go b/schema/notify/ty/flags.go new file mode 100644 index 0000000..c1e2df1 --- /dev/null +++ b/schema/notify/ty/flags.go @@ -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()") +}