gogreen/regions.go

69 lines
1.5 KiB
Go
Raw Normal View History

2014-09-03 14:03:30 +00:00
package godo
import (
"context"
"net/http"
)
2016-11-29 02:42:23 +00:00
// RegionsService is an interface for interfacing with the regions
2015-09-18 13:10:59 +00:00
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Regions
type RegionsService interface {
2016-11-29 02:42:23 +00:00
List(context.Context, *ListOptions) ([]Region, *Response, error)
}
// RegionsServiceOp handles communication with the region related methods of the
2014-09-03 14:03:30 +00:00
// DigitalOcean API.
type RegionsServiceOp struct {
2014-09-03 14:03:30 +00:00
client *Client
}
var _ RegionsService = &RegionsServiceOp{}
2014-09-03 14:03:30 +00:00
// Region represents a DigitalOcean Region
type Region struct {
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Sizes []string `json:"sizes,omitempty"`
Available bool `json:"available,omitempty"`
Features []string `json:"features,omitempty"`
2014-09-03 14:03:30 +00:00
}
type regionsRoot struct {
Regions []Region
2022-08-14 17:34:48 +00:00
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
2014-09-03 14:03:30 +00:00
}
func (r Region) String() string {
return Stringify(r)
}
// List all regions
2016-11-29 02:42:23 +00:00
func (s *RegionsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Region, *Response, error) {
2014-09-03 14:03:30 +00:00
path := "v2/regions"
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
2014-09-03 14:03:30 +00:00
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, nil, err
}
root := new(regionsRoot)
resp, err := s.client.Do(ctx, req, root)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
2014-09-03 14:03:30 +00:00
return root.Regions, resp, err
2014-09-03 14:03:30 +00:00
}