forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall_devices.go
129 lines (110 loc) · 3.98 KB
/
firewall_devices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)
// FirewallDeviceType represents the different kinds of devices governable by a Firewall
type FirewallDeviceType string
// FirewallDeviceType constants start with FirewallDevice
const (
FirewallDeviceLinode FirewallDeviceType = "linode"
FirewallDeviceNodeBalancer FirewallDeviceType = "nodebalancer"
)
// FirewallDevice represents a device governed by a Firewall
type FirewallDevice struct {
ID int `json:"id"`
Entity FirewallDeviceEntity `json:"entity"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
// FirewallDeviceCreateOptions fields are those accepted by CreateFirewallDevice
type FirewallDeviceCreateOptions struct {
ID int `json:"id"`
Type FirewallDeviceType `json:"type"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (device *FirewallDevice) UnmarshalJSON(b []byte) error {
type Mask FirewallDevice
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(device),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
device.Created = (*time.Time)(p.Created)
device.Updated = (*time.Time)(p.Updated)
return nil
}
// FirewallDeviceEntity contains information about a device associated with a Firewall
type FirewallDeviceEntity struct {
ID int `json:"id"`
Type FirewallDeviceType `json:"type"`
Label string `json:"label"`
URL string `json:"url"`
}
// FirewallDevicesPagedResponse represents a Linode API response for FirewallDevices
type FirewallDevicesPagedResponse struct {
*PageOptions
Data []FirewallDevice `json:"data"`
}
// endpoint gets the endpoint URL for FirewallDevices of a given Firewall
func (FirewallDevicesPagedResponse) endpoint(ids ...any) string {
id, _ := ids[0].(int)
return fmt.Sprintf("networking/firewalls/%d/devices", id)
}
func (resp *FirewallDevicesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(FirewallDevicesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*FirewallDevicesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListFirewallDevices get devices associated with a given Firewall
func (c *Client) ListFirewallDevices(ctx context.Context, firewallID int, opts *ListOptions) ([]FirewallDevice, error) {
response := FirewallDevicesPagedResponse{}
err := c.listHelper(ctx, &response, opts, firewallID)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetFirewallDevice gets a FirewallDevice given an ID
func (c *Client) GetFirewallDevice(ctx context.Context, firewallID, deviceID int) (*FirewallDevice, error) {
e := fmt.Sprintf("networking/firewalls/%d/devices/%d", firewallID, deviceID)
req := c.R(ctx).SetResult(&FirewallDevice{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*FirewallDevice), nil
}
// AddFirewallDevice associates a Device with a given Firewall
func (c *Client) CreateFirewallDevice(ctx context.Context, firewallID int, opts FirewallDeviceCreateOptions) (*FirewallDevice, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("networking/firewalls/%d/devices", firewallID)
req := c.R(ctx).SetResult(&FirewallDevice{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*FirewallDevice), nil
}
// DeleteFirewallDevice disassociates a Device with a given Firewall
func (c *Client) DeleteFirewallDevice(ctx context.Context, firewallID, deviceID int) error {
e := fmt.Sprintf("networking/firewalls/%d/devices/%d", firewallID, deviceID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
}