-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstatus.go
169 lines (132 loc) · 2.89 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package zebra
import (
"errors"
"strings"
)
type Status struct {
Fault Fault `json:"fault,omitempty"`
LeaseStatus LeaseStatus `json:"lease,omitempty"`
UsedBy string `json:"usedBy,omitempty"`
State State `json:"state,omitempty"`
}
type (
Fault uint8
LeaseStatus uint8
State uint8
)
const (
None Fault = iota
Minor
Major
Critical
)
const (
Free LeaseStatus = iota
Leased
Setup
)
const (
Inactive State = iota
Active
)
const Unknown = "unknown"
var (
ErrFault = errors.New(`fault is incorrect, must be in ["none", "minor", "major", "critical"]`)
ErrLeaseStatus = errors.New(`lease is incorrect, must be in ["leased", "free", "setup"]`)
ErrState = errors.New(`state is incorrect, must be in ["active", "inactive"]`)
ErrCreatedTime = errors.New(`createdTime is incorrect, must be before current time`)
)
func (f *Fault) String() string {
strs := map[Fault]string{None: "none", Minor: "minor", Major: "major", Critical: "critical"}
fstr, ok := strs[*f]
if !ok {
return Unknown
}
return fstr
}
func (f *Fault) MarshalText() ([]byte, error) {
return []byte(f.String()), nil
}
func (f *Fault) UnmarshalText(data []byte) error {
fmap := map[string]Fault{
"none": None,
"minor": Minor,
"major": Major,
"critical": Critical,
}
fval, ok := fmap[strings.ToLower(string(data))]
if !ok {
return ErrFault
}
*f = fval
return nil
}
func (l LeaseStatus) String() string {
strs := map[LeaseStatus]string{Leased: "leased", Free: "free", Setup: "setup"}
lstr, ok := strs[l]
if !ok {
return Unknown
}
return lstr
}
func (l *LeaseStatus) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
func (l *LeaseStatus) UnmarshalText(data []byte) error {
lmap := map[string]LeaseStatus{
"leased": Leased,
"free": Free,
"setup": Setup,
}
lval, ok := lmap[strings.ToLower(string(data))]
if !ok {
return ErrLeaseStatus
}
*l = lval
return nil
}
func (s State) String() string {
strs := map[State]string{Active: "active", Inactive: "inactive"}
sstr, ok := strs[s]
if !ok {
return Unknown
}
return sstr
}
func (s *State) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *State) UnmarshalText(data []byte) error {
smap := map[string]State{
"active": Active,
"inactive": Inactive,
}
sval, ok := smap[strings.ToLower(string(data))]
if !ok {
return ErrState
}
*s = sval
return nil
}
func (s Status) Validate() error {
if s.Fault > Critical {
return ErrFault
}
if s.LeaseStatus > Setup {
return ErrLeaseStatus
}
if s.State > Active {
return ErrState
}
return nil
}
// DefaultStatus returns a Status object with starting values (i.e. healthy
// resource in a free state, active, no user, and create time as right now).
func DefaultStatus() Status {
return Status{
Fault: None,
LeaseStatus: Free,
UsedBy: "",
State: Inactive,
}
}