Skip to content

Commit 6030012

Browse files
committed
Add GetPrefixListByName helper with test coverage
1 parent fa49abe commit 6030012

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

prefixlists.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package linodego
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"time"
78

89
"github.com/linode/linodego/internal/parseabletime"
@@ -59,3 +60,27 @@ func (c *Client) GetPrefixList(ctx context.Context, id int) (*PrefixList, error)
5960
endpoint := formatAPIPath("networking/prefixlists/%d", id)
6061
return doGETRequest[PrefixList](ctx, c, endpoint)
6162
}
63+
64+
// GetPrefixListByName finds a Prefix List by its name (e.g., "pl:system:object-storage:us-iad").
65+
// Returns nil and an error if no matching prefix list is found.
66+
func (c *Client) GetPrefixListByName(ctx context.Context, name string) (*PrefixList, error) {
67+
f := Filter{}
68+
f.AddField(Eq, "name", name)
69+
70+
fJSON, err := f.MarshalJSON()
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
opts := ListOptions{Filter: string(fJSON)}
76+
lists, err := c.ListPrefixLists(ctx, &opts)
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
if len(lists) == 0 {
82+
return nil, fmt.Errorf("prefix list with name %q not found", name)
83+
}
84+
85+
return &lists[0], nil
86+
}

test/unit/prefixlists_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,68 @@ func TestPrefixList_UnmarshalJSON(t *testing.T) {
119119
}
120120
assert.Nil(t, prefixList.Deleted)
121121
}
122+
123+
func TestPrefixLists_GetByName(t *testing.T) {
124+
var base ClientBaseCase
125+
base.SetUp(t)
126+
defer base.TearDown(t)
127+
128+
response := map[string]any{
129+
"data": []map[string]any{
130+
{
131+
"id": 999,
132+
"name": "pl:system:resolvers:us-iad:staging",
133+
"description": "Resolver ACL",
134+
"visibility": "restricted",
135+
"source_prefixlist_id": nil,
136+
"ipv4": []string{"139.144.192.62"},
137+
"ipv6": []string{"2600:3c05:e001:bc::1"},
138+
"version": 7,
139+
"created": "2021-01-01T00:00:00",
140+
"updated": "2021-06-01T00:00:00",
141+
"deleted": nil,
142+
},
143+
},
144+
"page": 1,
145+
"pages": 1,
146+
"results": 1,
147+
}
148+
149+
base.MockGet("networking/prefixlists", response)
150+
151+
pl, err := base.Client.GetPrefixListByName(context.Background(), "pl:system:resolvers:us-iad:staging")
152+
assert.NoError(t, err)
153+
assert.NotNil(t, pl)
154+
155+
assert.Equal(t, 999, pl.ID)
156+
assert.Equal(t, "pl:system:resolvers:us-iad:staging", pl.Name)
157+
assert.Equal(t, "Resolver ACL", pl.Description)
158+
assert.Equal(t, "restricted", pl.Visibility)
159+
assert.Equal(t, 7, pl.Version)
160+
if assert.NotNil(t, pl.IPv4) {
161+
assert.Equal(t, []string{"139.144.192.62"}, *pl.IPv4)
162+
}
163+
if assert.NotNil(t, pl.IPv6) {
164+
assert.Equal(t, []string{"2600:3c05:e001:bc::1"}, *pl.IPv6)
165+
}
166+
}
167+
168+
func TestPrefixLists_GetByName_NotFound(t *testing.T) {
169+
var base ClientBaseCase
170+
base.SetUp(t)
171+
defer base.TearDown(t)
172+
173+
response := map[string]any{
174+
"data": []map[string]any{},
175+
"page": 1,
176+
"pages": 1,
177+
"results": 0,
178+
}
179+
180+
base.MockGet("networking/prefixlists", response)
181+
182+
pl, err := base.Client.GetPrefixListByName(context.Background(), "pl:nonexistent")
183+
assert.Error(t, err)
184+
assert.Nil(t, pl)
185+
assert.Contains(t, err.Error(), "not found")
186+
}

0 commit comments

Comments
 (0)