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
|
|
|
|
}
|
|
|
|
switch strings.ToLower(roleString) {
|
2022-06-29 09:26:29 -04:00
|
|
|
case "controlplane":
|
|
|
|
*r = ControlPlane
|
|
|
|
case "worker":
|
|
|
|
*r = Worker
|
2022-04-13 06:39:55 -04:00
|
|
|
case "admin":
|
|
|
|
*r = Admin
|
2022-04-11 04:31:10 -04:00
|
|
|
default:
|
|
|
|
*r = Unknown
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|