2017-02-07 21:33:23 +00:00
|
|
|
package lib
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
import "sort"
|
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
// Region on Vultr
|
|
|
|
type Region struct {
|
|
|
|
ID int `json:"DCID,string"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Country string `json:"country"`
|
|
|
|
Continent string `json:"continent"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Ddos bool `json:"ddos_protection"`
|
|
|
|
BlockStorage bool `json:"block_storage"`
|
|
|
|
Code string `json:"regioncode"`
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
type regions []Region
|
|
|
|
|
|
|
|
func (s regions) Len() int { return len(s) }
|
|
|
|
func (s regions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s regions) Less(i, j int) bool {
|
|
|
|
// sort order: continent, name
|
|
|
|
if s[i].Continent < s[j].Continent {
|
|
|
|
return true
|
|
|
|
} else if s[i].Continent > s[j].Continent {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return s[i].Name < s[j].Name
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
// GetRegions returns a list of all available Vultr regions
|
|
|
|
func (c *Client) GetRegions() ([]Region, error) {
|
|
|
|
var regionMap map[string]Region
|
|
|
|
if err := c.get(`regions/list`, ®ionMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var regionList []Region
|
|
|
|
for _, os := range regionMap {
|
|
|
|
regionList = append(regionList, os)
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
sort.Sort(regions(regionList))
|
2017-02-07 21:33:23 +00:00
|
|
|
return regionList, nil
|
|
|
|
}
|