diff --git a/instances.go b/instances.go index f6def2c62..d309d5f8c 100644 --- a/instances.go +++ b/instances.go @@ -100,8 +100,8 @@ type InstanceAlert struct { NetworkIn int `json:"network_in"` NetworkOut int `json:"network_out"` TransferQuota int `json:"transfer_quota"` - SystemAlerts []int `json:"system_alerts,omitempty"` - UserAlerts []int `json:"user_alerts,omitempty"` + SystemAlerts []int `json:"system_alerts,omitzero"` + UserAlerts []int `json:"user_alerts,omitzero"` } // InstanceBackup represents backup settings for an instance @@ -230,8 +230,8 @@ type InstanceCreateOptions struct { // InstanceACLPAlertsOptions represents ACLP alerts options for instance creation and cloning. type InstanceACLPAlertsOptions struct { - SystemAlerts []int `json:"system_alerts,omitempty"` - UserAlerts []int `json:"user_alerts,omitempty"` + SystemAlerts []int `json:"system_alerts,omitzero"` + UserAlerts []int `json:"user_alerts,omitzero"` } // InstanceCreatePlacementGroupOptions represents the placement group diff --git a/test/unit/instance_test.go b/test/unit/instance_test.go index f4e12eca7..0017aac44 100644 --- a/test/unit/instance_test.go +++ b/test/unit/instance_test.go @@ -2,6 +2,7 @@ package unit import ( "context" + "encoding/json" "fmt" "strconv" "testing" @@ -44,7 +45,7 @@ func TestInstances_List(t *testing.T) { require.NotNil(t, linode.PlacementGroup.MigratingTo) assert.Equal(t, 2468, *linode.PlacementGroup.MigratingTo) assert.Equal(t, "linode/migrate", linode.MaintenancePolicy) - if len(linode.Alerts.SystemAlerts) > 2 { + if len(linode.Alerts.SystemAlerts) >= 2 { assert.Equal(t, 123, linode.Alerts.SystemAlerts[0]) assert.Equal(t, 456, linode.Alerts.SystemAlerts[1]) } @@ -84,7 +85,7 @@ func TestInstance_Get(t *testing.T) { assert.Equal(t, "linode/migrate", instance.MaintenancePolicy) require.NotNil(t, instance.PlacementGroup.MigratingTo) assert.Equal(t, 2468, *instance.PlacementGroup.MigratingTo) - if len(instance.Alerts.SystemAlerts) > 2 { + if len(instance.Alerts.SystemAlerts) >= 2 { assert.Equal(t, 123, instance.Alerts.SystemAlerts[0]) assert.Equal(t, 456, instance.Alerts.SystemAlerts[1]) } @@ -206,7 +207,7 @@ func TestInstance_Create(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "new-instance", instance.Label) assert.Equal(t, "linode/migrate", instance.MaintenancePolicy) - if len(instance.Alerts.SystemAlerts) > 2 { + if len(instance.Alerts.SystemAlerts) >= 2 { assert.Equal(t, 123, instance.Alerts.SystemAlerts[0]) assert.Equal(t, 456, instance.Alerts.SystemAlerts[1]) } @@ -238,7 +239,7 @@ func TestInstance_Update(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "updated-instance", instance.Label) assert.Equal(t, "linode/power_off_on", instance.MaintenancePolicy) - if len(instance.Alerts.SystemAlerts) > 2 { + if len(instance.Alerts.SystemAlerts) >= 2 { assert.Equal(t, 123, instance.Alerts.SystemAlerts[0]) assert.Equal(t, 456, instance.Alerts.SystemAlerts[1]) } @@ -304,7 +305,7 @@ func TestInstance_Clone(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "cloned-instance", instance.Label) assert.Equal(t, "linode/migrate", instance.MaintenancePolicy) - if len(instance.Alerts.SystemAlerts) > 2 { + if len(instance.Alerts.SystemAlerts) >= 2 { assert.Equal(t, 123, instance.Alerts.SystemAlerts[0]) assert.Equal(t, 456, instance.Alerts.SystemAlerts[1]) } @@ -359,3 +360,21 @@ func TestInstance_Rebuild(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "linode/ubuntu22.04", instance.Image) } + +func TestInstanceACLPAlerts_MarshalEmptyList(t *testing.T) { + opts := linodego.InstanceACLPAlertsOptions{ + SystemAlerts: []int{}, + UserAlerts: []int{}, + } + + buf, err := json.Marshal(opts) + if err != nil { + t.Fatalf("failed to marshal options: %v", err) + } + + // The goal of omitzero here is to ensure empty list is present + expected := `{"system_alerts":[],"user_alerts":[]}` + if string(buf) != expected { + t.Errorf("expected JSON %s, got %s", expected, string(buf)) + } +}