-
Notifications
You must be signed in to change notification settings - Fork 81
/
firewall_devices.go
100 lines (82 loc) · 3.02 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
package linodego
import (
"context"
"encoding/json"
"time"
"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"`
}
// ListFirewallDevices get devices associated with a given Firewall
func (c *Client) ListFirewallDevices(ctx context.Context, firewallID int, opts *ListOptions) ([]FirewallDevice, error) {
response, err := getPaginatedResults[FirewallDevice](ctx, c, formatAPIPath("networking/firewalls/%d/devices", firewallID), opts)
if err != nil {
return nil, err
}
return response, nil
}
// GetFirewallDevice gets a FirewallDevice given an ID
func (c *Client) GetFirewallDevice(ctx context.Context, firewallID, deviceID int) (*FirewallDevice, error) {
e := formatAPIPath("networking/firewalls/%d/devices/%d", firewallID, deviceID)
response, err := doGETRequest[FirewallDevice](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// AddFirewallDevice associates a Device with a given Firewall
func (c *Client) CreateFirewallDevice(ctx context.Context, firewallID int, opts FirewallDeviceCreateOptions) (*FirewallDevice, error) {
e := formatAPIPath("networking/firewalls/%d/devices", firewallID)
response, err := doPOSTRequest[FirewallDevice](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// DeleteFirewallDevice disassociates a Device with a given Firewall
func (c *Client) DeleteFirewallDevice(ctx context.Context, firewallID, deviceID int) error {
e := formatAPIPath("networking/firewalls/%d/devices/%d", firewallID, deviceID)
err := doDELETERequest(ctx, c, e)
return err
}