Skip to content

Commit 95e8cd4

Browse files
CLOUDP-303005: Allow Serverless Alerts to specify decimal thresholds by @tvdw-mongodb (#2160)
Allow Serverless Alerts to specify decimal thresholds by @tvdw-mongodb
1 parent 03bf858 commit 95e8cd4

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

api/v1/alert_configurations.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,16 @@ func (in *MetricThreshold) IsEqual(threshold *admin.ServerlessMetricThreshold) b
360360
if threshold == nil {
361361
return false
362362
}
363+
thresholdFloat, err := strconv.ParseFloat(in.Threshold, 64)
364+
if err != nil {
365+
// A parsing error means we're not equal, because the other object has a valid
366+
// value. The actual error will be returned when calling ToAtlas.
367+
return false
368+
}
369+
363370
return in.MetricName == threshold.GetMetricName() &&
364371
in.Operator == threshold.GetOperator() &&
365-
in.Threshold == strconv.FormatInt(int64(threshold.GetThreshold()), 10) &&
372+
thresholdFloat == threshold.GetThreshold() &&
366373
in.Units == threshold.GetUnits() &&
367374
in.Mode == threshold.GetMode()
368375
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package v1
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"go.mongodb.org/atlas-sdk/v20231115008/admin"
8+
9+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
10+
)
11+
12+
func TestServerlessMetricThreshold(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
akoData *MetricThreshold
16+
atlasData *admin.ServerlessMetricThreshold
17+
equal bool
18+
}{
19+
{
20+
name: "Should be able to parse float Theshold",
21+
atlasData: &admin.ServerlessMetricThreshold{
22+
MetricName: "test",
23+
Mode: pointer.MakePtr("test"),
24+
Operator: pointer.MakePtr("IN"),
25+
Threshold: pointer.MakePtr(3.14),
26+
Units: pointer.MakePtr("test"),
27+
},
28+
akoData: &MetricThreshold{
29+
MetricName: "test",
30+
Operator: "IN",
31+
Threshold: "3.14",
32+
Units: "test",
33+
Mode: "test",
34+
},
35+
equal: true,
36+
},
37+
{
38+
name: "Should be able to parse int Theshold",
39+
atlasData: &admin.ServerlessMetricThreshold{
40+
MetricName: "test",
41+
Mode: pointer.MakePtr("test"),
42+
Operator: pointer.MakePtr("IN"),
43+
Threshold: pointer.MakePtr[float64](3),
44+
Units: pointer.MakePtr("test"),
45+
},
46+
akoData: &MetricThreshold{
47+
MetricName: "test",
48+
Operator: "IN",
49+
Threshold: "3",
50+
Units: "test",
51+
Mode: "test",
52+
},
53+
equal: true,
54+
},
55+
{
56+
name: "Should be false if Theshold is not a number",
57+
atlasData: &admin.ServerlessMetricThreshold{
58+
MetricName: "test",
59+
Mode: pointer.MakePtr("test"),
60+
Operator: pointer.MakePtr("IN"),
61+
Threshold: pointer.MakePtr(3.14),
62+
Units: pointer.MakePtr("test"),
63+
},
64+
akoData: &MetricThreshold{
65+
MetricName: "test",
66+
Operator: "IN",
67+
Threshold: "13InvalidFloat",
68+
Units: "test",
69+
Mode: "test",
70+
},
71+
equal: false,
72+
},
73+
{
74+
name: "Should be false input is nil",
75+
atlasData: nil,
76+
akoData: &MetricThreshold{
77+
MetricName: "test",
78+
Operator: "IN",
79+
Threshold: "3.14",
80+
Units: "test",
81+
Mode: "test",
82+
},
83+
equal: false,
84+
},
85+
{
86+
name: "Should be false if operator mismatched",
87+
atlasData: &admin.ServerlessMetricThreshold{
88+
MetricName: "test",
89+
Mode: pointer.MakePtr("test"),
90+
Operator: pointer.MakePtr("IN"),
91+
Threshold: pointer.MakePtr(3.14),
92+
Units: pointer.MakePtr("test"),
93+
},
94+
akoData: &MetricThreshold{
95+
MetricName: "test",
96+
Operator: "LOWER",
97+
Threshold: "3.14",
98+
Units: "test",
99+
Mode: "test",
100+
},
101+
equal: false,
102+
},
103+
{
104+
name: "Should fail if Threshold mismatched",
105+
atlasData: &admin.ServerlessMetricThreshold{
106+
MetricName: "test",
107+
Mode: pointer.MakePtr("test"),
108+
Operator: pointer.MakePtr("IN"),
109+
Threshold: pointer.MakePtr(3.14),
110+
Units: pointer.MakePtr("test"),
111+
},
112+
akoData: &MetricThreshold{
113+
MetricName: "test",
114+
Operator: "IN",
115+
Threshold: "2.718",
116+
Units: "test",
117+
Mode: "test",
118+
},
119+
equal: false,
120+
},
121+
}
122+
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
require.Equal(t, tt.equal, tt.akoData.IsEqual(tt.atlasData))
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)