2022-09-05 07:06:08 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 15:03:15 +00:00
|
|
|
package role
|
|
|
|
|
2022-04-11 08:31:10 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-22 15:03:15 +00:00
|
|
|
//go:generate stringer -type=Role
|
|
|
|
|
|
|
|
// Role is a peer's role.
|
|
|
|
type Role uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown Role = iota
|
2022-06-29 13:26:29 +00:00
|
|
|
ControlPlane
|
|
|
|
Worker
|
2022-04-13 10:39:55 +00:00
|
|
|
Admin
|
2022-03-22 15:03:15 +00:00
|
|
|
)
|
2022-04-11 08:31:10 +00:00
|
|
|
|
|
|
|
// MarshalJSON marshals the Role to JSON string.
|
|
|
|
func (r Role) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the Role from JSON string.
|
|
|
|
func (r *Role) UnmarshalJSON(b []byte) error {
|
|
|
|
var roleString string
|
|
|
|
if err := json.Unmarshal(b, &roleString); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-06 09:57:35 +00:00
|
|
|
*r = FromString(roleString)
|
2022-04-11 08:31:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-12 11:40:38 +00:00
|
|
|
|
2022-10-06 09:57:35 +00:00
|
|
|
// FromString returns the Role for the given string.
|
2022-10-12 11:40:38 +00:00
|
|
|
func FromString(s string) Role {
|
|
|
|
switch strings.ToLower(s) {
|
2022-10-06 09:57:35 +00:00
|
|
|
case "controlplane", "control-plane":
|
2022-10-12 11:40:38 +00:00
|
|
|
return ControlPlane
|
|
|
|
case "worker":
|
|
|
|
return Worker
|
|
|
|
case "admin":
|
|
|
|
return Admin
|
|
|
|
default:
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
}
|