Skip to content

Commit

Permalink
Migrated firewall_rules to instance_firewalls to request helpers (#533)
Browse files Browse the repository at this point in the history
* Migrated firewall_rules to instance_firewalls to request helpers

* Reran fixtures and removed faulty test

* Added missing paged response structs

* Reran fixtures
  • Loading branch information
ezilber-akamai authored Jul 8, 2024
1 parent 72ef4eb commit 2a5ae92
Show file tree
Hide file tree
Showing 27 changed files with 833 additions and 1,591 deletions.
21 changes: 7 additions & 14 deletions firewall_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package linodego

import (
"context"
"encoding/json"
"fmt"
)

// NetworkProtocol enum type
Expand Down Expand Up @@ -43,27 +41,22 @@ type FirewallRuleSet struct {

// GetFirewallRules gets the FirewallRuleSet for the given Firewall.
func (c *Client) GetFirewallRules(ctx context.Context, firewallID int) (*FirewallRuleSet, error) {
e := fmt.Sprintf("networking/firewalls/%d/rules", firewallID)
req := c.R(ctx).SetResult(&FirewallRuleSet{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("networking/firewalls/%d/rules", firewallID)
response, err := doGETRequest[FirewallRuleSet](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*FirewallRuleSet), nil

return response, nil
}

// UpdateFirewallRules updates the FirewallRuleSet for the given Firewall
func (c *Client) UpdateFirewallRules(ctx context.Context, firewallID int, rules FirewallRuleSet) (*FirewallRuleSet, error) {
body, err := json.Marshal(rules)
e := formatAPIPath("networking/firewalls/%d/rules", firewallID)
response, err := doPUTRequest[FirewallRuleSet](ctx, c, e, rules)
if err != nil {
return nil, err
}

e := fmt.Sprintf("networking/firewalls/%d/rules", firewallID)
req := c.R(ctx).SetResult(&FirewallRuleSet{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*FirewallRuleSet), nil
return response, nil
}
61 changes: 12 additions & 49 deletions firewalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -82,87 +80,52 @@ func (f *Firewall) UnmarshalJSON(b []byte) error {
return nil
}

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

func (FirewallsPagedResponse) endpoint(_ ...any) string {
return "networking/firewalls"
}

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

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

err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[Firewall](ctx, c, "networking/firewalls", opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}

// CreateFirewall creates a single Firewall with at least one set of inbound or outbound rules
func (c *Client) CreateFirewall(ctx context.Context, opts FirewallCreateOptions) (*Firewall, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := "networking/firewalls"
req := c.R(ctx).SetResult(&Firewall{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
response, err := doPOSTRequest[Firewall](ctx, c, e, opts)
if err != nil {
return nil, err
}

return r.Result().(*Firewall), nil
return response, nil
}

// GetFirewall gets a single Firewall with the provided ID
func (c *Client) GetFirewall(ctx context.Context, firewallID int) (*Firewall, error) {
e := fmt.Sprintf("networking/firewalls/%d", firewallID)
req := c.R(ctx).SetResult(&Firewall{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("networking/firewalls/%d", firewallID)
response, err := doGETRequest[Firewall](ctx, c, e)
if err != nil {
return nil, err
}

return r.Result().(*Firewall), nil
return response, nil
}

// UpdateFirewall updates a Firewall with the given ID
func (c *Client) UpdateFirewall(ctx context.Context, firewallID int, opts FirewallUpdateOptions) (*Firewall, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := fmt.Sprintf("networking/firewalls/%d", firewallID)
req := c.R(ctx).SetResult(&Firewall{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
e := formatAPIPath("networking/firewalls/%d", firewallID)
response, err := doPUTRequest[Firewall](ctx, c, e, opts)
if err != nil {
return nil, err
}

return r.Result().(*Firewall), nil
return response, nil
}

// DeleteFirewall deletes a single Firewall with the provided ID
func (c *Client) DeleteFirewall(ctx context.Context, firewallID int) error {
e := fmt.Sprintf("networking/firewalls/%d", firewallID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("networking/firewalls/%d", firewallID)
err := doDELETERequest(ctx, c, e)
return err
}
57 changes: 19 additions & 38 deletions instance_config_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package linodego

import (
"context"
"encoding/json"
"fmt"
)

// InstanceConfigInterface contains information about a configuration's network interface
Expand Down Expand Up @@ -109,19 +107,13 @@ func (c *Client) AppendInstanceConfigInterface(
configID int,
opts InstanceConfigInterfaceCreateOptions,
) (*InstanceConfigInterface, error) {
body, err := json.Marshal(opts)
e := formatAPIPath("/linode/instances/%d/configs/%d/interfaces", linodeID, configID)
response, err := doPOSTRequest[InstanceConfigInterface](ctx, c, e, opts)
if err != nil {
return nil, err
}

req := c.R(ctx).SetResult(&InstanceConfigInterface{}).SetBody(string(body))
e := fmt.Sprintf("/linode/instances/%d/configs/%d/interfaces", linodeID, configID)
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}

return r.Result().(*InstanceConfigInterface), nil
return response, nil
}

func (c *Client) GetInstanceConfigInterface(
Expand All @@ -130,36 +122,36 @@ func (c *Client) GetInstanceConfigInterface(
configID int,
interfaceID int,
) (*InstanceConfigInterface, error) {
e := fmt.Sprintf(
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/%d",
linodeID,
configID,
interfaceID,
)
req := c.R(ctx).SetResult(&InstanceConfigInterface{})
r, err := coupleAPIErrors(req.Get(e))
response, err := doGETRequest[InstanceConfigInterface](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*InstanceConfigInterface), nil

return response, nil
}

func (c *Client) ListInstanceConfigInterfaces(
ctx context.Context,
linodeID int,
configID int,
) ([]InstanceConfigInterface, error) {
e := fmt.Sprintf(
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces",
linodeID,
configID,
)
req := c.R(ctx).SetResult([]InstanceConfigInterface{})
r, err := coupleAPIErrors(req.Get(e))
response, err := doGETRequest[[]InstanceConfigInterface](ctx, c, e)
if err != nil {
return nil, err
}
return *r.Result().(*[]InstanceConfigInterface), nil

return *response, nil
}

func (c *Client) UpdateInstanceConfigInterface(
Expand All @@ -169,23 +161,18 @@ func (c *Client) UpdateInstanceConfigInterface(
interfaceID int,
opts InstanceConfigInterfaceUpdateOptions,
) (*InstanceConfigInterface, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := fmt.Sprintf(
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/%d",
linodeID,
configID,
interfaceID,
)
req := c.R(ctx).SetResult(&InstanceConfigInterface{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
response, err := doPUTRequest[InstanceConfigInterface](ctx, c, e, opts)
if err != nil {
return nil, err
}
return r.Result().(*InstanceConfigInterface), nil

return response, nil
}

func (c *Client) DeleteInstanceConfigInterface(
Expand All @@ -194,13 +181,13 @@ func (c *Client) DeleteInstanceConfigInterface(
configID int,
interfaceID int,
) error {
e := fmt.Sprintf(
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/%d",
linodeID,
configID,
interfaceID,
)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
err := doDELETERequest(ctx, c, e)
return err
}

Expand All @@ -210,18 +197,12 @@ func (c *Client) ReorderInstanceConfigInterfaces(
configID int,
opts InstanceConfigInterfacesReorderOptions,
) error {
body, err := json.Marshal(opts)
if err != nil {
return err
}
e := fmt.Sprintf(
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/order",
linodeID,
configID,
)

req := c.R(ctx).SetBody(string(body))
_, err = coupleAPIErrors(req.Post(e))
_, err := doPOSTRequest[OAuthClient](ctx, c, e, opts)

return err
}
Loading

0 comments on commit 2a5ae92

Please sign in to comment.