Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
61 changes: 61 additions & 0 deletions prefixlists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package linodego

import (
"context"
"encoding/json"
"time"

"github.com/linode/linodego/internal/parseabletime"
)

// PrefixList represents a network prefix list returned by the API.
type PrefixList struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Copy link
Member

@zliang-akamai zliang-akamai Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omitempty is not necessary in a resource struct which won't be used in an API request body.

Suggested change
Description string `json:"description,omitempty"`
Description string `json:"description"`

Visibility string `json:"visibility"`
SourcePrefixListID *int `json:"source_prefixlist_id"`
IPv4 *[]string `json:"ipv4"`
IPv6 *[]string `json:"ipv6"`
Version int `json:"version"`

Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Deleted *time.Time `json:"-"`
}
Comment on lines +12 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customer_id seems to be missed here?


// UnmarshalJSON implements custom timestamp parsing for PrefixList values.
func (p *PrefixList) UnmarshalJSON(data []byte) error {
type Mask PrefixList

aux := struct {
*Mask

Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
Deleted *parseabletime.ParseableTime `json:"deleted"`
}{
Mask: (*Mask)(p),
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

p.Created = (*time.Time)(aux.Created)
p.Updated = (*time.Time)(aux.Updated)
p.Deleted = (*time.Time)(aux.Deleted)

return nil
}

// ListPrefixLists returns a paginated collection of Prefix Lists.
func (c *Client) ListPrefixLists(ctx context.Context, opts *ListOptions) ([]PrefixList, error) {
return getPaginatedResults[PrefixList](ctx, c, "networking/prefixlists", opts)
}

// GetPrefixList fetches a single Prefix List by its ID.
func (c *Client) GetPrefixList(ctx context.Context, id int) (*PrefixList, error) {
endpoint := formatAPIPath("networking/prefixlists/%d", id)
return doGETRequest[PrefixList](ctx, c, endpoint)
}
121 changes: 121 additions & 0 deletions test/unit/prefixlists_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package unit

import (
"context"
"encoding/json"
"testing"
"time"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
)

func TestPrefixLists_List(t *testing.T) {
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

response := map[string]any{
"data": []map[string]any{
{
"id": 321,
"name": "pl:system:resolvers:us-iad:staging",
"description": "Resolver ACL",
"visibility": "restricted",
"source_prefixlist_id": nil,
"ipv4": []string{"139.144.192.62", "139.144.192.60"},
"ipv6": []string{"2600:3c05:e001:bc::1", "2600:3c05:e001:bc::2"},
"version": 4,
"created": "2018-01-01T00:01:01",
"updated": "2019-01-01T00:01:01",
"deleted": nil,
},
},
"page": 1,
"pages": 1,
"results": 1,
}

base.MockGet("networking/prefixlists", response)

prefixLists, err := base.Client.ListPrefixLists(context.Background(), nil)
assert.NoError(t, err)
assert.Len(t, prefixLists, 1)

pl := prefixLists[0]
assert.Equal(t, 321, pl.ID)
assert.Equal(t, "pl:system:resolvers:us-iad:staging", pl.Name)
assert.Equal(t, "restricted", pl.Visibility)
if assert.NotNil(t, pl.IPv4) {
assert.Equal(t, []string{"139.144.192.62", "139.144.192.60"}, *pl.IPv4)
}
if assert.NotNil(t, pl.IPv6) {
assert.Equal(t, []string{"2600:3c05:e001:bc::1", "2600:3c05:e001:bc::2"}, *pl.IPv6)
}
if assert.NotNil(t, pl.Created) {
assert.Equal(t, time.Date(2018, time.January, 1, 0, 1, 1, 0, time.UTC), pl.Created.UTC())
}
if assert.NotNil(t, pl.Updated) {
assert.Equal(t, time.Date(2019, time.January, 1, 0, 1, 1, 0, time.UTC), pl.Updated.UTC())
}
assert.Nil(t, pl.Deleted)
}

func TestPrefixLists_Get(t *testing.T) {
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

prefixListID := 654

response := map[string]any{
"id": prefixListID,
"name": "pl::customer:example",
"description": "Example customer list",
"visibility": "account",
"source_prefixlist_id": nil,
"ipv4": []string{"198.51.100.0/24"},
"ipv6": []string{"2001:db8::/32"},
"version": 2,
"created": "2020-02-02T02:02:02",
"updated": "2020-03-03T03:03:03",
"deleted": nil,
}

base.MockGet(formatMockAPIPath("networking/prefixlists/%d", prefixListID), response)

prefixList, err := base.Client.GetPrefixList(context.Background(), prefixListID)
assert.NoError(t, err)
assert.Equal(t, prefixListID, prefixList.ID)
if assert.NotNil(t, prefixList.Created) {
assert.Equal(t, time.Date(2020, time.February, 2, 2, 2, 2, 0, time.UTC), prefixList.Created.UTC())
}
if assert.NotNil(t, prefixList.Updated) {
assert.Equal(t, time.Date(2020, time.March, 3, 3, 3, 3, 0, time.UTC), prefixList.Updated.UTC())
}
}

func TestPrefixList_UnmarshalJSON(t *testing.T) {
var prefixList linodego.PrefixList

raw := []byte(`{
"id": 1,
"name": "pl:test",
"visibility": "restricted",
"ipv4": ["203.0.113.0/24"],
"ipv6": ["2001:db8::/64"],
"version": 5,
"created": "2017-05-05T05:05:05",
"updated": "2017-06-06T06:06:06",
"deleted": null
}`)

assert.NoError(t, json.Unmarshal(raw, &prefixList))
if assert.NotNil(t, prefixList.Created) {
assert.Equal(t, time.Date(2017, time.May, 5, 5, 5, 5, 0, time.UTC), prefixList.Created.UTC())
}
if assert.NotNil(t, prefixList.Updated) {
assert.Equal(t, time.Date(2017, time.June, 6, 6, 6, 6, 0, time.UTC), prefixList.Updated.UTC())
}
assert.Nil(t, prefixList.Deleted)
}
Loading