Skip to content

Commit

Permalink
new: add support listing nodebalancer firewalls (#436)
Browse files Browse the repository at this point in the history
## 📝 Description

**What does this PR do and why is this change necessary?**

Add support for `nodebalancers/{nb_id}/firewalls` endpoint

## ✔️ How to Test

**How do I run the relevant unit/integration tests?**

```bash
make ARGS='-run TestNodeBalancerFirewalls_List' fixtures
```
  • Loading branch information
jriddle-linode committed Dec 6, 2023
1 parent 2fe6f94 commit 1c6ad41
Show file tree
Hide file tree
Showing 3 changed files with 609 additions and 0 deletions.
41 changes: 41 additions & 0 deletions nodebalancer_firewalls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package linodego

import (
"context"
"fmt"

"github.com/go-resty/resty/v2"
)

// NodeBalancerFirewallsPagedResponse represents a Linode API response for listing of Cloud Firewalls
type NodeBalancerFirewallsPagedResponse struct {
*PageOptions
Data []Firewall `json:"data"`
}

func (NodeBalancerFirewallsPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("nodebalancers/%d/firewalls", id)
}

func (resp *NodeBalancerFirewallsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(NodeBalancerFirewallsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*NodeBalancerFirewallsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListNodeBalancerFirewalls returns a paginated list of Cloud Firewalls for nodebalancerID
func (c *Client) ListNodeBalancerFirewalls(ctx context.Context, nodebalancerID int, opts *ListOptions) ([]Firewall, error) {
response := NodeBalancerFirewallsPagedResponse{}

err := c.listHelper(ctx, &response, opts, nodebalancerID)
if err != nil {
return nil, err
}

return response.Data, nil
}
Loading

0 comments on commit 1c6ad41

Please sign in to comment.