-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_systems.go
413 lines (361 loc) · 11.1 KB
/
api_systems.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package apstra
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
const (
apiUrlSystems = "/api/systems"
apiUrlSystemsPrefix = "/api/systems" + apiUrlPathDelim
apiUrlSystemsById = apiUrlSystemsPrefix + "%s"
systemCommsOn = "on"
systemCommsOff = "off"
ErrAgentNotConnect = iota
)
const ( // new block resets iota to 0
SystemAdminStateNone = SystemAdminState(iota) // default type 0
SystemAdminStateNormal
SystemAdminStateDecomm
SystemAdminStateMaint
SystemAdminStateUnknown = "unknown system admin state '%s'"
systemAdminStateNone = rawSystemAdminState("")
systemAdminStateNormal = rawSystemAdminState("normal")
systemAdminStateDecomm = rawSystemAdminState("decomm")
systemAdminStateMaint = rawSystemAdminState("maint")
systemAdminStateUnknown = "unknown system admin state '%d'"
)
type SystemAdminState int
func (o SystemAdminState) Int() int {
return int(o)
}
func (o SystemAdminState) String() string {
switch o {
case SystemAdminStateNone:
return string(systemAdminStateNone)
case SystemAdminStateNormal:
return string(systemAdminStateNormal)
case SystemAdminStateDecomm:
return string(systemAdminStateDecomm)
case SystemAdminStateMaint:
return string(systemAdminStateMaint)
default:
return fmt.Sprintf(systemAdminStateUnknown, o)
}
}
func (o SystemAdminState) raw() rawSystemAdminState {
return rawSystemAdminState(o.String())
}
type rawSystemAdminState string
func (o rawSystemAdminState) string() string {
return string(o)
}
func (o rawSystemAdminState) parse() (int, error) {
switch o {
case systemAdminStateNone:
return int(SystemAdminStateNone), nil
case systemAdminStateNormal:
return int(SystemAdminStateNormal), nil
case systemAdminStateDecomm:
return int(SystemAdminStateDecomm), nil
case systemAdminStateMaint:
return int(SystemAdminStateMaint), nil
default:
return 0, fmt.Errorf(SystemAdminStateUnknown, o)
}
}
type NodeDeployMode int
type nodeDeployMode string
const (
NodeDeployModeNone = NodeDeployMode(iota)
NodeDeployModeDeploy
NodeDeployModeUndeploy
NodeDeployModeReady
NodeDeployModeDrain
NodeDeployModeUnknown = "unknown node deploy mode '%s'"
nodeDeployModeNone = nodeDeployMode("")
nodeDeployModeDeploy = nodeDeployMode("deploy")
nodeDeployModeUndeploy = nodeDeployMode("undeploy")
nodeDeployModeReady = nodeDeployMode("ready")
nodeDeployModeDrain = nodeDeployMode("drain")
nodeDeployModeUnknown = "unknown node deploy mode '%d'"
)
func (o NodeDeployMode) Int() int {
return int(o)
}
func (o NodeDeployMode) String() string {
switch o {
case NodeDeployModeNone:
return string(nodeDeployModeNone)
case NodeDeployModeDeploy:
return string(nodeDeployModeDeploy)
case NodeDeployModeUndeploy:
return string(nodeDeployModeUndeploy)
case NodeDeployModeReady:
return string(nodeDeployModeReady)
case NodeDeployModeDrain:
return string(nodeDeployModeDrain)
default:
return fmt.Sprintf(nodeDeployModeUnknown, o)
}
}
func (o *NodeDeployMode) FromString(in string) error {
i, err := nodeDeployMode(in).parse()
if err != nil {
return err
}
*o = NodeDeployMode(i)
return nil
}
func (o nodeDeployMode) string() string {
return string(o)
}
func (o nodeDeployMode) parse() (int, error) {
switch o {
case nodeDeployModeNone:
return int(NodeDeployModeNone), nil
case nodeDeployModeDeploy:
return int(NodeDeployModeDeploy), nil
case nodeDeployModeUndeploy:
return int(NodeDeployModeUndeploy), nil
case nodeDeployModeReady:
return int(NodeDeployModeReady), nil
case nodeDeployModeDrain:
return int(NodeDeployModeDrain), nil
default:
return 0, fmt.Errorf(NodeDeployModeUnknown, o)
}
}
type SystemId string
type optionsSystemsResponse struct {
Items []SystemId `json:"items"`
Methods []string `json:"methods"`
}
type ManagedSystemInfo struct {
ContainerStatus SystemContainerStatus `json:"container_status"`
DeviceKey string `json:"device_key"`
Facts SystemFacts `json:"facts"`
Id SystemId `json:"id"`
Services []string `json:"services"`
Status SystemStatus `json:"status"`
UserConfig SystemUserConfig `json:"user_config"`
}
type rawManagedSystemInfo struct {
ContainerStatus SystemContainerStatus `json:"container_status"`
DeviceKey string `json:"device_key"`
Facts SystemFacts `json:"facts"`
Id SystemId `json:"id"`
Services []string `json:"services"`
Status rawSystemStatus `json:"status"`
UserConfig rawSystemUserConfig `json:"user_config"`
}
func (o *rawManagedSystemInfo) polish() (*ManagedSystemInfo, error) {
userConfig, err := o.UserConfig.polish()
if err != nil {
return nil, err
}
return &ManagedSystemInfo{
ContainerStatus: o.ContainerStatus,
DeviceKey: o.DeviceKey,
Facts: o.Facts,
Id: o.Id,
Services: o.Services,
Status: *o.Status.polish(),
UserConfig: *userConfig,
}, nil
}
type SystemContainerStatus struct {
Error string `json:"error"`
Host string `json:"host"`
Name string `json:"name"`
Status string `json:"status"`
}
type SystemFacts struct {
AosHclModel ObjectId `json:"aos_hcl_model"`
AosServer string `json:"aos_server"`
AosVersion string `json:"aos_version"`
ChassisMacRanges string `json:"chassis_mac_ranges"`
HwModel string `json:"hw_model"`
HwVersion string `json:"hw_version"`
MgmtIfname string `json:"mgmt_ifname"`
MgmtIpaddr string `json:"mgmt_ipaddr"`
MgmtMacaddr string `json:"mgmt_macaddr"`
OsArch string `json:"os_arch"`
OsFamily string `json:"os_family"`
OsVersion string `json:"os_version"`
OsVersionInfo struct {
Build string `json:"build"`
Major string `json:"major"`
Minor string `json:"minor"`
} `json:"os_version_info"`
SerialNumber string `json:"serial_number"`
Vendor string `json:"vendor"`
}
type SystemStatus struct {
AgentStartTime time.Time `json:"agent_start_time"`
CommState string `json:"comm_state"`
DeviceStartTime time.Time `json:"device_start_time"`
ErrorMessage string `json:"error_message"`
IsAcknowledged bool `json:"is_acknowledged"`
OperationMode AgentMode `json:"operation_mode"`
State string `json:"state"`
}
type rawSystemStatus struct {
AgentStartTime time.Time `json:"agent_start_time"`
CommState string `json:"comm_state"`
DeviceStartTime time.Time `json:"device_start_time"`
ErrorMessage string `json:"error_message"`
IsAcknowledged bool `json:"is_acknowledged"`
OperationMode rawAgentMode `json:"operation_mode"`
State string `json:"state"`
UserConfig SystemUserConfig `json:"user_config"`
}
func (o *rawSystemStatus) polish() *SystemStatus {
return &SystemStatus{
AgentStartTime: o.AgentStartTime,
CommState: o.CommState,
DeviceStartTime: o.DeviceStartTime,
ErrorMessage: o.ErrorMessage,
IsAcknowledged: o.IsAcknowledged,
OperationMode: AgentMode(o.OperationMode.parse()),
State: o.State,
}
}
type systemUpdate struct {
UserConfig rawSystemUserConfig `json:"user_config"`
}
type SystemUserConfig struct {
AdminState SystemAdminState `json:"admin_state,omitempty"`
AosHclModel ObjectId `json:"aos_hcl_model,omitempty"`
Location string `json:"location,omitempty"`
}
func (o *SystemUserConfig) raw() *rawSystemUserConfig {
return &rawSystemUserConfig{
AdminState: o.AdminState.raw(),
AosHclModel: o.AosHclModel,
Location: o.Location,
}
}
type rawSystemUserConfig struct {
AdminState rawSystemAdminState `json:"admin_state,omitempty"`
AosHclModel ObjectId `json:"aos_hcl_model,omitempty"`
Location string `json:"location,omitempty"`
}
func (o *rawSystemUserConfig) polish() (*SystemUserConfig, error) {
adminState, err := o.AdminState.parse()
if err != nil {
return nil, err
}
return &SystemUserConfig{
AdminState: SystemAdminState(adminState),
AosHclModel: o.AosHclModel,
Location: o.Location,
}, nil
}
func (o *Client) listSystems(ctx context.Context) ([]SystemId, error) {
method := http.MethodOptions
urlStr := apiUrlSystems
apstraUrl, err := url.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("error parsing url '%s' - %w", urlStr, err)
}
response := &optionsSystemsResponse{}
err = o.talkToApstra(ctx, &talkToApstraIn{
method: method,
url: apstraUrl,
apiResponse: response,
})
if err != nil {
return nil, fmt.Errorf("error listing systems - %w", err)
}
return response.Items, nil
}
func (o *Client) getSystemInfo(ctx context.Context, id SystemId) (*ManagedSystemInfo, error) {
response := &rawManagedSystemInfo{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlSystemsById, id),
apiResponse: response,
})
if err != nil {
var ttae TalkToApstraErr
if errors.As(err, &ttae) && ttae.Response.StatusCode == http.StatusNotFound {
return nil, ApstraClientErr{
errType: ErrNotfound,
err: err,
}
}
return nil, err
}
return response.polish()
}
func (o *Client) getAllSystemsInfo(ctx context.Context) ([]ManagedSystemInfo, error) {
response := &struct{ Items []rawManagedSystemInfo }{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: apiUrlSystems,
apiResponse: response,
})
if err != nil {
return nil, err
}
result := make([]ManagedSystemInfo, len(response.Items))
for i, rmsi := range response.Items {
polished, err := rmsi.polish()
if err != nil {
return nil, err
}
result[i] = *polished
}
return result, nil
}
func (o *Client) updateSystemByAgentId(ctx context.Context, agentId ObjectId, cfg *SystemUserConfig) error {
agent, err := o.getSystemAgent(ctx, agentId)
if err != nil {
return fmt.Errorf("cannot get info for agent '%s' - %w", agentId, err)
}
if agent.Status.SystemId == "" {
return fmt.Errorf("cannot acknowledge system from agent '%s' - system ID is empty", agentId)
}
return o.updateSystem(ctx, agent.Status.SystemId, cfg)
}
func (o *Client) updateSystem(ctx context.Context, id SystemId, cfg *SystemUserConfig) error {
method := http.MethodPut
urlStr := fmt.Sprintf(apiUrlSystemsById, id)
apstraUrl, err := url.Parse(urlStr)
if err != nil {
return fmt.Errorf("error parsing url '%s' - %w", urlStr, err)
}
return o.talkToApstra(ctx, &talkToApstraIn{
method: method,
url: apstraUrl,
apiInput: &systemUpdate{UserConfig: *cfg.raw()},
})
}
func (o *Client) deleteSystem(ctx context.Context, id SystemId) error {
method := http.MethodDelete
urlStr := fmt.Sprintf(apiUrlSystemsById, id)
apstraUrl, err := url.Parse(urlStr)
if err != nil {
return fmt.Errorf("error parsing url '%s' - %w", urlStr, err)
}
return o.talkToApstra(ctx, &talkToApstraIn{
method: method,
url: apstraUrl,
})
}
func AllNodeDeployModes() []NodeDeployMode {
i := 0
var result []NodeDeployMode
for {
var ndm NodeDeployMode
err := ndm.FromString(NodeDeployMode(i).String())
if err != nil {
return result[:i]
}
result = append(result, ndm)
i++
}
}