Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions test/unit/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package unit

import (
"context"
"encoding/json"
"fmt"
"strconv"
"testing"
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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))
}
Comment thread
yec-akamai marked this conversation as resolved.
}
Loading