Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated regions_availability to vlans from resty to request helpers #551

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions paged_response_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type IPv6RangesPagedResponse legacyPagedResponse[IPv6Range]
// Deprecated: LinodeKernelsPagedResponse exists for historical compatibility and should not be used.
type LinodeKernelsPagedResponse legacyPagedResponse[LinodeKernel]

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

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

Expand Down Expand Up @@ -130,6 +133,21 @@ type ObjectStorageClustersPagedResponse legacyPagedResponse[ObjectStorageCluster
// Deprecated: PaymentsPagedResponse exists for historical compatibility and should not be used.
type PaymentsPagedResponse legacyPagedResponse[Payment]

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

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

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

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

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

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

Expand Down
44 changes: 9 additions & 35 deletions regions_availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package linodego

import (
"context"
"fmt"
"net/url"

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

// Region represents a linode region object
Expand All @@ -15,32 +11,11 @@ type RegionAvailability struct {
Available bool `json:"available"`
}

// RegionsAvailabilityPagedResponse represents a linode API response for listing
type RegionsAvailabilityPagedResponse struct {
*PageOptions
Data []RegionAvailability `json:"data"`
}

// endpoint gets the endpoint URL for Region
func (RegionsAvailabilityPagedResponse) endpoint(_ ...any) string {
return "regions/availability"
}

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

// ListRegionsAvailability lists Regions. This endpoint is cached by default.
func (c *Client) ListRegionsAvailability(ctx context.Context, opts *ListOptions) ([]RegionAvailability, error) {
response := RegionsAvailabilityPagedResponse{}
e := "regions/availability"

endpoint, err := generateListCacheURL(response.endpoint(), opts)
endpoint, err := generateListCacheURL(e, opts)
if err != nil {
return nil, err
}
Expand All @@ -49,32 +24,31 @@ func (c *Client) ListRegionsAvailability(ctx context.Context, opts *ListOptions)
return result.([]RegionAvailability), nil
}

err = c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[RegionAvailability](ctx, c, e, opts)
if err != nil {
return nil, err
}

c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)
c.addCachedResponse(endpoint, response, &cacheExpiryTime)

return response.Data, nil
return response, nil
}

// GetRegionAvailability gets the template with the provided ID. This endpoint is cached by default.
func (c *Client) GetRegionAvailability(ctx context.Context, regionID string) (*RegionAvailability, error) {
e := fmt.Sprintf("regions/%s/availability", url.PathEscape(regionID))
e := formatAPIPath("regions/%s/availability", regionID)

if result := c.getCachedResponse(e); result != nil {
result := result.(RegionAvailability)
return &result, nil
}

req := c.R(ctx).SetResult(&RegionAvailability{})
r, err := coupleAPIErrors(req.Get(e))
response, err := doGETRequest[RegionAvailability](ctx, c, e)
if err != nil {
return nil, err
}

c.addCachedResponse(e, r.Result(), &cacheExpiryTime)
c.addCachedResponse(e, response, &cacheExpiryTime)

return r.Result().(*RegionAvailability), nil
return response, nil
}
73 changes: 12 additions & 61 deletions stackscripts.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 @@ -111,83 +109,36 @@ func (i Stackscript) GetUpdateOptions() StackscriptUpdateOptions {
}
}

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

// endpoint gets the endpoint URL for Stackscript
func (StackscriptsPagedResponse) endpoint(_ ...any) string {
return "linode/stackscripts"
}

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

// ListStackscripts lists Stackscripts
func (c *Client) ListStackscripts(ctx context.Context, opts *ListOptions) ([]Stackscript, error) {
response := StackscriptsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
response, err := getPaginatedResults[Stackscript](ctx, c, "linode/stackscripts", opts)
return response, err
}

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

// CreateStackscript creates a StackScript
func (c *Client) CreateStackscript(ctx context.Context, opts StackscriptCreateOptions) (*Stackscript, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := "linode/stackscripts"
req := c.R(ctx).SetResult(&Stackscript{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Stackscript), nil
response, err := doPOSTRequest[Stackscript](ctx, c, e, opts)
return response, err
}

// UpdateStackscript updates the StackScript with the specified id
func (c *Client) UpdateStackscript(ctx context.Context, scriptID int, opts StackscriptUpdateOptions) (*Stackscript, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

req := c.R(ctx).SetResult(&Stackscript{}).SetBody(string(body))
e := fmt.Sprintf("linode/stackscripts/%d", scriptID)
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Stackscript), nil
e := formatAPIPath("linode/stackscripts/%d", scriptID)
response, err := doPUTRequest[Stackscript](ctx, c, e, opts)
return response, err
}

// DeleteStackscript deletes the StackScript with the specified id
func (c *Client) DeleteStackscript(ctx context.Context, scriptID int) error {
e := fmt.Sprintf("linode/stackscripts/%d", scriptID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("linode/stackscripts/%d", scriptID)
err := doDELETERequest(ctx, c, e)
return err
}
41 changes: 5 additions & 36 deletions support.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package linodego

import (
"context"
"fmt"
"time"

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

// Ticket represents a support ticket object
Expand Down Expand Up @@ -42,46 +39,18 @@ const (
TicketOpen TicketStatus = "open"
)

// TicketsPagedResponse represents a paginated ticket API response
type TicketsPagedResponse struct {
*PageOptions
Data []Ticket `json:"data"`
}

func (TicketsPagedResponse) endpoint(_ ...any) string {
return "support/tickets"
}

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

// ListTickets returns a collection of Support Tickets on the Account. Support Tickets
// can be both tickets opened with Linode for support, as well as tickets generated by
// Linode regarding the Account. This collection includes all Support Tickets generated
// on the Account, with open tickets returned first.
func (c *Client) ListTickets(ctx context.Context, opts *ListOptions) ([]Ticket, error) {
response := TicketsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
response, err := getPaginatedResults[Ticket](ctx, c, "support/tickets", opts)
return response, err
}

// GetTicket gets a Support Ticket on the Account with the specified ID
func (c *Client) GetTicket(ctx context.Context, ticketID int) (*Ticket, error) {
e := fmt.Sprintf("support/tickets/%d", ticketID)
req := c.R(ctx).SetResult(&Ticket{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Ticket), nil
e := formatAPIPath("support/tickets/%d", ticketID)
response, err := doGETRequest[Ticket](ctx, c, e)
return response, err
}
Loading