forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
regions.go
93 lines (73 loc) · 2.37 KB
/
regions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// RegionService is the interface to interact with Region endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/region
type RegionService interface {
Availability(ctx context.Context, regionID string, planType string) (*PlanAvailability, *http.Response, error)
List(ctx context.Context, options *ListOptions) ([]Region, *Meta, *http.Response, error)
}
var _ RegionService = &RegionServiceHandler{}
// RegionServiceHandler handles interaction with the region methods for the Vultr API
type RegionServiceHandler struct {
client *Client
}
// Region represents a Vultr region
type Region struct {
ID string `json:"id"`
City string `json:"city"`
Country string `json:"country"`
Continent string `json:"continent,omitempty"`
Options []string `json:"options"`
}
type regionBase struct {
Regions []Region `json:"regions"`
Meta *Meta
}
// PlanAvailability contains all available plans.
type PlanAvailability struct {
AvailablePlans []string `json:"available_plans"`
}
// List returns all available regions
func (r *RegionServiceHandler) List(ctx context.Context, options *ListOptions) ([]Region, *Meta, *http.Response, error) {
uri := "/v2/regions"
req, err := r.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
regions := new(regionBase)
resp, err := r.client.DoWithContext(ctx, req, ®ions)
if err != nil {
return nil, nil, resp, err
}
return regions.Regions, regions.Meta, resp, nil
}
// Availability retrieves a list of the plan IDs currently available for a given location.
func (r *RegionServiceHandler) Availability(ctx context.Context, regionID string, planType string) (*PlanAvailability, *http.Response, error) {
uri := fmt.Sprintf("/v2/regions/%s/availability", regionID)
req, err := r.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
// Optional planType filter
if planType != "" {
q := req.URL.Query()
q.Add("type", planType)
req.URL.RawQuery = q.Encode()
}
plans := new(PlanAvailability)
resp, err := r.client.DoWithContext(ctx, req, plans)
if err != nil {
return nil, resp, err
}
return plans, resp, nil
}