Skip to content

Commit 768d1b0

Browse files
Merged main into TPT-2873_firewall_rules-instance_firewalls
2 parents 6df5518 + 1328865 commit 768d1b0

35 files changed

+3815
-2218
lines changed

instance_ips.go

+15-34
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package linodego
22

33
import (
44
"context"
5-
"encoding/json"
6-
"fmt"
7-
"net/url"
85
)
96

107
// InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance
@@ -94,26 +91,24 @@ const (
9491

9592
// GetInstanceIPAddresses gets the IPAddresses for a Linode instance
9693
func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) {
97-
e := fmt.Sprintf("linode/instances/%d/ips", linodeID)
98-
req := c.R(ctx).SetResult(&InstanceIPAddressResponse{})
99-
r, err := coupleAPIErrors(req.Get(e))
94+
e := formatAPIPath("linode/instances/%d/ips", linodeID)
95+
response, err := doGETRequest[InstanceIPAddressResponse](ctx, c, e)
10096
if err != nil {
10197
return nil, err
10298
}
103-
return r.Result().(*InstanceIPAddressResponse), nil
99+
100+
return response, nil
104101
}
105102

106103
// GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address
107104
func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) {
108-
ipaddress = url.PathEscape(ipaddress)
109-
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipaddress)
110-
req := c.R(ctx).SetResult(&InstanceIP{})
111-
r, err := coupleAPIErrors(req.Get(e))
105+
e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipaddress)
106+
response, err := doGETRequest[InstanceIP](ctx, c, e)
112107
if err != nil {
113108
return nil, err
114109
}
115110

116-
return r.Result().(*InstanceIP), nil
111+
return response, nil
117112
}
118113

119114
// AddInstanceIPAddress adds a public or private IP to a Linode instance
@@ -123,42 +118,28 @@ func (c *Client) AddInstanceIPAddress(ctx context.Context, linodeID int, public
123118
Public bool `json:"public"`
124119
}{"ipv4", public}
125120

126-
body, err := json.Marshal(instanceipRequest)
127-
if err != nil {
128-
return nil, err
129-
}
130-
131-
e := fmt.Sprintf("linode/instances/%d/ips", linodeID)
132-
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
133-
r, err := coupleAPIErrors(req.Post(e))
121+
e := formatAPIPath("linode/instances/%d/ips", linodeID)
122+
response, err := doPOSTRequest[InstanceIP](ctx, c, e, instanceipRequest)
134123
if err != nil {
135124
return nil, err
136125
}
137126

138-
return r.Result().(*InstanceIP), nil
127+
return response, nil
139128
}
140129

141130
// UpdateInstanceIPAddress updates the IPAddress with the specified instance id and IP address
142131
func (c *Client) UpdateInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string, opts IPAddressUpdateOptions) (*InstanceIP, error) {
143-
body, err := json.Marshal(opts)
132+
e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipAddress)
133+
response, err := doPUTRequest[InstanceIP](ctx, c, e, opts)
144134
if err != nil {
145135
return nil, err
146136
}
147137

148-
ipAddress = url.PathEscape(ipAddress)
149-
150-
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
151-
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
152-
r, err := coupleAPIErrors(req.Put(e))
153-
if err != nil {
154-
return nil, err
155-
}
156-
return r.Result().(*InstanceIP), nil
138+
return response, nil
157139
}
158140

159141
func (c *Client) DeleteInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string) error {
160-
ipAddress = url.PathEscape(ipAddress)
161-
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
162-
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
142+
e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipAddress)
143+
err := doDELETERequest(ctx, c, e)
163144
return err
164145
}

instance_snapshots.go

+19-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package linodego
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"time"
87

98
"github.com/linode/linodego/internal/parseabletime"
@@ -88,64 +87,57 @@ func (i *InstanceSnapshot) UnmarshalJSON(b []byte) error {
8887

8988
// GetInstanceSnapshot gets the snapshot with the provided ID
9089
func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) {
91-
e := fmt.Sprintf("linode/instances/%d/backups/%d", linodeID, snapshotID)
92-
req := c.R(ctx).SetResult(&InstanceSnapshot{})
93-
r, err := coupleAPIErrors(req.Get(e))
90+
e := formatAPIPath("linode/instances/%d/backups/%d", linodeID, snapshotID)
91+
response, err := doGETRequest[InstanceSnapshot](ctx, c, e)
9492
if err != nil {
9593
return nil, err
9694
}
97-
return r.Result().(*InstanceSnapshot), nil
95+
96+
return response, nil
9897
}
9998

10099
// CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted.
101100
func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) {
102-
body, err := json.Marshal(map[string]string{"label": label})
103-
if err != nil {
104-
return nil, err
105-
}
106-
e := fmt.Sprintf("linode/instances/%d/backups", linodeID)
107-
req := c.R(ctx).SetResult(&InstanceSnapshot{}).SetBody(string(body))
108-
r, err := coupleAPIErrors(req.Post(e))
101+
opts := map[string]string{"label": label}
102+
103+
e := formatAPIPath("linode/instances/%d/backups", linodeID)
104+
response, err := doPOSTRequest[InstanceSnapshot](ctx, c, e, opts)
109105
if err != nil {
110106
return nil, err
111107
}
112108

113-
return r.Result().(*InstanceSnapshot), nil
109+
return response, nil
114110
}
115111

116112
// GetInstanceBackups gets the Instance's available Backups.
117113
// This is not called ListInstanceBackups because a single object is returned, matching the API response.
118114
func (c *Client) GetInstanceBackups(ctx context.Context, linodeID int) (*InstanceBackupsResponse, error) {
119-
e := fmt.Sprintf("linode/instances/%d/backups", linodeID)
120-
req := c.R(ctx).SetResult(&InstanceBackupsResponse{})
121-
r, err := coupleAPIErrors(req.Get(e))
115+
e := formatAPIPath("linode/instances/%d/backups", linodeID)
116+
response, err := doGETRequest[InstanceBackupsResponse](ctx, c, e)
122117
if err != nil {
123118
return nil, err
124119
}
125-
return r.Result().(*InstanceBackupsResponse), nil
120+
121+
return response, nil
126122
}
127123

128124
// EnableInstanceBackups Enables backups for the specified Linode.
129125
func (c *Client) EnableInstanceBackups(ctx context.Context, linodeID int) error {
130-
e := fmt.Sprintf("linode/instances/%d/backups/enable", linodeID)
131-
_, err := coupleAPIErrors(c.R(ctx).Post(e))
126+
e := formatAPIPath("linode/instances/%d/backups/enable", linodeID)
127+
_, err := doPOSTRequest[InstanceBackup, any](ctx, c, e)
132128
return err
133129
}
134130

135131
// CancelInstanceBackups Cancels backups for the specified Linode.
136132
func (c *Client) CancelInstanceBackups(ctx context.Context, linodeID int) error {
137-
e := fmt.Sprintf("linode/instances/%d/backups/cancel", linodeID)
138-
_, err := coupleAPIErrors(c.R(ctx).Post(e))
133+
e := formatAPIPath("linode/instances/%d/backups/cancel", linodeID)
134+
_, err := doPOSTRequest[InstanceBackup, any](ctx, c, e)
139135
return err
140136
}
141137

142138
// RestoreInstanceBackup Restores a Linode's Backup to the specified Linode.
143139
func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error {
144-
body, err := json.Marshal(opts)
145-
if err != nil {
146-
return NewError(err)
147-
}
148-
e := fmt.Sprintf("linode/instances/%d/backups/%d/restore", linodeID, backupID)
149-
_, err = coupleAPIErrors(c.R(ctx).SetBody(string(body)).Post(e))
140+
e := formatAPIPath("linode/instances/%d/backups/%d/restore", linodeID, backupID)
141+
_, err := doPOSTRequest[InstanceBackup](ctx, c, e, opts)
150142
return err
151143
}

instance_stats.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package linodego
22

33
import (
44
"context"
5-
"fmt"
65
)
76

87
// StatsNet represents a network stats object
@@ -35,22 +34,22 @@ type InstanceStats struct {
3534

3635
// GetInstanceStats gets the template with the provided ID
3736
func (c *Client) GetInstanceStats(ctx context.Context, linodeID int) (*InstanceStats, error) {
38-
e := fmt.Sprintf("linode/instances/%d/stats", linodeID)
39-
req := c.R(ctx).SetResult(&InstanceStats{})
40-
r, err := coupleAPIErrors(req.Get(e))
37+
e := formatAPIPath("linode/instances/%d/stats", linodeID)
38+
response, err := doGETRequest[InstanceStats](ctx, c, e)
4139
if err != nil {
4240
return nil, err
4341
}
44-
return r.Result().(*InstanceStats), nil
42+
43+
return response, nil
4544
}
4645

4746
// GetInstanceStatsByDate gets the template with the provided ID, year, and month
4847
func (c *Client) GetInstanceStatsByDate(ctx context.Context, linodeID int, year int, month int) (*InstanceStats, error) {
49-
e := fmt.Sprintf("linode/instances/%d/stats/%d/%d", linodeID, year, month)
50-
req := c.R(ctx).SetResult(&InstanceStats{})
51-
r, err := coupleAPIErrors(req.Get(e))
48+
e := formatAPIPath("linode/instances/%d/stats/%d/%d", linodeID, year, month)
49+
response, err := doGETRequest[InstanceStats](ctx, c, e)
5250
if err != nil {
5351
return nil, err
5452
}
55-
return r.Result().(*InstanceStats), nil
53+
54+
return response, nil
5655
}

instance_volumes.go

+3-28
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,14 @@ package linodego
22

33
import (
44
"context"
5-
"fmt"
6-
7-
"github.com/go-resty/resty/v2"
85
)
96

10-
// InstanceVolumesPagedResponse represents a paginated InstanceVolume API response
11-
type InstanceVolumesPagedResponse struct {
12-
*PageOptions
13-
Data []Volume `json:"data"`
14-
}
15-
16-
// endpoint gets the endpoint URL for InstanceVolume
17-
func (InstanceVolumesPagedResponse) endpoint(ids ...any) string {
18-
id := ids[0].(int)
19-
return fmt.Sprintf("linode/instances/%d/volumes", id)
20-
}
21-
22-
func (resp *InstanceVolumesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
23-
res, err := coupleAPIErrors(r.SetResult(InstanceVolumesPagedResponse{}).Get(e))
24-
if err != nil {
25-
return 0, 0, err
26-
}
27-
castedRes := res.Result().(*InstanceVolumesPagedResponse)
28-
resp.Data = append(resp.Data, castedRes.Data...)
29-
return castedRes.Pages, castedRes.Results, nil
30-
}
31-
327
// ListInstanceVolumes lists InstanceVolumes
338
func (c *Client) ListInstanceVolumes(ctx context.Context, linodeID int, opts *ListOptions) ([]Volume, error) {
34-
response := InstanceVolumesPagedResponse{}
35-
err := c.listHelper(ctx, &response, opts, linodeID)
9+
response, err := getPaginatedResults[Volume](ctx, c, formatAPIPath("linode/instances/%d/volumes", linodeID), opts)
3610
if err != nil {
3711
return nil, err
3812
}
39-
return response.Data, nil
13+
14+
return response, nil
4015
}

0 commit comments

Comments
 (0)