Skip to content

Commit 5119842

Browse files
authored
new: Support account_availability (#434) (#444)
* support account_availability * fix lint * fix client test * update dc to region
1 parent 8958f9b commit 5119842

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

account_availability.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package linodego
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/go-resty/resty/v2"
9+
)
10+
11+
// AccountAvailability returns the resources information in a region which are NOT available to an account.
12+
type AccountAvailability struct {
13+
// region id
14+
Region string `json:"region"`
15+
16+
// the unavailable resources in a region to the customer
17+
Unavailable []string `json:"unavailable"`
18+
}
19+
20+
// AccountAvailabilityPagedResponse represents a paginated Account Availability API response
21+
type AccountAvailabilityPagedResponse struct {
22+
*PageOptions
23+
Data []AccountAvailability `json:"data"`
24+
}
25+
26+
// endpoint gets the endpoint URL for AccountAvailability
27+
func (AccountAvailabilityPagedResponse) endpoint(_ ...any) string {
28+
return "/account/availability"
29+
}
30+
31+
func (resp *AccountAvailabilityPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
32+
res, err := coupleAPIErrors(r.SetResult(AccountAvailabilityPagedResponse{}).Get(e))
33+
if err != nil {
34+
return 0, 0, err
35+
}
36+
castedRes := res.Result().(*AccountAvailabilityPagedResponse)
37+
resp.Data = append(resp.Data, castedRes.Data...)
38+
return castedRes.Pages, castedRes.Results, nil
39+
}
40+
41+
// ListAccountAvailabilities lists all available regions and the resources which are NOT available to the account.
42+
func (c *Client) ListAccountAvailabilities(ctx context.Context, opts *ListOptions) ([]AccountAvailability, error) {
43+
response := AccountAvailabilityPagedResponse{}
44+
err := c.listHelper(ctx, &response, opts)
45+
if err != nil {
46+
return nil, err
47+
}
48+
return response.Data, nil
49+
}
50+
51+
// GetAccountAvailability gets the unavailable resources in a region to the customer.
52+
func (c *Client) GetAccountAvailability(ctx context.Context, regionID string) (*AccountAvailability, error) {
53+
req := c.R(ctx).SetResult(&AccountAvailability{})
54+
regionID = url.PathEscape(regionID)
55+
b := fmt.Sprintf("account/availability/%s", regionID)
56+
r, err := coupleAPIErrors(req.Get(b))
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return r.Result().(*AccountAvailability), nil
62+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package integration
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/linode/linodego"
8+
)
9+
10+
func TestAccountAvailability_List(t *testing.T) {
11+
client, teardown := createTestClient(t, "fixtures/TestAccountAvailability_List")
12+
defer teardown()
13+
14+
availabilities, err := client.ListAccountAvailabilities(context.Background(), &linodego.ListOptions{})
15+
if err != nil {
16+
t.Errorf("Error getting Account Availabilities, expected struct, got error %v", err)
17+
}
18+
19+
if len(availabilities) == 0 {
20+
t.Errorf("Expected to see account availabilities returned.")
21+
}
22+
}
23+
24+
func TestAccountAvailability_Get(t *testing.T) {
25+
client, teardown := createTestClient(t, "fixtures/TestAccountAvailability_Get")
26+
defer teardown()
27+
28+
regionID := "us-east"
29+
availability, err := client.GetAccountAvailability(context.Background(), regionID)
30+
31+
if err != nil {
32+
t.Errorf("Error getting Account Availability, expected struct, got error %v", err)
33+
}
34+
35+
if availability.Region != regionID {
36+
t.Errorf("expected region ID to be %s; got %s", regionID, availability.Region)
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: ""
6+
form: {}
7+
headers:
8+
Accept:
9+
- application/json
10+
Content-Type:
11+
- application/json
12+
User-Agent:
13+
- linodego/dev https://github.com/linode/linodego
14+
url: https://api.linode.com/v4beta/account/availability/us-east
15+
method: GET
16+
response:
17+
body: '{"region": "us-east", "unavailable": []}'
18+
headers:
19+
Access-Control-Allow-Credentials:
20+
- "true"
21+
Access-Control-Allow-Headers:
22+
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
23+
Access-Control-Allow-Methods:
24+
- HEAD, GET, OPTIONS, POST, PUT, DELETE
25+
Access-Control-Allow-Origin:
26+
- '*'
27+
Access-Control-Expose-Headers:
28+
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
29+
Cache-Control:
30+
- private, max-age=0, s-maxage=0, no-cache, no-store
31+
- private, max-age=60, s-maxage=60
32+
Content-Length:
33+
- "40"
34+
Content-Security-Policy:
35+
- default-src 'none'
36+
Content-Type:
37+
- application/json
38+
Server:
39+
- nginx
40+
Strict-Transport-Security:
41+
- max-age=31536000
42+
Vary:
43+
- Authorization, X-Filter
44+
- Authorization, X-Filter
45+
X-Accepted-Oauth-Scopes:
46+
- account:read_only
47+
X-Content-Type-Options:
48+
- nosniff
49+
X-Frame-Options:
50+
- DENY
51+
- DENY
52+
X-Oauth-Scopes:
53+
- '*'
54+
X-Ratelimit-Limit:
55+
- "1200"
56+
X-Xss-Protection:
57+
- 1; mode=block
58+
status: 200 OK
59+
code: 200
60+
duration: ""
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: ""
6+
form: {}
7+
headers:
8+
Accept:
9+
- application/json
10+
Content-Type:
11+
- application/json
12+
User-Agent:
13+
- linodego/dev https://github.com/linode/linodego
14+
url: https://api.linode.com/v4beta/account/availability
15+
method: GET
16+
response:
17+
body: '{"data": [{"region": "ap-west", "unavailable": []}, {"region": "ca-central",
18+
"unavailable": []}, {"region": "ap-southeast", "unavailable": []}, {"region":
19+
"us-central", "unavailable": []}, {"region": "us-west", "unavailable": []},
20+
{"region": "us-southeast", "unavailable": []}, {"region": "us-east", "unavailable":
21+
[]}, {"region": "eu-west", "unavailable": []}, {"region": "ap-south", "unavailable":
22+
[]}, {"region": "eu-central", "unavailable": []}, {"region": "ap-northeast",
23+
"unavailable": []}], "page": 1, "pages": 1, "results": 11}'
24+
headers:
25+
Access-Control-Allow-Credentials:
26+
- "true"
27+
Access-Control-Allow-Headers:
28+
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
29+
Access-Control-Allow-Methods:
30+
- HEAD, GET, OPTIONS, POST, PUT, DELETE
31+
Access-Control-Allow-Origin:
32+
- '*'
33+
Access-Control-Expose-Headers:
34+
- X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Status
35+
Cache-Control:
36+
- private, max-age=0, s-maxage=0, no-cache, no-store
37+
- private, max-age=60, s-maxage=60
38+
Content-Security-Policy:
39+
- default-src 'none'
40+
Content-Type:
41+
- application/json
42+
Server:
43+
- nginx
44+
Strict-Transport-Security:
45+
- max-age=31536000
46+
Vary:
47+
- Accept-Encoding
48+
- Authorization, X-Filter
49+
- Authorization, X-Filter
50+
X-Accepted-Oauth-Scopes:
51+
- account:read_only
52+
X-Content-Type-Options:
53+
- nosniff
54+
X-Frame-Options:
55+
- DENY
56+
- DENY
57+
X-Oauth-Scopes:
58+
- '*'
59+
X-Ratelimit-Limit:
60+
- "1200"
61+
X-Xss-Protection:
62+
- 1; mode=block
63+
status: 200 OK
64+
code: 200
65+
duration: ""

0 commit comments

Comments
 (0)