Skip to content

Commit de3d0f9

Browse files
authored
TPT-3925: Use omitzero for ACLP alerts to support empty lists (#928)
* rm omitempty * use pointer * update unit test * misc * use omitzero * nit * add marshal empty test
1 parent 8fdc705 commit de3d0f9

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

instances.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ type InstanceAlert struct {
100100
NetworkIn int `json:"network_in"`
101101
NetworkOut int `json:"network_out"`
102102
TransferQuota int `json:"transfer_quota"`
103-
SystemAlerts []int `json:"system_alerts,omitempty"`
104-
UserAlerts []int `json:"user_alerts,omitempty"`
103+
SystemAlerts []int `json:"system_alerts,omitzero"`
104+
UserAlerts []int `json:"user_alerts,omitzero"`
105105
}
106106

107107
// InstanceBackup represents backup settings for an instance
@@ -230,8 +230,8 @@ type InstanceCreateOptions struct {
230230

231231
// InstanceACLPAlertsOptions represents ACLP alerts options for instance creation and cloning.
232232
type InstanceACLPAlertsOptions struct {
233-
SystemAlerts []int `json:"system_alerts,omitempty"`
234-
UserAlerts []int `json:"user_alerts,omitempty"`
233+
SystemAlerts []int `json:"system_alerts,omitzero"`
234+
UserAlerts []int `json:"user_alerts,omitzero"`
235235
}
236236

237237
// InstanceCreatePlacementGroupOptions represents the placement group

test/unit/instance_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package unit
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"strconv"
78
"testing"
@@ -44,7 +45,7 @@ func TestInstances_List(t *testing.T) {
4445
require.NotNil(t, linode.PlacementGroup.MigratingTo)
4546
assert.Equal(t, 2468, *linode.PlacementGroup.MigratingTo)
4647
assert.Equal(t, "linode/migrate", linode.MaintenancePolicy)
47-
if len(linode.Alerts.SystemAlerts) > 2 {
48+
if len(linode.Alerts.SystemAlerts) >= 2 {
4849
assert.Equal(t, 123, linode.Alerts.SystemAlerts[0])
4950
assert.Equal(t, 456, linode.Alerts.SystemAlerts[1])
5051
}
@@ -84,7 +85,7 @@ func TestInstance_Get(t *testing.T) {
8485
assert.Equal(t, "linode/migrate", instance.MaintenancePolicy)
8586
require.NotNil(t, instance.PlacementGroup.MigratingTo)
8687
assert.Equal(t, 2468, *instance.PlacementGroup.MigratingTo)
87-
if len(instance.Alerts.SystemAlerts) > 2 {
88+
if len(instance.Alerts.SystemAlerts) >= 2 {
8889
assert.Equal(t, 123, instance.Alerts.SystemAlerts[0])
8990
assert.Equal(t, 456, instance.Alerts.SystemAlerts[1])
9091
}
@@ -206,7 +207,7 @@ func TestInstance_Create(t *testing.T) {
206207
assert.NoError(t, err)
207208
assert.Equal(t, "new-instance", instance.Label)
208209
assert.Equal(t, "linode/migrate", instance.MaintenancePolicy)
209-
if len(instance.Alerts.SystemAlerts) > 2 {
210+
if len(instance.Alerts.SystemAlerts) >= 2 {
210211
assert.Equal(t, 123, instance.Alerts.SystemAlerts[0])
211212
assert.Equal(t, 456, instance.Alerts.SystemAlerts[1])
212213
}
@@ -238,7 +239,7 @@ func TestInstance_Update(t *testing.T) {
238239
assert.NoError(t, err)
239240
assert.Equal(t, "updated-instance", instance.Label)
240241
assert.Equal(t, "linode/power_off_on", instance.MaintenancePolicy)
241-
if len(instance.Alerts.SystemAlerts) > 2 {
242+
if len(instance.Alerts.SystemAlerts) >= 2 {
242243
assert.Equal(t, 123, instance.Alerts.SystemAlerts[0])
243244
assert.Equal(t, 456, instance.Alerts.SystemAlerts[1])
244245
}
@@ -304,7 +305,7 @@ func TestInstance_Clone(t *testing.T) {
304305
assert.NoError(t, err)
305306
assert.Equal(t, "cloned-instance", instance.Label)
306307
assert.Equal(t, "linode/migrate", instance.MaintenancePolicy)
307-
if len(instance.Alerts.SystemAlerts) > 2 {
308+
if len(instance.Alerts.SystemAlerts) >= 2 {
308309
assert.Equal(t, 123, instance.Alerts.SystemAlerts[0])
309310
assert.Equal(t, 456, instance.Alerts.SystemAlerts[1])
310311
}
@@ -359,3 +360,21 @@ func TestInstance_Rebuild(t *testing.T) {
359360
assert.NoError(t, err)
360361
assert.Equal(t, "linode/ubuntu22.04", instance.Image)
361362
}
363+
364+
func TestInstanceACLPAlerts_MarshalEmptyList(t *testing.T) {
365+
opts := linodego.InstanceACLPAlertsOptions{
366+
SystemAlerts: []int{},
367+
UserAlerts: []int{},
368+
}
369+
370+
buf, err := json.Marshal(opts)
371+
if err != nil {
372+
t.Fatalf("failed to marshal options: %v", err)
373+
}
374+
375+
// The goal of omitzero here is to ensure empty list is present
376+
expected := `{"system_alerts":[],"user_alerts":[]}`
377+
if string(buf) != expected {
378+
t.Errorf("expected JSON %s, got %s", expected, string(buf))
379+
}
380+
}

0 commit comments

Comments
 (0)