From 7323e45201fb349f219f402ac432368a0e330a7f Mon Sep 17 00:00:00 2001 From: Joshua Sager Date: Thu, 9 Jul 2020 15:15:16 -0400 Subject: [PATCH 1/2] Added support for /datacenters API call --- fastly/datacenters.go | 24 ++++++++++++++ fastly/datacenters_test.go | 19 +++++++++++ fastly/fixtures/datacenters/list.yaml | 46 +++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 fastly/datacenters.go create mode 100644 fastly/datacenters_test.go create mode 100644 fastly/fixtures/datacenters/list.yaml diff --git a/fastly/datacenters.go b/fastly/datacenters.go new file mode 100644 index 000000000..10c5be0ab --- /dev/null +++ b/fastly/datacenters.go @@ -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 +} diff --git a/fastly/datacenters_test.go b/fastly/datacenters_test.go new file mode 100644 index 000000000..c73eecde6 --- /dev/null +++ b/fastly/datacenters_test.go @@ -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") + } +} diff --git a/fastly/fixtures/datacenters/list.yaml b/fastly/fixtures/datacenters/list.yaml new file mode 100644 index 000000000..11c214ec5 --- /dev/null +++ b/fastly/fixtures/datacenters/list.yaml @@ -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 From 2d0d9284862caa223bf0eaae2182e6da6b89246d Mon Sep 17 00:00:00 2001 From: Joshua Sager Date: Mon, 13 Jul 2020 14:57:25 -0400 Subject: [PATCH 2/2] Added Coordinates struct to represent the location of a datacenter --- fastly/datacenters.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fastly/datacenters.go b/fastly/datacenters.go index 10c5be0ab..0bf708011 100644 --- a/fastly/datacenters.go +++ b/fastly/datacenters.go @@ -1,12 +1,20 @@ package fastly +// Coordinates represent the location of a datacenter. +type Coordinates struct { + Latitude float64 `mapstructure:"latitude"` + Longtitude float64 `mapstructure:"longitude"` + X float64 `mapstructure:"x"` + Y float64 `mapstructure:"y"` +} + // 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"` + Code string `mapstructure:"code"` + Coordinates Coordinates `mapstructure:"coordinates"` + Group string `mapstructure:"group"` + Name string `mapstructure:"name"` + Shield string `mapstructure:"shield"` } // AllDatacenters returns the lists of datacenters for Fastly's network.