firewall_devices.go raw
1 package linodego
2
3 import (
4 "context"
5 "encoding/json"
6 "time"
7
8 "github.com/linode/linodego/internal/parseabletime"
9 )
10
11 // FirewallDeviceType represents the different kinds of devices governable by a Firewall
12 type FirewallDeviceType string
13
14 // FirewallDeviceType constants start with FirewallDevice
15 const (
16 FirewallDeviceLinode FirewallDeviceType = "linode"
17 FirewallDeviceNodeBalancer FirewallDeviceType = "nodebalancer"
18 FirewallDeviceLinodeInterface FirewallDeviceType = "linode_interface"
19 )
20
21 // FirewallDevice represents a device governed by a Firewall
22 type FirewallDevice struct {
23 ID int `json:"id"`
24 Entity FirewallDeviceEntity `json:"entity"`
25 Created *time.Time `json:"-"`
26 Updated *time.Time `json:"-"`
27 }
28
29 // FirewallDeviceCreateOptions fields are those accepted by CreateFirewallDevice
30 type FirewallDeviceCreateOptions struct {
31 ID int `json:"id"`
32 Type FirewallDeviceType `json:"type"`
33 }
34
35 // UnmarshalJSON implements the json.Unmarshaler interface
36 func (device *FirewallDevice) UnmarshalJSON(b []byte) error {
37 type Mask FirewallDevice
38
39 p := struct {
40 *Mask
41
42 Created *parseabletime.ParseableTime `json:"created"`
43 Updated *parseabletime.ParseableTime `json:"updated"`
44 }{
45 Mask: (*Mask)(device),
46 }
47
48 if err := json.Unmarshal(b, &p); err != nil {
49 return err
50 }
51
52 device.Created = (*time.Time)(p.Created)
53 device.Updated = (*time.Time)(p.Updated)
54
55 return nil
56 }
57
58 // FirewallDeviceEntity contains information about a device associated with a Firewall
59 type FirewallDeviceEntity struct {
60 ID int `json:"id"`
61 Type FirewallDeviceType `json:"type"`
62 Label string `json:"label"`
63 URL string `json:"url"`
64 }
65
66 // ListFirewallDevices get devices associated with a given Firewall
67 func (c *Client) ListFirewallDevices(ctx context.Context, firewallID int, opts *ListOptions) ([]FirewallDevice, error) {
68 return getPaginatedResults[FirewallDevice](ctx, c, formatAPIPath("networking/firewalls/%d/devices", firewallID), opts)
69 }
70
71 // GetFirewallDevice gets a FirewallDevice given an ID
72 func (c *Client) GetFirewallDevice(ctx context.Context, firewallID, deviceID int) (*FirewallDevice, error) {
73 e := formatAPIPath("networking/firewalls/%d/devices/%d", firewallID, deviceID)
74 return doGETRequest[FirewallDevice](ctx, c, e)
75 }
76
77 // CreateFirewallDevice associates a Device with a given Firewall
78 func (c *Client) CreateFirewallDevice(ctx context.Context, firewallID int, opts FirewallDeviceCreateOptions) (*FirewallDevice, error) {
79 e := formatAPIPath("networking/firewalls/%d/devices", firewallID)
80 return doPOSTRequest[FirewallDevice](ctx, c, e, opts)
81 }
82
83 // DeleteFirewallDevice disassociates a Device with a given Firewall
84 func (c *Client) DeleteFirewallDevice(ctx context.Context, firewallID, deviceID int) error {
85 e := formatAPIPath("networking/firewalls/%d/devices/%d", firewallID, deviceID)
86 return doDELETERequest(ctx, c, e)
87 }
88