Skip to content

Commit

Permalink
Migrated instance_ips to kernels to request helpers (#537)
Browse files Browse the repository at this point in the history
* Migrated instance_ips to kernels

* Reran fixtures

* Trigger workflows

* Reran more fixtures

* Fixed flaky test

* Added missing paged response structs
  • Loading branch information
ezilber-akamai authored Jul 5, 2024
1 parent a9a7dcf commit bd61260
Show file tree
Hide file tree
Showing 33 changed files with 4,174 additions and 3,855 deletions.
49 changes: 15 additions & 34 deletions instance_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package linodego

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

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

// GetInstanceIPAddresses gets the IPAddresses for a Linode instance
func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) {
e := fmt.Sprintf("linode/instances/%d/ips", linodeID)
req := c.R(ctx).SetResult(&InstanceIPAddressResponse{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("linode/instances/%d/ips", linodeID)
response, err := doGETRequest[InstanceIPAddressResponse](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*InstanceIPAddressResponse), nil

return response, nil
}

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

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

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

body, err := json.Marshal(instanceipRequest)
if err != nil {
return nil, err
}

e := fmt.Sprintf("linode/instances/%d/ips", linodeID)
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
e := formatAPIPath("linode/instances/%d/ips", linodeID)
response, err := doPOSTRequest[InstanceIP](ctx, c, e, instanceipRequest)
if err != nil {
return nil, err
}

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

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

ipAddress = url.PathEscape(ipAddress)

e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
return response, nil
}

func (c *Client) DeleteInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string) error {
ipAddress = url.PathEscape(ipAddress)
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipAddress)
err := doDELETERequest(ctx, c, e)
return err
}
46 changes: 19 additions & 27 deletions instance_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"time"

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

// GetInstanceSnapshot gets the snapshot with the provided ID
func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) {
e := fmt.Sprintf("linode/instances/%d/backups/%d", linodeID, snapshotID)
req := c.R(ctx).SetResult(&InstanceSnapshot{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("linode/instances/%d/backups/%d", linodeID, snapshotID)
response, err := doGETRequest[InstanceSnapshot](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*InstanceSnapshot), nil

return response, nil
}

// CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted.
func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) {
body, err := json.Marshal(map[string]string{"label": label})
if err != nil {
return nil, err
}
e := fmt.Sprintf("linode/instances/%d/backups", linodeID)
req := c.R(ctx).SetResult(&InstanceSnapshot{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
opts := map[string]string{"label": label}

e := formatAPIPath("linode/instances/%d/backups", linodeID)
response, err := doPOSTRequest[InstanceSnapshot](ctx, c, e, opts)
if err != nil {
return nil, err
}

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

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

return response, nil
}

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

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

// RestoreInstanceBackup Restores a Linode's Backup to the specified Linode.
func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error {
body, err := json.Marshal(opts)
if err != nil {
return NewError(err)
}
e := fmt.Sprintf("linode/instances/%d/backups/%d/restore", linodeID, backupID)
_, err = coupleAPIErrors(c.R(ctx).SetBody(string(body)).Post(e))
e := formatAPIPath("linode/instances/%d/backups/%d/restore", linodeID, backupID)
_, err := doPOSTRequest[InstanceBackup](ctx, c, e, opts)
return err
}
17 changes: 8 additions & 9 deletions instance_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package linodego

import (
"context"
"fmt"
)

// StatsNet represents a network stats object
Expand Down Expand Up @@ -35,22 +34,22 @@ type InstanceStats struct {

// GetInstanceStats gets the template with the provided ID
func (c *Client) GetInstanceStats(ctx context.Context, linodeID int) (*InstanceStats, error) {
e := fmt.Sprintf("linode/instances/%d/stats", linodeID)
req := c.R(ctx).SetResult(&InstanceStats{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("linode/instances/%d/stats", linodeID)
response, err := doGETRequest[InstanceStats](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*InstanceStats), nil

return response, nil
}

// GetInstanceStatsByDate gets the template with the provided ID, year, and month
func (c *Client) GetInstanceStatsByDate(ctx context.Context, linodeID int, year int, month int) (*InstanceStats, error) {
e := fmt.Sprintf("linode/instances/%d/stats/%d/%d", linodeID, year, month)
req := c.R(ctx).SetResult(&InstanceStats{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("linode/instances/%d/stats/%d/%d", linodeID, year, month)
response, err := doGETRequest[InstanceStats](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*InstanceStats), nil

return response, nil
}
31 changes: 3 additions & 28 deletions instance_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,14 @@ package linodego

import (
"context"
"fmt"

"github.com/go-resty/resty/v2"
)

// InstanceVolumesPagedResponse represents a paginated InstanceVolume API response
type InstanceVolumesPagedResponse struct {
*PageOptions
Data []Volume `json:"data"`
}

// endpoint gets the endpoint URL for InstanceVolume
func (InstanceVolumesPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("linode/instances/%d/volumes", id)
}

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

// ListInstanceVolumes lists InstanceVolumes
func (c *Client) ListInstanceVolumes(ctx context.Context, linodeID int, opts *ListOptions) ([]Volume, error) {
response := InstanceVolumesPagedResponse{}
err := c.listHelper(ctx, &response, opts, linodeID)
response, err := getPaginatedResults[Volume](ctx, c, formatAPIPath("linode/instances/%d/volumes", linodeID), opts)
if err != nil {
return nil, err
}
return response.Data, nil

return response, nil
}
Loading

0 comments on commit bd61260

Please sign in to comment.