/
api_system_agents.go
1056 lines (938 loc) · 30.1 KB
/
api_system_agents.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package apstra
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
)
const (
apiUrlSystemAgents = "/api/system-agents"
apiUrlSystemAgentsPrefix = "/api/system-agents" + apiUrlPathDelim
apiUrlSystemAgentsById = apiUrlSystemAgentsPrefix + "%s"
apiUrlSystemAgentCheck = apiUrlSystemAgentsPrefix + "%s" + "/check"
apiUrlSystemAgentInstall = apiUrlSystemAgentsPrefix + "%s" + "/install-agent"
apiUrlSystemAgentUninstall = apiUrlSystemAgentsPrefix + "%s" + "/uninstall-agent"
apiUrlSystemAgentJobHistory = apiUrlSystemAgentsPrefix + "%s" + "/job-history"
offBox = "offbox"
onBox = "onbox"
)
const ( // new block resets iota to 0
AgentCxnStateNone = AgentCxnState(iota)
AgentCxnStateConnected
AgentCxnStateDisconnected
AgentCxnStateAuthFail
AgentCxnStateUnknown
agentCxnStateNone = rawAgentCxnState("")
agentCxnStateConnected = rawAgentCxnState("connected")
agentCxnStateDisconnected = rawAgentCxnState("disconnected")
agentCxnStateAuthFail = rawAgentCxnState("auth_failed")
agentCxnStateUnknown = "system agent connection state %d unknown"
)
const ( // new block resets iota to 0
AgentJobTypeNull = AgentJobType(iota) // default type 0
AgentJobTypeNone
AgentJobTypeInstall
AgentJobTypeCheck
AgentJobTypeRevertToPristine
AgentJobTypeUninstall
AgentJobTypeUpgrade
AgentJobTypeUnknown
agentJobTypeNull = rawAgentJobType("")
agentJobTypeNone = rawAgentJobType("none")
agentJobTypeInstall = rawAgentJobType("install")
agentJobTypeCheck = rawAgentJobType("check")
agentJobTypeRevertToPristine = rawAgentJobType("revertToPristine")
agentJobTypeUninstall = rawAgentJobType("uninstall")
agentJobTypeUpgrade = rawAgentJobType("upgrade")
agentJobTypeUnknown = "system agent job type %d unknown"
)
const ( // new block resets iota to 0
AgentJobStateNull = AgentJobState(iota)
AgentJobStateInit
AgentJobStateInProgress
AgentJobStateSuccess
AgentJobStateFailed
AgentJobStateUnknown
agentJobStateNull = rawAgentJobState("")
agentJobStateInit = rawAgentJobState("init")
agentJobStateInProgress = rawAgentJobState("inprogress")
agentJobStateSuccess = rawAgentJobState("success")
agentJobStateFailed = rawAgentJobState("failed")
agentJobStateUnknown = "system agent job state %d unknown"
)
const ( // new block resets iota to 0
AgentPlatformNull = AgentPlatform(iota) // default type 0
AgentPlatformJunos
AgentPlatformEOS
AgentPlatformNXOS
AgentPlatformUnknown
agentPlatformNull = rawAgentPlatform("")
agentPlatformJunos = rawAgentPlatform("junos")
agentPlatformEOS = rawAgentPlatform("eos")
agentPlatformNXOS = rawAgentPlatform("nxos")
agentPlatformUnknown = "system agent platform %d unknown"
)
type JobId int
type AgentTypeOffbox bool
func (o AgentTypeOffbox) raw() rawAgentType {
if o {
return offBox
}
return onBox
}
type rawAgentType string
func (o rawAgentType) parse() AgentTypeOffbox {
return o == offBox
}
type AgentJobState int
func (o AgentJobState) Int() int {
return int(o)
}
func (o AgentJobState) String() string {
switch o {
case AgentJobStateNull:
return string(agentJobStateNull)
case AgentJobStateInit:
return string(agentJobStateInit)
case AgentJobStateInProgress:
return string(agentJobStateInProgress)
case AgentJobStateSuccess:
return string(agentJobStateSuccess)
case AgentJobStateFailed:
return string(agentJobStateFailed)
default:
return fmt.Sprintf(agentJobStateUnknown, o)
}
}
//lint:ignore U1000 linter bug, the method is used
func (o AgentJobState) raw() rawAgentJobState {
return rawAgentJobState(o.String())
}
func (o AgentJobState) HasExited() bool {
switch o {
// todo: more states which look like "exited" ?
case AgentJobStateFailed:
return true
case AgentJobStateSuccess:
return true
}
return false
}
type rawAgentJobState string
func (o rawAgentJobState) string() string {
return string(o)
}
func (o rawAgentJobState) parse() int {
switch o {
case agentJobStateNull:
return int(AgentJobStateNull)
case agentJobStateInit:
return int(AgentJobStateInit)
case agentJobStateInProgress:
return int(AgentJobStateInProgress)
case agentJobStateSuccess:
return int(AgentJobStateSuccess)
case agentJobStateFailed:
return int(AgentJobStateFailed)
default:
return int(AgentJobStateUnknown)
}
}
type AgentJobType int
func (o AgentJobType) Int() int {
return int(o)
}
func (o AgentJobType) String() string {
switch o {
case AgentJobTypeNull:
return string(agentJobTypeNull)
case AgentJobTypeInstall:
return string(agentJobTypeInstall)
case AgentJobTypeCheck:
return string(agentJobTypeCheck)
case AgentJobTypeRevertToPristine:
return string(agentJobTypeRevertToPristine)
case AgentJobTypeUninstall:
return string(agentJobTypeUninstall)
case AgentJobTypeUpgrade:
return string(agentJobTypeUpgrade)
case AgentJobTypeNone:
return string(agentJobTypeNone)
default:
return fmt.Sprintf(agentJobTypeUnknown, o)
}
}
//lint:ignore U1000 linter bug
func (o AgentJobType) raw() rawAgentJobType {
return rawAgentJobType(o.String())
}
type rawAgentJobType string
func (o rawAgentJobType) string() string {
return string(o)
}
func (o rawAgentJobType) parse() int {
switch o {
case agentJobTypeNull:
return int(AgentJobTypeNull)
case agentJobTypeInstall:
return int(AgentJobTypeInstall)
case agentJobTypeCheck:
return int(AgentJobTypeCheck)
case agentJobTypeRevertToPristine:
return int(AgentJobTypeRevertToPristine)
case agentJobTypeUninstall:
return int(AgentJobTypeUninstall)
case agentJobTypeUpgrade:
return int(AgentJobTypeUpgrade)
case agentJobTypeNone:
return int(AgentJobTypeNone)
default:
return int(AgentJobTypeUnknown)
}
}
type AgentPlatform int
func (o AgentPlatform) Int() int {
return int(o)
}
func (o AgentPlatform) String() string {
switch o {
case AgentPlatformNull:
return string(agentPlatformNull)
case AgentPlatformJunos:
return string(agentPlatformJunos)
case AgentPlatformEOS:
return string(agentPlatformEOS)
case AgentPlatformNXOS:
return string(agentPlatformNXOS)
default:
return fmt.Sprintf(agentPlatformUnknown, o)
}
}
func (o AgentPlatform) offbox() AgentTypeOffbox {
switch o {
case AgentPlatformJunos:
return true
default:
return false
}
}
type rawAgentPlatform string
func (o rawAgentPlatform) string() string {
return string(o)
}
func (o rawAgentPlatform) parse() int {
switch o {
case agentPlatformNull:
return int(AgentPlatformNull)
case agentPlatformEOS:
return int(AgentPlatformEOS)
case agentPlatformJunos:
return int(AgentPlatformJunos)
case agentPlatformNXOS:
return int(AgentPlatformNXOS)
default:
return int(AgentPlatformUnknown)
}
}
type AgentCxnState int
func (o AgentCxnState) Int() int {
return int(o)
}
func (o AgentCxnState) String() string {
switch o {
case AgentCxnStateNone:
return string(agentCxnStateNone)
case AgentCxnStateConnected:
return string(agentCxnStateConnected)
case AgentCxnStateDisconnected:
return string(agentCxnStateDisconnected)
case AgentCxnStateAuthFail:
return string(agentCxnStateAuthFail)
default:
return fmt.Sprintf(agentCxnStateUnknown, o)
}
}
type rawAgentCxnState string
func (o rawAgentCxnState) string() string {
return string(o)
}
func (o rawAgentCxnState) parse() int {
switch o {
case agentCxnStateNone:
return int(AgentCxnStateNone)
case agentCxnStateConnected:
return int(AgentCxnStateConnected)
case agentCxnStateDisconnected:
return int(AgentCxnStateDisconnected)
case agentCxnStateAuthFail:
return int(AgentCxnStateAuthFail)
default:
return int(AgentCxnStateUnknown)
}
}
type AgentPackages map[string]string
// todo: method for inventory
// to be checked by agent profile and agent create/update methods,
// refuse to reference packages which do not exist.
func (o *AgentPackages) raw() rawAgentPackages {
// todo: one of these lines causes 'null' in JSON output, while the other causes '[]' ... which one is correct for the API?
//raw := rawAgentPackages{}
var raw rawAgentPackages
for k, v := range *o {
raw = append(raw, k+apstraSystemAgentPlatformStringSep+v)
}
return raw
}
type rawAgentPackages []string
func (o rawAgentPackages) polish() AgentPackages {
var polish AgentPackages = make(map[string]string)
for _, s := range o {
kv := strings.SplitN(s, apstraSystemAgentPlatformStringSep, 2)
switch len(kv) {
case 2:
polish[kv[0]] = kv[1]
case 1:
polish[kv[0]] = ""
}
}
return polish
}
type rawAgentJobTypes []rawAgentJobType
func (o rawAgentJobTypes) polish() []AgentJobType {
result := make([]AgentJobType, len(o))
for i, t := range o {
result[i] = AgentJobType(t.parse())
}
return result
}
type agentJobHistoryResponse struct {
Items []rawAgentJobStatus `json:"items"`
}
type optionsAgentsResponse struct {
Items []ObjectId `json:"items"`
Methods []string `json:"methods"`
}
type getAgentsResponse struct {
Items []rawSystemAgent `json:"items"`
}
type AgentStatus struct {
ConnectionState AgentCxnState
PackagesInstalled AgentPackages
CurrentTask string
PendingType string
JobId JobId
OperationMode SystemManagementLevel
PlatformVersion string
Platform AgentPlatform
State string
SystemId SystemId
HasCredential bool
Error string
StatusMessage string
AosVersion string
}
//lint:ignore U1000 linter bug
func (o *AgentStatus) raw() *rawAgentStatus {
return &rawAgentStatus{
ConnectionState: rawAgentCxnState(o.ConnectionState.String()),
PackagesInstalled: o.PackagesInstalled.raw(),
CurrentTask: o.CurrentTask,
PendingType: o.PendingType,
JobId: o.JobId,
OperationMode: systemManagementLevel(o.OperationMode.String()),
PlatformVersion: o.PlatformVersion,
Platform: rawAgentPlatform(o.Platform.String()),
State: o.State,
SystemId: o.SystemId,
HasCredential: o.HasCredential,
Error: o.Error,
StatusMessage: o.StatusMessage,
AosVersion: o.AosVersion,
}
}
type rawAgentStatus struct {
ConnectionState rawAgentCxnState `json:"connection_state"`
PackagesInstalled rawAgentPackages `json:"packages_installed"`
CurrentTask string `json:"current_task"`
PendingType string `json:"pending_type"`
JobId JobId `json:"job_id"`
OperationMode systemManagementLevel `json:"operation_mode"`
PlatformVersion string `json:"platform_version"`
Platform rawAgentPlatform `json:"platform"`
State string `json:"state"`
SystemId SystemId `json:"system_id"`
HasCredential bool `json:"has_credential"`
Error string `json:"error"`
StatusMessage string `json:"status_message"`
AosVersion string `json:"aos_version"`
}
func (o *rawAgentStatus) polish() (*AgentStatus, error) {
operationMode, err := o.OperationMode.parse()
if err != nil {
return nil, err
}
return &AgentStatus{
ConnectionState: AgentCxnState(o.ConnectionState.parse()),
PackagesInstalled: o.PackagesInstalled.polish(),
CurrentTask: o.CurrentTask,
PendingType: o.PendingType,
JobId: o.JobId,
OperationMode: SystemManagementLevel(operationMode),
PlatformVersion: o.PlatformVersion,
Platform: AgentPlatform(o.Platform.parse()),
State: o.State,
SystemId: o.SystemId,
HasCredential: o.HasCredential,
Error: o.Error,
StatusMessage: o.StatusMessage,
AosVersion: o.AosVersion,
}, nil
}
type AgentJobStatus struct {
Started time.Time
JobType AgentJobType
Finished time.Time
HostId string
CurrentTask string
IsLogAvailable bool
JobId JobId
Created time.Time
State AgentJobState
AgentTypeOffBox AgentTypeOffbox
Error string
}
//lint:ignore U1000 linter bug
func (o *AgentJobStatus) raw() *rawAgentJobStatus {
return &rawAgentJobStatus{
Started: o.Started,
JobType: o.JobType.raw(),
Finished: o.Finished,
HostId: o.HostId,
CurrentTask: o.CurrentTask,
IsLogAvailable: o.IsLogAvailable,
JobId: o.JobId,
Created: o.Created,
State: o.State.raw(),
AgentType: o.AgentTypeOffBox.raw(),
Error: o.Error,
}
}
type rawAgentJobStatus struct {
Started time.Time `json:"started"`
JobType rawAgentJobType `json:"job_type"`
Finished time.Time `json:"finished"`
HostId string `json:"host_id"` // todo: device s/n? own type?
CurrentTask string `json:"current_task"`
IsLogAvailable bool `json:"is_log_available"`
JobId JobId `json:"job_id"`
Created time.Time `json:"created"`
State rawAgentJobState `json:"state"`
AgentType rawAgentType `json:"agent_type"`
Error string `json:"error"`
}
func (o *rawAgentJobStatus) polish() *AgentJobStatus {
return &AgentJobStatus{
Started: o.Started,
JobType: AgentJobType(o.JobType.parse()),
Finished: o.Finished,
HostId: o.HostId,
CurrentTask: o.CurrentTask,
IsLogAvailable: o.IsLogAvailable,
JobId: o.JobId,
Created: o.Created,
State: AgentJobState(o.State.parse()),
AgentTypeOffBox: o.AgentType.parse(),
Error: o.Error,
}
}
type TelemetryExtStatus struct {
PackagesInstalled AgentPackages `json:"packages_installed"`
StatusMessage string `json:"status_message"`
}
//lint:ignore U1000 linter bug
func (o *TelemetryExtStatus) raw() *rawTelemetryExtStatus {
return &rawTelemetryExtStatus{
PackagesInstalled: o.PackagesInstalled.raw(),
StatusMessage: o.StatusMessage,
}
}
type rawTelemetryExtStatus struct {
PackagesInstalled rawAgentPackages `json:"packages_installed"`
StatusMessage string `json:"status_message"`
}
func (o *rawTelemetryExtStatus) polish() *TelemetryExtStatus {
return &TelemetryExtStatus{
PackagesInstalled: o.PackagesInstalled.polish(),
StatusMessage: o.StatusMessage,
}
}
type PlatformStatus struct {
JobId JobId `json:"job_id"`
PlatformVersion string `json:"platform_version"`
Platform string `json:"platform"`
State string `json:"state"`
HasCredential bool `json:"has_credential"`
Error string `json:"error"`
CurrentTask string `json:"current_task"`
}
type ContainerStatus struct {
Status string `json:"status"`
Name string `json:"name"`
TaskId string `json:"task_id"`
LastUpdate string `json:"last_update"`
Host string `json:"host"`
Error string `json:"error"`
ServiceId string `json:"service_id"`
}
type DeviceFacts struct {
DeviceOsFamily string `json:"device_os_family"`
Hostname string `json:"hostname"`
DeviceState string `json:"device_state"`
DeviceOsVersion string `json:"device_os_version"`
}
type PlatformConfig struct {
ContainerEnable bool `json:"container_enable"`
}
type SystemAgent struct {
Id ObjectId `json:"id"`
Config SystemAgentConfig `json:"config"`
RunningConfig SystemAgentConfig `json:"running_config"`
LastJobStatus AgentJobStatus `json:"last_job_status"`
Status AgentStatus `json:"status"`
TelemetryExtStatus TelemetryExtStatus `json:"telemetry_ext_status"`
ContainerStatus ContainerStatus `json:"container_status"`
DeviceFacts DeviceFacts `json:"device_facts"`
PlatformConfig PlatformConfig `json:"platform_config"`
PlatformStatus PlatformStatus `json:"platform_status"`
}
//lint:ignore U1000 keep for future use
func (o SystemAgent) raw() *rawSystemAgent {
return &rawSystemAgent{
Config: *o.Config.raw(),
Id: o.Id,
LastJobStatus: *o.LastJobStatus.raw(),
RunningConfig: *o.RunningConfig.raw(),
Status: *o.Status.raw(),
TelemetryExtStatus: *o.TelemetryExtStatus.raw(),
ContainerStatus: o.ContainerStatus,
DeviceFacts: o.DeviceFacts,
PlatformConfig: o.PlatformConfig,
PlatformStatus: o.PlatformStatus,
}
}
type rawSystemAgent struct {
Id ObjectId `json:"id"`
Status rawAgentStatus `json:"status"`
Config rawSystemAgentConfig `json:"config"`
RunningConfig rawSystemAgentConfig `json:"running_config"`
LastJobStatus rawAgentJobStatus `json:"last_job_status"`
TelemetryExtStatus rawTelemetryExtStatus `json:"telemetry_ext_status"`
DeviceFacts DeviceFacts `json:"device_facts"`
ContainerStatus ContainerStatus `json:"container_status"`
PlatformConfig PlatformConfig `json:"platform_config"`
PlatformStatus PlatformStatus `json:"platform_status"`
}
func (o *rawSystemAgent) polish() (*SystemAgent, error) {
status, err := o.Status.polish()
if err != nil {
return nil, err
}
config, err := o.Config.polish()
if err != nil {
return nil, err
}
runningConfig, err := o.RunningConfig.polish()
if err != nil {
return nil, err
}
return &SystemAgent{
Id: o.Id,
Config: *config,
LastJobStatus: *o.LastJobStatus.polish(),
RunningConfig: *runningConfig,
Status: *status,
TelemetryExtStatus: *o.TelemetryExtStatus.polish(),
ContainerStatus: o.ContainerStatus,
DeviceFacts: o.DeviceFacts,
PlatformConfig: o.PlatformConfig,
PlatformStatus: o.PlatformStatus,
}, nil
}
type SystemAgentConfig struct {
Id ObjectId
Label string
Profile ObjectId
ForcePackageInstall bool
InstallRequirements bool
Packages AgentPackages
OpenOptions map[string]string
Platform string
ManagementIp string
AgentTypeOffBox AgentTypeOffbox
OperationMode SystemManagementLevel
AllowedJobTypes []AgentJobType
}
//lint:ignore U1000 keep for future use
func (o *SystemAgentConfig) raw() *rawSystemAgentConfig {
return &rawSystemAgentConfig{
Profile: o.Profile,
ForcePackageInstall: o.ForcePackageInstall,
InstallRequirements: o.InstallRequirements,
Packages: o.Packages.raw(),
OpenOptions: o.OpenOptions,
Label: o.Label,
Platform: o.Platform,
ManagementIp: o.ManagementIp,
AgentType: o.AgentTypeOffBox.raw(),
OperationMode: systemManagementLevel(o.OperationMode.String()),
Id: o.Id,
}
}
type rawSystemAgentConfig struct {
Profile ObjectId `json:"profile"`
ForcePackageInstall bool `json:"force_package_install"`
InstallRequirements bool `json:"install_requirements"`
Packages rawAgentPackages `json:"packages"`
OpenOptions map[string]string `json:"open_options"`
Label string `json:"label"`
Platform string `json:"platform"`
ManagementIp string `json:"management_ip"`
AgentType rawAgentType `json:"agent_type"`
OperationMode systemManagementLevel `json:"operation_mode"`
Id ObjectId `json:"id"`
AllowedJobTypes rawAgentJobTypes `json:"allowed_job_types"`
}
func (o *rawSystemAgentConfig) polish() (*SystemAgentConfig, error) {
operationMode, err := o.OperationMode.parse()
if err != nil {
return nil, err
}
return &SystemAgentConfig{
Profile: o.Profile,
ForcePackageInstall: o.ForcePackageInstall,
InstallRequirements: o.InstallRequirements,
Packages: o.Packages.polish(),
OpenOptions: o.OpenOptions,
Label: o.Label,
Platform: o.Platform,
ManagementIp: o.ManagementIp,
AgentTypeOffBox: o.AgentType.parse(),
OperationMode: SystemManagementLevel(operationMode),
Id: o.Id,
AllowedJobTypes: o.AllowedJobTypes.polish(),
}, nil
}
// SystemAgentRequest is used when creating/updating system agents
type SystemAgentRequest struct {
AgentTypeOffbox AgentTypeOffbox
ManagementIp string
OperationMode SystemManagementLevel
JobOnCreate AgentJobType
Profile ObjectId
Username string
Password string
Packages AgentPackages
ForcePackageInstall bool
InstallRequirements bool
EnableMonitor bool
Label string
Platform AgentPlatform
}
func (o *SystemAgentRequest) raw() *rawSystemAgentRequest {
var platform rawAgentPlatform
if o.AgentTypeOffbox {
platform = rawAgentPlatform(o.Platform.String())
}
return &rawSystemAgentRequest{
AgentType: o.AgentTypeOffbox.raw(),
ManagementIp: o.ManagementIp,
Profile: o.Profile,
OperationMode: systemManagementLevel(o.OperationMode.String()),
JobOnCreate: rawAgentJobType(o.JobOnCreate.String()),
Username: o.Username,
ForcePackageInstall: o.ForcePackageInstall,
InstallRequirements: o.InstallRequirements,
EnableMonitor: o.EnableMonitor,
Password: o.Password,
Packages: o.Packages.raw(),
Label: o.Label,
Platform: platform,
}
}
type rawSystemAgentRequest struct {
AgentType rawAgentType `json:"agent_type,omitempty"`
ManagementIp string `json:"management_ip,omitempty"`
Profile ObjectId `json:"profile,omitempty"`
OperationMode systemManagementLevel `json:"operation_mode,omitempty"`
JobOnCreate rawAgentJobType `json:"job_on_create,omitempty"`
Username string `json:"username,omitempty"`
ForcePackageInstall bool `json:"force_package_install,omitempty"`
InstallRequirements bool `json:"install_requirements,omitempty"`
EnableMonitor bool `json:"enable_monitor,omitempty"`
Password string `json:"password,omitempty"`
Packages rawAgentPackages `json:"packages,omitempty"`
Label string `json:"label,omitempty"`
Platform rawAgentPlatform `json:"platform,omitempty"`
}
type jobIdResponse struct {
Id JobId `json:"id"`
}
func (o *Client) listSystemAgents(ctx context.Context) ([]ObjectId, error) {
response := &optionsAgentsResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodOptions,
urlStr: apiUrlSystemAgents,
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.Items, nil
}
func (o *Client) getSystemAgent(ctx context.Context, id ObjectId) (*SystemAgent, error) {
response := &rawSystemAgent{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlSystemAgentsById, id),
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.polish()
}
func (o *Client) getAllSystemAgents(ctx context.Context) ([]SystemAgent, error) {
response := &getAgentsResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: apiUrlSystemAgents,
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
var result []SystemAgent
for _, i := range response.Items {
polished, err := i.polish()
if err != nil {
return nil, err
}
result = append(result, *polished)
}
return result, nil
}
func (o *Client) getSystemAgentByManagementIp(ctx context.Context, ip string) (*SystemAgent, error) {
asa, err := o.getAllSystemAgents(ctx)
if err != nil {
return nil, err
}
for _, a := range asa {
if a.Config.ManagementIp == ip || a.RunningConfig.ManagementIp == ip { // what's the difference?
return &a, nil
}
}
return nil, ClientErr{
errType: ErrNotfound,
err: fmt.Errorf("no System Agent with management IP '%s' found", ip),
}
}
func (o *Client) createSystemAgent(ctx context.Context, request *SystemAgentRequest) (ObjectId, error) {
response := &objectIdResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
urlStr: apiUrlSystemAgents,
apiInput: request.raw(),
apiResponse: response,
})
if err != nil {
return "", convertTtaeToAceWherePossible(err)
}
return response.Id, nil
}
func (o *Client) updateSystemAgent(ctx context.Context, id ObjectId, cfg *SystemAgentRequest) error {
rawCfg := cfg.raw()
rawCfg.AgentType = "" // cannot change agent type
response := &objectIdResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPatch,
urlStr: fmt.Sprintf(apiUrlSystemAgentsById, id),
apiInput: rawCfg,
apiResponse: response,
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *Client) deleteSystemAgent(ctx context.Context, id ObjectId) error {
agentInfo, err := o.getSystemAgent(ctx, id)
if err != nil {
return fmt.Errorf("error fetching agent info prior to deletion - %w", convertTtaeToAceWherePossible(err))
}
_, err = o.SystemAgentRunJob(ctx, id, AgentJobTypeUninstall)
if err != nil {
return fmt.Errorf("error running agent uninstall job prior to deletion - %w", convertTtaeToAceWherePossible(err))
}
err = o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodDelete,
urlStr: fmt.Sprintf(apiUrlSystemAgentsById, id),
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
// wait for agent's system comms status to go down before returning from "deleteSystemAgent" because
// a) deleteSystem is probably next in line
// b) apstra complains:
// Can't delete the device in neither STOCKED nor DECOMM state. Device is in OOS-READY state.
if agentInfo.Status.SystemId != "" {
minuteCountdown, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()
for {
systemInfo, err := o.getSystemInfo(minuteCountdown, agentInfo.Status.SystemId)
if err != nil {
return fmt.Errorf("error checking system state after agent deletion - %w", convertTtaeToAceWherePossible(err))
}
if systemInfo.Status.CommState == systemCommsOn {
continue
}
break
}
}
return nil
}
func (o *Client) systemAgentStartJob(ctx context.Context, id ObjectId, job AgentJobType) (JobId, error) {
var urlStr string
switch job {
case AgentJobTypeCheck:
urlStr = fmt.Sprintf(apiUrlSystemAgentCheck, id)
case AgentJobTypeInstall:
urlStr = fmt.Sprintf(apiUrlSystemAgentInstall, id)
case AgentJobTypeUninstall:
urlStr = fmt.Sprintf(apiUrlSystemAgentUninstall, id)
default:
return 0, fmt.Errorf("don't know how to run job '%s' (type %d)", job.String(), job)
}
response := &jobIdResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
urlStr: urlStr,
apiResponse: response,
})
if err != nil {
return 0, convertTtaeToAceWherePossible(err)
}
return response.Id, nil
}
func (o *Client) getSystemAgentJobHistory(ctx context.Context, id ObjectId) ([]AgentJobStatus, error) {
response := &agentJobHistoryResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlSystemAgentJobHistory, id),
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
var result []AgentJobStatus
for _, js := range response.Items {
result = append(result, *js.polish())
}
return result, nil
}
func (o *Client) getSystemAgentJobStatus(ctx context.Context, agentId ObjectId, jobId JobId) (*AgentJobStatus, error) {
jobs, err := o.getSystemAgentJobHistory(ctx, agentId)
if err != nil {
return nil, fmt.Errorf("error getting agent job history - %w", err)
}
// pick out and return the requested jobId
for _, j := range jobs {
if j.JobId == jobId {
return &j, nil
}
}
// jobId not found - return error
return nil, ClientErr{
errType: ErrNotfound,
err: fmt.Errorf("agent '%s' job history does not include job '%d'", agentId, jobId),
}
}
func (o *Client) systemAgentWaitForJobToExist(ctx context.Context, agentId ObjectId, jobId JobId) error {
// loop until we find a reason to return
for {
// bail out if our context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
_, err := o.getSystemAgentJobStatus(ctx, agentId, jobId)
if err != nil {
var ace ClientErr
if !(errors.As(err, &ace) && ace.Type() == ErrNotfound) {
// error other than notfound - stop looking - return error
return fmt.Errorf("error getting job status - %w", err)
}
} else {
// no error - the job exists - clean return
return nil
}
time.Sleep(clientPollingIntervalMs * time.Millisecond)
}
}
func (o *Client) systemAgentWaitForJobTermination(ctx context.Context, agentId ObjectId, jobId JobId) error {
// loop until we find a reason to return
for {
// bail out if our context is cancelled
select {
case <-ctx.Done():