-
Notifications
You must be signed in to change notification settings - Fork 23
/
status.go
293 lines (253 loc) · 7.81 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"time"
"github.com/juju/errors"
"github.com/juju/schema"
)
// HasStatus defines the common methods for setting and getting status
// entries for the various entities.
type HasStatus interface {
Status() Status
SetStatus(StatusArgs)
}
type HasOperatorStatus interface {
SetOperatorStatus(StatusArgs)
OperatorStatus() Status
}
// HasModificationStatus defines the comment methods for setting and getting
// status entries for the various entities that are modified by actions.
// The modification changes, are changes that can alter the machine instance
// and setting the status can then be surfaced to the operator using the status.
// This is different from agent-status or machine-status, where the statuses
// tend to imply how the machine health is during a provisioning cycle or hook
// integration.
// Statuses that are expected: Applied, Error.
type HasModificationStatus interface {
ModificationStatus() Status
// SetModificationStatus allows the changing of the modification status, of
// a type, which is meant to highlight the changing to a machine instance
// after it's been provisioned.
SetModificationStatus(StatusArgs)
}
// HasStatusHistory defines the common methods for setting and
// getting historical status entries for the various entities.
type HasStatusHistory interface {
StatusHistory() []Status
SetStatusHistory([]StatusArgs)
}
// Status represents an agent, application, or workload status.
type Status interface {
Value() string
Message() string
Data() map[string]interface{}
Updated() time.Time
NeverSet() bool
}
// StatusArgs is an argument struct used to set the agent, application, or
// workload status.
type StatusArgs struct {
Value string
Message string
Data map[string]interface{}
Updated time.Time
NeverSet bool
}
func newStatus(args StatusArgs) *status {
return &status{
Version: 2,
StatusPoint_: StatusPoint_{
Value_: args.Value,
Message_: args.Message,
Data_: args.Data,
Updated_: args.Updated.UTC(),
NeverSet_: args.NeverSet,
},
}
}
func newStatusHistory() StatusHistory_ {
return StatusHistory_{
Version: 2,
}
}
// StatusPoint_ implements Status, and represents the status
// of an entity at a point in time. Used in the serialization of
// both status and StatusHistory_.
type StatusPoint_ struct {
Value_ string `yaml:"value"`
Message_ string `yaml:"message,omitempty"`
Data_ map[string]interface{} `yaml:"data,omitempty"`
Updated_ time.Time `yaml:"updated"`
NeverSet_ bool `yaml:"neverset"`
}
type status struct {
Version int `yaml:"version"`
StatusPoint_ `yaml:"status"`
}
type StatusHistory_ struct {
Version int `yaml:"version"`
History []*StatusPoint_ `yaml:"history"`
}
// Value implements Status.
func (a *StatusPoint_) Value() string {
return a.Value_
}
// Message implements Status.
func (a *StatusPoint_) Message() string {
return a.Message_
}
// Data implements Status.
func (a *StatusPoint_) Data() map[string]interface{} {
return a.Data_
}
// Updated implements Status.
func (a *StatusPoint_) Updated() time.Time {
return a.Updated_
}
// NeverSet implements Status.
func (a *StatusPoint_) NeverSet() bool {
return a.NeverSet_
}
func importStatus(source map[string]interface{}) (*status, error) {
checker := versionedEmbeddedChecker("status")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotate(err, "status version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
getFields, ok := statusFieldsFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
source = valid["status"].(map[string]interface{})
point, err := importStatusAllVersions(schema.FieldMap(getFields()), version, source)
if err != nil {
return nil, errors.Trace(err)
}
return &status{
Version: 2,
StatusPoint_: point,
}, nil
}
func importStatusHistory(history *StatusHistory_, source map[string]interface{}) error {
checker := versionedChecker("history")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return errors.Annotate(err, "status version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
getFields, ok := statusFieldsFuncs[version]
if !ok {
return errors.NotValidf("version %d", version)
}
sourceList := valid["history"].([]interface{})
points, err := importStatusList(sourceList, getFields, version)
if err != nil {
return errors.Trace(err)
}
history.History = points
return nil
}
func importStatusList(sourceList []interface{}, getFields statusFieldsFunc, version int) ([]*StatusPoint_, error) {
result := make([]*StatusPoint_, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for status %d, %T", i, value)
}
point, err := importStatusAllVersions(schema.FieldMap(getFields()), version, source)
if err != nil {
return nil, errors.Annotatef(err, "status history %d", i)
}
result = append(result, &point)
}
return result, nil
}
func importModificationStatus(source interface{}) (*status, error) {
if sourceMap, ok := source.(map[string]interface{}); ok {
return importStatus(sourceMap)
}
return nil, nil
}
type statusFieldsFunc func() (schema.Fields, schema.Defaults)
var statusFieldsFuncs = map[int]statusFieldsFunc{
1: statusV1Fields,
2: statusV2Fields,
}
func statusV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"value": schema.String(),
"message": schema.String(),
"data": schema.StringMap(schema.Any()),
"updated": schema.Time(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"message": "",
"data": schema.Omit,
}
return fields, defaults
}
func statusV2Fields() (schema.Fields, schema.Defaults) {
fields, defaults := statusV1Fields()
fields["neverset"] = schema.Bool()
defaults["neverset"] = false
return fields, defaults
}
func importStatusAllVersions(checker schema.Checker, importVersion int, source map[string]interface{}) (StatusPoint_, error) {
coerced, err := checker.Coerce(source, nil)
if err != nil {
return StatusPoint_{}, errors.Annotatef(err, "status v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
var data map[string]interface{}
if sourceData, set := valid["data"]; set {
data = sourceData.(map[string]interface{})
}
statusPoint := StatusPoint_{
Value_: valid["value"].(string),
Message_: valid["message"].(string),
Data_: data,
Updated_: valid["updated"].(time.Time),
}
if importVersion >= 2 {
statusPoint.NeverSet_ = valid["neverset"].(bool)
}
return statusPoint, nil
}
// StatusHistory implements HasStatusHistory.
func (s *StatusHistory_) StatusHistory() []Status {
var result []Status
if count := len(s.History); count > 0 {
result = make([]Status, count)
for i, value := range s.History {
result[i] = value
}
}
return result
}
// SetStatusHistory implements HasStatusHistory.
func (s *StatusHistory_) SetStatusHistory(args []StatusArgs) {
points := make([]*StatusPoint_, len(args))
for i, arg := range args {
points[i] = &StatusPoint_{
Value_: arg.Value,
Message_: arg.Message,
Data_: arg.Data,
Updated_: arg.Updated.UTC(),
NeverSet_: arg.NeverSet,
}
}
s.History = points
}
func addStatusHistorySchema(fields schema.Fields) {
fields["status-history"] = schema.StringMap(schema.Any())
}
func (s *StatusHistory_) importStatusHistory(valid map[string]interface{}) error {
return importStatusHistory(s, valid["status-history"].(map[string]interface{}))
}