-
Notifications
You must be signed in to change notification settings - Fork 98
Adding support for PrefixList APIs #839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
komer3
wants to merge
5
commits into
linode:tmp-firewall-ruleset
Choose a base branch
from
komer3:adding-prefixList-support
base: tmp-firewall-ruleset
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−27
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3bd0ed
Adding support for PrefixList APIs
komer3 b73b76e
Merge branch 'main' into adding-prefixList-support
komer3 94ac0f5
gofmt
komer3 a2af4da
Adding func for endpoint (GET networking/firewalls/{fw_id}/rules/expa…
komer3 790dade
Merge branch 'tmp-firewall-ruleset' into adding-prefixList-support
komer3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"` | ||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| // 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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omitemptyis not necessary in a resource struct which won't be used in an API request body.