Skip to content

Commit

Permalink
Migrated volumes to vpc_subnet from resty to request helpers (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezilber-akamai authored Jul 15, 2024
1 parent aa85919 commit 2b91f1c
Show file tree
Hide file tree
Showing 26 changed files with 4,277 additions and 3,112 deletions.
9 changes: 9 additions & 0 deletions paged_response_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ type PaymentsPagedResponse legacyPagedResponse[Payment]

// Deprecated: UsersPagedResponse exists for historical compatibility and should not be used.
type UsersPagedResponse legacyPagedResponse[User]

// Deprecated: VolumesPagedResponse exists for historical compatibility and should not be used.
type VolumesPagedResponse legacyPagedResponse[Volume]

// Deprecated: VPCsPagedResponse exists for historical compatibility and should not be used.
type VPCsPagedResponse legacyPagedResponse[VPC]

// Deprecated: VPCSubnetsPagedResponse exists for historical compatibility and should not be used.
type VPCSubnetsPagedResponse legacyPagedResponse[VPCSubnet]
418 changes: 256 additions & 162 deletions test/integration/fixtures/TestVPC_CreateGet.yaml

Large diffs are not rendered by default.

389 changes: 238 additions & 151 deletions test/integration/fixtures/TestVPC_Create_Invalid.yaml

Large diffs are not rendered by default.

418 changes: 256 additions & 162 deletions test/integration/fixtures/TestVPC_List.yaml

Large diffs are not rendered by default.

356 changes: 211 additions & 145 deletions test/integration/fixtures/TestVPC_ListAllIPAddresses.yaml

Large diffs are not rendered by default.

360 changes: 213 additions & 147 deletions test/integration/fixtures/TestVPC_ListIPAddresses.yaml

Large diffs are not rendered by default.

401 changes: 246 additions & 155 deletions test/integration/fixtures/TestVPC_Subnet_Create.yaml

Large diffs are not rendered by default.

419 changes: 257 additions & 162 deletions test/integration/fixtures/TestVPC_Subnet_Create_Invalid_data.yaml

Large diffs are not rendered by default.

416 changes: 255 additions & 161 deletions test/integration/fixtures/TestVPC_Subnet_List.yaml

Large diffs are not rendered by default.

417 changes: 256 additions & 161 deletions test/integration/fixtures/TestVPC_Subnet_Update.yaml

Large diffs are not rendered by default.

417 changes: 256 additions & 161 deletions test/integration/fixtures/TestVPC_Subnet_Update_Invalid_Label.yaml

Large diffs are not rendered by default.

546 changes: 332 additions & 214 deletions test/integration/fixtures/TestVPC_Subnet_WithInstance.yaml

Large diffs are not rendered by default.

417 changes: 256 additions & 161 deletions test/integration/fixtures/TestVPC_Update.yaml

Large diffs are not rendered by default.

417 changes: 256 additions & 161 deletions test/integration/fixtures/TestVPC_Update_Invalid.yaml

Large diffs are not rendered by default.

196 changes: 107 additions & 89 deletions test/integration/fixtures/TestVolume_Create.yaml

Large diffs are not rendered by default.

209 changes: 113 additions & 96 deletions test/integration/fixtures/TestVolume_Get.yaml

Large diffs are not rendered by default.

209 changes: 113 additions & 96 deletions test/integration/fixtures/TestVolume_List.yaml

Large diffs are not rendered by default.

224 changes: 120 additions & 104 deletions test/integration/fixtures/TestVolume_Resize.yaml

Large diffs are not rendered by default.

209 changes: 113 additions & 96 deletions test/integration/fixtures/TestVolume_Update.yaml

Large diffs are not rendered by default.

219 changes: 118 additions & 101 deletions test/integration/fixtures/TestVolume_WaitForLinodeID_linode.yaml

Large diffs are not rendered by default.

209 changes: 113 additions & 96 deletions test/integration/fixtures/TestVolume_WaitForLinodeID_nil.yaml

Large diffs are not rendered by default.

228 changes: 122 additions & 106 deletions test/integration/fixtures/TestVolume_WaitForLinodeID_volume.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ interactions:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/volumes/4210941
url: https://api.linode.com/v4beta/volumes/4979073
method: GET
response:
body: '{"id": 4210941, "status": "active", "label": "go-vol-test-def", "created":
body: '{"id": 4979073, "status": "active", "label": "go-vol-test-def", "created":
"2018-01-02T03:04:05", "updated": "2018-01-02T03:04:05", "filesystem_path":
"/dev/disk/by-id/scsi-0Linode_Volume_go-vol-test-def", "size": 20, "linode_id":
59542537, "linode_label": "go-test-ins-5647vkb87baw", "region": "ap-west", "tags":
61342082, "linode_label": "go-test-ins-8w21pl1h4n1x", "region": "ap-west", "tags":
[], "hardware_type": "nvme"}'
headers:
Access-Control-Allow-Credentials:
Expand All @@ -30,6 +30,8 @@ interactions:
- '*'
Access-Control-Expose-Headers:
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
Akamai-Internal-Account:
- '*'
Cache-Control:
- max-age=0, no-cache, no-store
Connection:
Expand All @@ -41,7 +43,7 @@ interactions:
Content-Type:
- application/json
Expires:
- Fri, 31 May 2024 17:35:46 GMT
- Thu, 11 Jul 2024 20:02:05 GMT
Pragma:
- no-cache
Strict-Transport-Security:
Expand All @@ -57,10 +59,7 @@ interactions:
- DENY
- DENY
X-Oauth-Scopes:
- account:read_write databases:read_write domains:read_write events:read_write
firewall:read_write images:read_write ips:read_write linodes:read_write lke:read_write
longview:read_write nodebalancers:read_write object_storage:read_write stackscripts:read_write
volumes:read_write vpc:read_write
- '*'
X-Ratelimit-Limit:
- "400"
X-Xss-Protection:
Expand Down
117 changes: 28 additions & 89 deletions volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -67,12 +65,6 @@ type VolumeAttachOptions struct {
PersistAcrossBoots *bool `json:"persist_across_boots,omitempty"`
}

// VolumesPagedResponse represents a linode API response for listing of volumes
type VolumesPagedResponse struct {
*PageOptions
Data []Volume `json:"data"`
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (v *Volume) UnmarshalJSON(b []byte) error {
type Mask Volume
Expand Down Expand Up @@ -114,125 +106,72 @@ func (v Volume) GetCreateOptions() (createOpts VolumeCreateOptions) {
return
}

// endpoint gets the endpoint URL for Volume
func (VolumesPagedResponse) endpoint(_ ...any) string {
return "volumes"
}

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

// ListVolumes lists Volumes
func (c *Client) ListVolumes(ctx context.Context, opts *ListOptions) ([]Volume, error) {
response := VolumesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
response, err := getPaginatedResults[Volume](ctx, c, "volumes", opts)
return response, err
}

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

// AttachVolume attaches a volume to a Linode instance
func (c *Client) AttachVolume(ctx context.Context, volumeID int, opts *VolumeAttachOptions) (*Volume, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := fmt.Sprintf("volumes/%d/attach", volumeID)
req := c.R(ctx).SetResult(&Volume{}).SetBody(string(body))
resp, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}

return resp.Result().(*Volume), nil
e := formatAPIPath("volumes/%d/attach", volumeID)
response, err := doPOSTRequest[Volume](ctx, c, e, opts)
return response, err
}

// CreateVolume creates a Linode Volume
func (c *Client) CreateVolume(ctx context.Context, opts VolumeCreateOptions) (*Volume, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := "volumes"
req := c.R(ctx).SetResult(&Volume{}).SetBody(string(body))
resp, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}

return resp.Result().(*Volume), nil
response, err := doPOSTRequest[Volume](ctx, c, e, opts)
return response, err
}

// UpdateVolume updates the Volume with the specified id
func (c *Client) UpdateVolume(ctx context.Context, volumeID int, opts VolumeUpdateOptions) (*Volume, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, NewError(err)
}

e := fmt.Sprintf("volumes/%d", volumeID)
req := c.R(ctx).SetResult(&Volume{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}

return r.Result().(*Volume), nil
e := formatAPIPath("volumes/%d", volumeID)
response, err := doPUTRequest[Volume](ctx, c, e, opts)
return response, err
}

// CloneVolume clones a Linode volume
func (c *Client) CloneVolume(ctx context.Context, volumeID int, label string) (*Volume, error) {
body := fmt.Sprintf("{\"label\":\"%s\"}", label)
e := fmt.Sprintf("volumes/%d/clone", volumeID)
req := c.R(ctx).SetResult(&Volume{}).SetBody(body)
resp, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
opts := map[string]any{
"label": label,
}

return resp.Result().(*Volume), nil
e := formatAPIPath("volumes/%d/clone", volumeID)
response, err := doPOSTRequest[Volume](ctx, c, e, opts)
return response, err
}

// DetachVolume detaches a Linode volume
func (c *Client) DetachVolume(ctx context.Context, volumeID int) error {
body := ""
e := fmt.Sprintf("volumes/%d/detach", volumeID)
_, err := coupleAPIErrors(c.R(ctx).SetBody(body).Post(e))
e := formatAPIPath("volumes/%d/detach", volumeID)
_, err := doPOSTRequest[Volume, any](ctx, c, e)
return err
}

// ResizeVolume resizes an instance to new Linode type
func (c *Client) ResizeVolume(ctx context.Context, volumeID int, size int) error {
body := fmt.Sprintf("{\"size\": %d}", size)
e := fmt.Sprintf("volumes/%d/resize", volumeID)
_, err := coupleAPIErrors(c.R(ctx).SetBody(body).Post(e))
opts := map[string]int{
"size": size,
}

e := formatAPIPath("volumes/%d/resize", volumeID)
_, err := doPOSTRequest[Volume](ctx, c, e, opts)
return err
}

// DeleteVolume deletes the Volume with the specified id
func (c *Client) DeleteVolume(ctx context.Context, volumeID int) error {
e := fmt.Sprintf("volumes/%d", volumeID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("volumes/%d", volumeID)
err := doDELETERequest(ctx, c, e)
return err
}
74 changes: 13 additions & 61 deletions vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand All @@ -32,25 +30,6 @@ type VPCUpdateOptions struct {
Description string `json:"description,omitempty"`
}

type VPCsPagedResponse struct {
*PageOptions
Data []VPC `json:"data"`
}

func (VPCsPagedResponse) endpoint(_ ...any) string {
return "vpcs"
}

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

func (v VPC) GetCreateOptions() VPCCreateOptions {
subnetCreations := make([]VPCSubnetCreateOptions, len(v.Subnets))
for i, s := range v.Subnets {
Expand Down Expand Up @@ -95,61 +74,34 @@ func (c *Client) CreateVPC(
ctx context.Context,
opts VPCCreateOptions,
) (*VPC, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

req := c.R(ctx).SetResult(&VPC{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post("vpcs"))
if err != nil {
return nil, err
}

return r.Result().(*VPC), nil
e := "vpcs"
response, err := doPOSTRequest[VPC](ctx, c, e, opts)
return response, err
}

func (c *Client) GetVPC(ctx context.Context, vpcID int) (*VPC, error) {
e := fmt.Sprintf("/vpcs/%d", vpcID)
req := c.R(ctx).SetResult(&VPC{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*VPC), nil
e := formatAPIPath("/vpcs/%d", vpcID)
response, err := doGETRequest[VPC](ctx, c, e)
return response, err
}

func (c *Client) ListVPCs(ctx context.Context, opts *ListOptions) ([]VPC, error) {
response := VPCsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
response, err := getPaginatedResults[VPC](ctx, c, "vpcs", opts)
return response, err
}

func (c *Client) UpdateVPC(
ctx context.Context,
vpcID int,
opts VPCUpdateOptions,
) (*VPC, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := fmt.Sprintf("vpcs/%d", vpcID)
req := c.R(ctx).SetResult(&VPC{}).SetBody(body)
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}

return r.Result().(*VPC), nil
e := formatAPIPath("vpcs/%d", vpcID)
response, err := doPUTRequest[VPC](ctx, c, e, opts)
return response, err
}

func (c *Client) DeleteVPC(ctx context.Context, vpcID int) error {
e := fmt.Sprintf("vpcs/%d", vpcID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("vpcs/%d", vpcID)
err := doDELETERequest(ctx, c, e)
return err
}
Loading

0 comments on commit 2b91f1c

Please sign in to comment.