2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package role
|
|
|
|
|
2022-04-11 04:31:10 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
//go:generate stringer -type=Role
|
|
|
|
|
|
|
|
// Role is a peer's role.
|
|
|
|
type Role uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown Role = iota
|
2022-06-29 09:26:29 -04:00
|
|
|
ControlPlane
|
|
|
|
Worker
|
2022-04-13 06:39:55 -04:00
|
|
|
Admin
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
2022-04-11 04:31:10 -04: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 05:57:35 -04:00
|
|
|
*r = FromString(roleString)
|
2022-04-11 04:31:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-12 07:40:38 -04:00
|
|
|
|
2022-10-06 05:57:35 -04:00
|
|
|
// FromString returns the Role for the given string.
|
2022-10-12 07:40:38 -04:00
|
|
|
func FromString(s string) Role {
|
|
|
|
switch strings.ToLower(s) {
|
2022-10-06 05:57:35 -04:00
|
|
|
case "controlplane", "control-plane":
|
2022-10-12 07:40:38 -04:00
|
|
|
return ControlPlane
|
|
|
|
case "worker":
|
|
|
|
return Worker
|
|
|
|
case "admin":
|
|
|
|
return Admin
|
|
|
|
default:
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
}
|