-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 📝 Description **What does this PR do and why is this change necessary?** Adding support for `regions/availability` and `regions/{region_id}/availability` endpoints. ## ✔️ How to Test **How do I run the relevant unit/integration tests?** `make ARGS='-run TestRegionsAvailability_List' fixtures`
- Loading branch information
1 parent
a966a25
commit 66e79a8
Showing
3 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package linodego | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
// Region represents a linode region object | ||
type RegionAvailability struct { | ||
Region string `json:"region"` | ||
Plan string `json:"plan"` | ||
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{} | ||
|
||
endpoint, err := generateListCacheURL(response.endpoint(), opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result := c.getCachedResponse(endpoint); result != nil { | ||
return result.([]RegionAvailability), nil | ||
} | ||
|
||
err = c.listHelper(ctx, &response, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime) | ||
|
||
return response.Data, 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)) | ||
|
||
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)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.addCachedResponse(e, r.Result(), &cacheExpiryTime) | ||
|
||
return r.Result().(*RegionAvailability), nil | ||
} |
Oops, something went wrong.