forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
2708 lines (2385 loc) · 67.7 KB
/
models.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 pkger
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/notification"
icheck "github.com/influxdata/influxdb/notification/check"
"github.com/influxdata/influxdb/notification/endpoint"
"github.com/influxdata/influxdb/notification/rule"
)
// Package kind types.
const (
KindUnknown Kind = ""
KindBucket Kind = "Bucket"
KindCheck Kind = "Check"
KindCheckDeadman Kind = "CheckDeadman"
KindCheckThreshold Kind = "CheckThreshold"
KindDashboard Kind = "Dashboard"
KindLabel Kind = "Label"
KindNotificationEndpoint Kind = "NotificationEndpoint"
KindNotificationEndpointHTTP Kind = "NotificationEndpointHTTP"
KindNotificationEndpointPagerDuty Kind = "NotificationEndpointPagerDuty"
KindNotificationEndpointSlack Kind = "NotificationEndpointSlack"
KindNotificationRule Kind = "NotificationRule"
KindPackage Kind = "Package"
KindTask Kind = "Task"
KindTelegraf Kind = "Telegraf"
KindVariable Kind = "Variable"
)
var kinds = map[Kind]bool{
KindBucket: true,
KindCheck: true,
KindCheckDeadman: true,
KindCheckThreshold: true,
KindDashboard: true,
KindLabel: true,
KindNotificationEndpoint: true,
KindNotificationEndpointHTTP: true,
KindNotificationEndpointPagerDuty: true,
KindNotificationEndpointSlack: true,
KindNotificationRule: true,
KindTask: true,
KindTelegraf: true,
KindVariable: true,
}
var kindsUniqByName = map[Kind]bool{
KindBucket: true,
KindCheck: true,
KindCheckDeadman: true,
KindCheckThreshold: true,
KindLabel: true,
KindNotificationEndpoint: true,
KindNotificationEndpointHTTP: true,
KindNotificationEndpointPagerDuty: true,
KindNotificationEndpointSlack: true,
KindVariable: true,
}
// Kind is a resource kind.
type Kind string
// String provides the kind in human readable form.
func (k Kind) String() string {
if kinds[k] {
return string(k)
}
if k == KindUnknown {
return "unknown"
}
return string(k)
}
// OK validates the kind is valid.
func (k Kind) OK() error {
if k == KindUnknown {
return errors.New("invalid kind")
}
if !kinds[k] {
return errors.New("unsupported kind provided")
}
return nil
}
// ResourceType converts a kind to a known resource type (if applicable).
func (k Kind) ResourceType() influxdb.ResourceType {
switch k {
case KindBucket:
return influxdb.BucketsResourceType
case KindCheck, KindCheckDeadman, KindCheckThreshold:
return influxdb.ChecksResourceType
case KindDashboard:
return influxdb.DashboardsResourceType
case KindLabel:
return influxdb.LabelsResourceType
case KindNotificationEndpoint,
KindNotificationEndpointHTTP,
KindNotificationEndpointPagerDuty,
KindNotificationEndpointSlack:
return influxdb.NotificationEndpointResourceType
case KindNotificationRule:
return influxdb.NotificationRuleResourceType
case KindTask:
return influxdb.TasksResourceType
case KindTelegraf:
return influxdb.TelegrafsResourceType
case KindVariable:
return influxdb.VariablesResourceType
default:
return ""
}
}
func (k Kind) is(comps ...Kind) bool {
for _, c := range comps {
if c == k {
return true
}
}
return false
}
// SafeID is an equivalent influxdb.ID that encodes safely with
// zero values (influxdb.ID == 0).
type SafeID influxdb.ID
// Encode will safely encode the id.
func (s SafeID) Encode() ([]byte, error) {
id := influxdb.ID(s)
b, _ := id.Encode()
return b, nil
}
// String prints a encoded string representation of the id.
func (s SafeID) String() string {
return influxdb.ID(s).String()
}
// Diff is the result of a service DryRun call. The diff outlines
// what is new and or updated from the current state of the platform.
type Diff struct {
Buckets []DiffBucket `json:"buckets"`
Checks []DiffCheck `json:"checks"`
Dashboards []DiffDashboard `json:"dashboards"`
Labels []DiffLabel `json:"labels"`
LabelMappings []DiffLabelMapping `json:"labelMappings"`
NotificationEndpoints []DiffNotificationEndpoint `json:"notificationEndpoints"`
NotificationRules []DiffNotificationRule `json:"notificationRules"`
Tasks []DiffTask `json:"tasks"`
Telegrafs []DiffTelegraf `json:"telegrafConfigs"`
Variables []DiffVariable `json:"variables"`
}
// HasConflicts provides a binary t/f if there are any changes within package
// after dry run is complete.
func (d Diff) HasConflicts() bool {
for _, b := range d.Buckets {
if b.hasConflict() {
return true
}
}
for _, l := range d.Labels {
if l.hasConflict() {
return true
}
}
for _, v := range d.Variables {
if v.hasConflict() {
return true
}
}
return false
}
// DiffBucketValues are the varying values for a bucket.
type DiffBucketValues struct {
Description string `json:"description"`
RetentionRules retentionRules `json:"retentionRules"`
}
// DiffBucket is a diff of an individual bucket.
type DiffBucket struct {
ID SafeID `json:"id"`
Name string `json:"name"`
New DiffBucketValues `json:"new"`
Old *DiffBucketValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
}
func newDiffBucket(b *bucket, i *influxdb.Bucket) DiffBucket {
diff := DiffBucket{
Name: b.Name(),
New: DiffBucketValues{
Description: b.Description,
RetentionRules: b.RetentionRules,
},
}
if i != nil {
diff.ID = SafeID(i.ID)
diff.Old = &DiffBucketValues{
Description: i.Description,
}
if i.RetentionPeriod > 0 {
diff.Old.RetentionRules = retentionRules{newRetentionRule(i.RetentionPeriod)}
}
}
return diff
}
// IsNew indicates whether a pkg bucket is going to be new to the platform.
func (d DiffBucket) IsNew() bool {
return d.ID == SafeID(0)
}
func (d DiffBucket) hasConflict() bool {
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
}
// DiffCheckValues are the varying values for a check.
type DiffCheckValues struct {
influxdb.Check
}
// UnmarshalJSON decodes the check values.
func (d *DiffCheckValues) UnmarshalJSON(b []byte) (err error) {
d.Check, err = icheck.UnmarshalJSON(b)
return
}
// DiffCheck is a diff of an individual check.
type DiffCheck struct {
ID SafeID `json:"id"`
Name string `json:"name"`
New DiffCheckValues `json:"new"`
Old *DiffCheckValues `json:"old"`
}
func newDiffCheck(c *check, iCheck influxdb.Check) DiffCheck {
diff := DiffCheck{
Name: c.Name(),
New: DiffCheckValues{
Check: c.summarize().Check,
},
}
if iCheck != nil {
diff.ID = SafeID(iCheck.GetID())
diff.Old = &DiffCheckValues{
Check: iCheck,
}
}
return diff
}
// IsNew determines if the check in the pkg is new to the platform.
func (d DiffCheck) IsNew() bool {
return d.Old == nil
}
// DiffDashboard is a diff of an individual dashboard. This resource is always new.
type DiffDashboard struct {
Name string `json:"name"`
Desc string `json:"description"`
Charts []DiffChart `json:"charts"`
}
func newDiffDashboard(d *dashboard) DiffDashboard {
diff := DiffDashboard{
Name: d.Name(),
Desc: d.Description,
}
for _, c := range d.Charts {
diff.Charts = append(diff.Charts, DiffChart{
Properties: c.properties(),
Height: c.Height,
Width: c.Width,
})
}
return diff
}
// DiffChart is a diff of oa chart. Since all charts are new right now.
// the SummaryChart is reused here.
type DiffChart SummaryChart
// DiffLabelValues are the varying values for a label.
type DiffLabelValues struct {
Color string `json:"color"`
Description string `json:"description"`
}
// DiffLabel is a diff of an individual label.
type DiffLabel struct {
ID SafeID `json:"id"`
Name string `json:"name"`
New DiffLabelValues `json:"new"`
Old *DiffLabelValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
}
// IsNew indicates whether a pkg label is going to be new to the platform.
func (d DiffLabel) IsNew() bool {
return d.ID == SafeID(0)
}
func (d DiffLabel) hasConflict() bool {
return d.IsNew() || d.Old != nil && *d.Old != d.New
}
func newDiffLabel(l *label, i *influxdb.Label) DiffLabel {
diff := DiffLabel{
Name: l.Name(),
New: DiffLabelValues{
Color: l.Color,
Description: l.Description,
},
}
if i != nil {
diff.ID = SafeID(i.ID)
diff.Old = &DiffLabelValues{
Color: i.Properties["color"],
Description: i.Properties["description"],
}
}
return diff
}
// DiffLabelMapping is a diff of an individual label mapping. A
// single resource may have multiple mappings to multiple labels.
// A label can have many mappings to other resources.
type DiffLabelMapping struct {
IsNew bool `json:"isNew"`
ResType influxdb.ResourceType `json:"resourceType"`
ResID SafeID `json:"resourceID"`
ResName string `json:"resourceName"`
LabelID SafeID `json:"labelID"`
LabelName string `json:"labelName"`
}
// DiffNotificationEndpointValues are the varying values for a notification endpoint.
type DiffNotificationEndpointValues struct {
influxdb.NotificationEndpoint
}
// UnmarshalJSON decodes the notification endpoint. This is necessary unfortunately.
func (d *DiffNotificationEndpointValues) UnmarshalJSON(b []byte) (err error) {
d.NotificationEndpoint, err = endpoint.UnmarshalJSON(b)
return
}
// DiffNotificationEndpoint is a diff of an individual notification endpoint.
type DiffNotificationEndpoint struct {
ID SafeID `json:"id"`
Name string `json:"name"`
New DiffNotificationEndpointValues `json:"new"`
Old *DiffNotificationEndpointValues `json:"old"`
}
func newDiffNotificationEndpoint(ne *notificationEndpoint, i influxdb.NotificationEndpoint) DiffNotificationEndpoint {
diff := DiffNotificationEndpoint{
Name: ne.Name(),
New: DiffNotificationEndpointValues{
NotificationEndpoint: ne.summarize().NotificationEndpoint,
},
}
if i != nil {
diff.ID = SafeID(i.GetID())
diff.Old = &DiffNotificationEndpointValues{
NotificationEndpoint: i,
}
}
return diff
}
// IsNew indicates if the resource will be new to the platform or if it edits
// an existing resource.
func (d DiffNotificationEndpoint) IsNew() bool {
return d.Old == nil
}
// DiffNotificationRule is a diff of an individual notification rule. This resource is always new.
type DiffNotificationRule struct {
Name string `json:"name"`
Description string `json:"description"`
// These 3 fields represent the relationship of the rule to the endpoint.
EndpointID SafeID `json:"endpointID"`
EndpointName string `json:"endpointName"`
EndpointType string `json:"endpointType"`
Every string `json:"every"`
Offset string `json:"offset"`
MessageTemplate string `json:"messageTemplate"`
Status influxdb.Status `json:"status"`
StatusRules []SummaryStatusRule `json:"statusRules"`
TagRules []SummaryTagRule `json:"tagRules"`
}
func newDiffNotificationRule(r *notificationRule, iEndpoint influxdb.NotificationEndpoint) DiffNotificationRule {
sum := DiffNotificationRule{
Name: r.Name(),
Description: r.description,
EndpointName: r.endpointName.String(),
Every: r.every.String(),
Offset: r.offset.String(),
MessageTemplate: r.msgTemplate,
Status: r.Status(),
StatusRules: toSummaryStatusRules(r.statusRules),
TagRules: toSummaryTagRules(r.tagRules),
}
if iEndpoint != nil {
sum.EndpointID = SafeID(iEndpoint.GetID())
sum.EndpointType = iEndpoint.Type()
}
return sum
}
// DiffTask is a diff of an individual task. This resource is always new.
type DiffTask struct {
Name string `json:"name"`
Cron string `json:"cron"`
Description string `json:"description"`
Every string `json:"every"`
Offset string `json:"offset"`
Query string `json:"query"`
Status influxdb.Status `json:"status"`
}
func newDiffTask(t *task) DiffTask {
return DiffTask{
Name: t.Name(),
Cron: t.cron,
Description: t.description,
Every: durToStr(t.every),
Offset: durToStr(t.offset),
Query: t.query,
Status: t.Status(),
}
}
// DiffTelegraf is a diff of an individual telegraf. This resource is always new.
type DiffTelegraf struct {
influxdb.TelegrafConfig
}
func newDiffTelegraf(t *telegraf) DiffTelegraf {
return DiffTelegraf{
TelegrafConfig: t.config,
}
}
// DiffVariableValues are the varying values for a variable.
type DiffVariableValues struct {
Description string `json:"description"`
Args *influxdb.VariableArguments `json:"args"`
}
// DiffVariable is a diff of an individual variable.
type DiffVariable struct {
ID SafeID `json:"id"`
Name string `json:"name"`
New DiffVariableValues `json:"new"`
Old *DiffVariableValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
}
func newDiffVariable(v *variable, iv *influxdb.Variable) DiffVariable {
diff := DiffVariable{
Name: v.Name(),
New: DiffVariableValues{
Description: v.Description,
Args: v.influxVarArgs(),
},
}
if iv != nil {
diff.ID = SafeID(iv.ID)
diff.Old = &DiffVariableValues{
Description: iv.Description,
Args: iv.Arguments,
}
}
return diff
}
// IsNew indicates whether a pkg variable is going to be new to the platform.
func (d DiffVariable) IsNew() bool {
return d.ID == SafeID(0)
}
func (d DiffVariable) hasConflict() bool {
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
}
// Summary is a definition of all the resources that have or
// will be created from a pkg.
type Summary struct {
Buckets []SummaryBucket `json:"buckets"`
Checks []SummaryCheck `json:"checks"`
Dashboards []SummaryDashboard `json:"dashboards"`
NotificationEndpoints []SummaryNotificationEndpoint `json:"notificationEndpoints"`
NotificationRules []SummaryNotificationRule `json:"notificationRules"`
Labels []SummaryLabel `json:"labels"`
LabelMappings []SummaryLabelMapping `json:"labelMappings"`
MissingEnvs []string `json:"missingEnvRefs"`
MissingSecrets []string `json:"missingSecrets"`
Tasks []SummaryTask `json:"summaryTask"`
TelegrafConfigs []SummaryTelegraf `json:"telegrafConfigs"`
Variables []SummaryVariable `json:"variables"`
}
// SummaryBucket provides a summary of a pkg bucket.
type SummaryBucket struct {
ID SafeID `json:"id,omitempty"`
OrgID SafeID `json:"orgID,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
// TODO: return retention rules?
RetentionPeriod time.Duration `json:"retentionPeriod"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
// SummaryCheck provides a summary of a pkg check.
type SummaryCheck struct {
Check influxdb.Check `json:"check"`
Status influxdb.Status `json:"status"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
func (s *SummaryCheck) UnmarshalJSON(b []byte) error {
var out struct {
Status string `json:"status"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
Check json.RawMessage `json:"check"`
}
if err := json.Unmarshal(b, &out); err != nil {
return err
}
s.Status = influxdb.Status(out.Status)
s.LabelAssociations = out.LabelAssociations
var err error
s.Check, err = icheck.UnmarshalJSON(out.Check)
return err
}
// SummaryDashboard provides a summary of a pkg dashboard.
type SummaryDashboard struct {
ID SafeID `json:"id"`
OrgID SafeID `json:"orgID"`
Name string `json:"name"`
Description string `json:"description"`
Charts []SummaryChart `json:"charts"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
// chartKind identifies what kind of chart is eluded too. Each
// chart kind has their own requirements for what constitutes
// a chart.
type chartKind string
// available chart kinds
const (
chartKindUnknown chartKind = ""
chartKindGauge chartKind = "gauge"
chartKindHeatMap chartKind = "heatmap"
chartKindHistogram chartKind = "histogram"
chartKindMarkdown chartKind = "markdown"
chartKindScatter chartKind = "scatter"
chartKindSingleStat chartKind = "single_stat"
chartKindSingleStatPlusLine chartKind = "single_stat_plus_line"
chartKindXY chartKind = "xy"
)
func (c chartKind) ok() bool {
switch c {
case chartKindGauge, chartKindHeatMap, chartKindHistogram,
chartKindMarkdown, chartKindScatter, chartKindSingleStat,
chartKindSingleStatPlusLine, chartKindXY:
return true
default:
return false
}
}
func (c chartKind) title() string {
spacedKind := strings.ReplaceAll(string(c), "_", " ")
return strings.ReplaceAll(strings.Title(spacedKind), " ", "_")
}
// SummaryChart provides a summary of a pkg dashboard's chart.
type SummaryChart struct {
Properties influxdb.ViewProperties `json:"-"`
XPosition int `json:"xPos"`
YPosition int `json:"yPos"`
Height int `json:"height"`
Width int `json:"width"`
}
// MarshalJSON marshals a summary chart.
func (s *SummaryChart) MarshalJSON() ([]byte, error) {
b, err := influxdb.MarshalViewPropertiesJSON(s.Properties)
if err != nil {
return nil, err
}
type alias SummaryChart
out := struct {
Props json.RawMessage `json:"properties"`
alias
}{
Props: b,
alias: alias(*s),
}
return json.Marshal(out)
}
// UnmarshalJSON unmarshals a view properities and other data.
func (s *SummaryChart) UnmarshalJSON(b []byte) error {
type alias SummaryChart
a := (*alias)(s)
if err := json.Unmarshal(b, a); err != nil {
return err
}
s.XPosition = a.XPosition
s.XPosition = a.YPosition
s.Height = a.Height
s.Width = a.Width
vp, err := influxdb.UnmarshalViewPropertiesJSON(b)
if err != nil {
return err
}
s.Properties = vp
return nil
}
// SummaryNotificationEndpoint provides a summary of a pkg notification endpoint.
type SummaryNotificationEndpoint struct {
NotificationEndpoint influxdb.NotificationEndpoint `json:"notificationEndpoint"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
// UnmarshalJSON unmarshals the notificatio endpoint. This is necessary b/c of
// the notification endpoint does not have a means ot unmarshal itself.
func (s *SummaryNotificationEndpoint) UnmarshalJSON(b []byte) error {
var a struct {
NotificationEndpoint json.RawMessage `json:"notificationEndpoint"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
if err := json.Unmarshal(b, &a); err != nil {
return err
}
s.LabelAssociations = a.LabelAssociations
e, err := endpoint.UnmarshalJSON(a.NotificationEndpoint)
s.NotificationEndpoint = e
return err
}
// Summary types for NotificationRules which provide a summary of a pkg notification rule.
type (
SummaryNotificationRule struct {
ID SafeID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
// These 3 fields represent the relationship of the rule to the endpoint.
EndpointID SafeID `json:"endpointID"`
EndpointName string `json:"endpointName"`
EndpointType string `json:"endpointType"`
Every string `json:"every"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
Offset string `json:"offset"`
MessageTemplate string `json:"messageTemplate"`
Status influxdb.Status `json:"status"`
StatusRules []SummaryStatusRule `json:"statusRules"`
TagRules []SummaryTagRule `json:"tagRules"`
}
SummaryStatusRule struct {
CurrentLevel string `json:"currentLevel"`
PreviousLevel string `json:"previousLevel"`
}
SummaryTagRule struct {
Key string `json:"key"`
Value string `json:"value"`
Operator string `json:"operator"`
}
)
// SummaryLabel provides a summary of a pkg label.
type SummaryLabel struct {
ID SafeID `json:"id"`
OrgID SafeID `json:"orgID"`
Name string `json:"name"`
Properties struct {
Color string `json:"color"`
Description string `json:"description"`
} `json:"properties"`
}
// SummaryLabelMapping provides a summary of a label mapped with a single resource.
type SummaryLabelMapping struct {
exists bool
ResourceID SafeID `json:"resourceID"`
ResourceName string `json:"resourceName"`
ResourceType influxdb.ResourceType `json:"resourceType"`
LabelName string `json:"labelName"`
LabelID SafeID `json:"labelID"`
}
// SummaryTask provides a summary of a task.
type SummaryTask struct {
ID SafeID `json:"id"`
Name string `json:"name"`
Cron string `json:"cron"`
Description string `json:"description"`
Every string `json:"every"`
Offset string `json:"offset"`
Query string `json:"query"`
Status influxdb.Status `json:"status"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
// SummaryTelegraf provides a summary of a pkg telegraf config.
type SummaryTelegraf struct {
TelegrafConfig influxdb.TelegrafConfig `json:"telegrafConfig"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
// SummaryVariable provides a summary of a pkg variable.
type SummaryVariable struct {
ID SafeID `json:"id,omitempty"`
OrgID SafeID `json:"orgID,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Arguments *influxdb.VariableArguments `json:"arguments"`
LabelAssociations []SummaryLabel `json:"labelAssociations"`
}
const (
fieldAPIVersion = "apiVersion"
fieldAssociations = "associations"
fieldDescription = "description"
fieldEvery = "every"
fieldKey = "key"
fieldKind = "kind"
fieldLanguage = "language"
fieldLevel = "level"
fieldMin = "min"
fieldMax = "max"
fieldName = "name"
fieldOffset = "offset"
fieldOperator = "operator"
fieldPrefix = "prefix"
fieldQuery = "query"
fieldSuffix = "suffix"
fieldStatus = "status"
fieldType = "type"
fieldValue = "value"
fieldValues = "values"
)
const (
fieldBucketRetentionRules = "retentionRules"
)
type bucket struct {
id influxdb.ID
OrgID influxdb.ID
Description string
name *references
RetentionRules retentionRules
labels sortedLabels
// existing provides context for a resource that already
// exists in the platform. If a resource already exists
// then it will be referenced here.
existing *influxdb.Bucket
}
func (b *bucket) ID() influxdb.ID {
if b.existing != nil {
return b.existing.ID
}
return b.id
}
func (b *bucket) Labels() []*label {
return b.labels
}
func (b *bucket) Name() string {
return b.name.String()
}
func (b *bucket) ResourceType() influxdb.ResourceType {
return KindBucket.ResourceType()
}
func (b *bucket) Exists() bool {
return b.existing != nil
}
func (b *bucket) summarize() SummaryBucket {
return SummaryBucket{
ID: SafeID(b.ID()),
OrgID: SafeID(b.OrgID),
Name: b.Name(),
Description: b.Description,
RetentionPeriod: b.RetentionRules.RP(),
LabelAssociations: toSummaryLabels(b.labels...),
}
}
func (b *bucket) valid() []validationErr {
return b.RetentionRules.valid()
}
func (b *bucket) shouldApply() bool {
return b.existing == nil ||
b.Description != b.existing.Description ||
b.Name() != b.existing.Name ||
b.RetentionRules.RP() != b.existing.RetentionPeriod
}
type mapperBuckets []*bucket
func (b mapperBuckets) Association(i int) labelAssociater {
return b[i]
}
func (b mapperBuckets) Len() int {
return len(b)
}
const (
retentionRuleTypeExpire = "expire"
)
type retentionRule struct {
Type string `json:"type" yaml:"type"`
Seconds int `json:"everySeconds" yaml:"everySeconds"`
}
func newRetentionRule(d time.Duration) retentionRule {
return retentionRule{
Type: retentionRuleTypeExpire,
Seconds: int(d.Round(time.Second) / time.Second),
}
}
func (r retentionRule) valid() []validationErr {
const hour = 3600
var ff []validationErr
if r.Seconds < hour {
ff = append(ff, validationErr{
Field: fieldRetentionRulesEverySeconds,
Msg: "seconds must be a minimum of " + strconv.Itoa(hour),
})
}
if r.Type != retentionRuleTypeExpire {
ff = append(ff, validationErr{
Field: fieldType,
Msg: `type must be "expire"`,
})
}
return ff
}
const (
fieldRetentionRulesEverySeconds = "everySeconds"
)
type retentionRules []retentionRule
func (r retentionRules) RP() time.Duration {
// TODO: this feels very odd to me, will need to follow up with
// team to better understand this
for _, rule := range r {
return time.Duration(rule.Seconds) * time.Second
}
return 0
}
func (r retentionRules) valid() []validationErr {
var failures []validationErr
for i, rule := range r {
if ff := rule.valid(); len(ff) > 0 {
failures = append(failures, validationErr{
Field: fieldBucketRetentionRules,
Index: intPtr(i),
Nested: ff,
})
}
}
return failures
}
type checkKind int
const (
checkKindDeadman checkKind = iota + 1
checkKindThreshold
)
const (
fieldCheckAllValues = "allValues"
fieldCheckReportZero = "reportZero"
fieldCheckStaleTime = "staleTime"
fieldCheckStatusMessageTemplate = "statusMessageTemplate"
fieldCheckTags = "tags"
fieldCheckThresholds = "thresholds"
fieldCheckTimeSince = "timeSince"
)
type check struct {
id influxdb.ID
orgID influxdb.ID
kind checkKind
name *references
description string
every time.Duration
level string
offset time.Duration
query string
reportZero bool
staleTime time.Duration
status string
statusMessage string
tags []struct{ k, v string }
timeSince time.Duration
thresholds []threshold
labels sortedLabels
existing influxdb.Check
}
func (c *check) Exists() bool {
return c.existing != nil
}
func (c *check) ID() influxdb.ID {
if c.existing != nil {
return c.existing.GetID()
}
return c.id
}
func (c *check) Labels() []*label {
return c.labels
}
func (c *check) Name() string {
return c.name.String()
}
func (c *check) ResourceType() influxdb.ResourceType {
return KindCheck.ResourceType()
}
func (c *check) Status() influxdb.Status {
status := influxdb.Status(c.status)
if status == "" {
status = influxdb.Active
}
return status
}
func (c *check) summarize() SummaryCheck {
base := icheck.Base{
ID: c.ID(),
OrgID: c.orgID,
Name: c.Name(),
Description: c.description,
Every: toNotificationDuration(c.every),
Offset: toNotificationDuration(c.offset),