Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to list all datacenters #210

Merged
merged 2 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions fastly/datacenters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fastly

// Datacenter is a list of Datacenters returned by the Fastly API.
type Datacenter struct {
Code string `mapstructure:"code"`
Coordinates map[string]string `mapstructure:"coordinates"`
Group string `mapstructure:"group"`
Name string `mapstructure:"name"`
Shield string `mapstructure:"shield"`
}

// AllDatacenters returns the lists of datacenters for Fastly's network.
func (c *Client) AllDatacenters() (datacenters []Datacenter, err error) {
resp, err := c.Get("/datacenters", nil)
if err != nil {
return nil, err
}
var m []Datacenter
if err := decodeBodyMap(resp.Body, &m); err != nil {
return nil, err
}

return m, nil
}
19 changes: 19 additions & 0 deletions fastly/datacenters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fastly

import "testing"

func TestDatacenters(t *testing.T) {
t.Parallel()

var err error
var datacenters []Datacenter
record(t, "datacenters/list", func(c *Client) {
datacenters, err = c.AllDatacenters()
})
if err != nil {
t.Fatal(err)
}
if len(datacenters) == 0 {
t.Fatal("missing datacenters")
}
}
46 changes: 46 additions & 0 deletions fastly/fixtures/datacenters/list.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
version: 1
rwmutex: {}
interactions:
- request:
body: ""
form: {}
headers:
Fastly-Key:
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
User-Agent:
- FastlyGo/0.4.3.dev (+github.com/fastly/go-fastly; go1.12)
url: https://api.fastly.com/datacenters
method: GET
response:
body: '[{"code":"AMS","name":"Amsterdam","group":"Europe","coordinates":{"x":0,"y":0,"latitude":52.308613,"longitude":4.763889},"shield":"amsterdam-nl"},{"code":"IAD","name":"Ashburn","group":"United States","coordinates":{"x":0,"y":0,"latitude":38.944533,"longitude":-77.455811}}]'
headers:
Accept-Ranges:
- bytes
Age:
- "0"
Cache-Control:
- no-cache
Content-Type:
- application/json
Date:
- Thu, 09 Jul 2020 20:16:27 GMT
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish
- 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-slwdc9037-CONTROL-SLWDC, cache-mdw17325-MDW
X-Timer:
- S1594319825.301688,VS0,VE114
status: 200 OK
code: 200