github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/fastly/data_source_ip_ranges.go (about) 1 package fastly 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "sort" 9 "strconv" 10 11 "github.com/hashicorp/go-cleanhttp" 12 "github.com/hashicorp/terraform/helper/hashcode" 13 "github.com/hashicorp/terraform/helper/schema" 14 ) 15 16 type dataSourceFastlyIPRangesResult struct { 17 Addresses []string 18 } 19 20 func dataSourceFastlyIPRanges() *schema.Resource { 21 return &schema.Resource{ 22 Read: dataSourceFastlyIPRangesRead, 23 24 Schema: map[string]*schema.Schema{ 25 "cidr_blocks": &schema.Schema{ 26 Type: schema.TypeList, 27 Computed: true, 28 Elem: &schema.Schema{Type: schema.TypeString}, 29 }, 30 }, 31 } 32 } 33 34 func dataSourceFastlyIPRangesRead(d *schema.ResourceData, meta interface{}) error { 35 36 conn := cleanhttp.DefaultClient() 37 38 log.Printf("[DEBUG] Reading IP ranges") 39 40 res, err := conn.Get("https://api.fastly.com/public-ip-list") 41 42 if err != nil { 43 return fmt.Errorf("Error listing IP ranges: %s", err) 44 } 45 46 defer res.Body.Close() 47 48 data, err := ioutil.ReadAll(res.Body) 49 50 if err != nil { 51 return fmt.Errorf("Error reading response body: %s", err) 52 } 53 54 d.SetId(strconv.Itoa(hashcode.String(string(data)))) 55 56 result := new(dataSourceFastlyIPRangesResult) 57 58 if err := json.Unmarshal(data, result); err != nil { 59 return fmt.Errorf("Error parsing result: %s", err) 60 } 61 62 sort.Strings(result.Addresses) 63 64 if err := d.Set("cidr_blocks", result.Addresses); err != nil { 65 return fmt.Errorf("Error setting ip ranges: %s", err) 66 } 67 68 return nil 69 70 }