Skip to content

Commit

Permalink
new: add support listing instance firewalls (#435)
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 `instances/{linode_id}/firewalls` endpoint

## ✔️ How to Test

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

```bash
make ARGS='-run TestInstanceFirewalls_List' fixtures
```
  • Loading branch information
jriddle-linode committed Dec 7, 2023
1 parent d3474bd commit 532f589
Show file tree
Hide file tree
Showing 3 changed files with 638 additions and 0 deletions.
41 changes: 41 additions & 0 deletions instance_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"
)

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

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

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

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

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

return response.Data, nil
}
Loading

0 comments on commit 532f589

Please sign in to comment.