Skip to content

Commit

Permalink
Merge pull request #201 from depop/bug/fix_upstream_http_errors
Browse files Browse the repository at this point in the history
Use gofastly library in ip ranges data source
  • Loading branch information
philippschulte authored Dec 13, 2019
2 parents 3eae94f + cb95d4d commit e8343df
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions fastly/data_source_ip_ranges.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package fastly

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"sort"
"strconv"

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -33,35 +29,21 @@ func dataSourceFastlyIPRanges() *schema.Resource {

func dataSourceFastlyIPRangesRead(d *schema.ResourceData, meta interface{}) error {

conn := cleanhttp.DefaultClient()
conn := meta.(*FastlyClient).conn

log.Printf("[DEBUG] Reading IP ranges")

res, err := conn.Get("https://api.fastly.com/public-ip-list")
addresses, err := conn.IPs()

if err != nil {
return fmt.Errorf("Error listing IP ranges: %s", err)
}

defer res.Body.Close()
d.SetId(hashcode.Strings(addresses))

data, err := ioutil.ReadAll(res.Body)
sort.Strings(addresses)

if err != nil {
return fmt.Errorf("Error reading response body: %s", err)
}

d.SetId(strconv.Itoa(hashcode.String(string(data))))

result := new(dataSourceFastlyIPRangesResult)

if err := json.Unmarshal(data, result); err != nil {
return fmt.Errorf("Error parsing result: %s", err)
}

sort.Strings(result.Addresses)

if err := d.Set("cidr_blocks", result.Addresses); err != nil {
if err := d.Set("cidr_blocks", addresses); err != nil {
return fmt.Errorf("Error setting ip ranges: %s", err)
}

Expand Down

0 comments on commit e8343df

Please sign in to comment.