forked from cloudflare/terraform-provider-cloudflare
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_ip_ranges.go
98 lines (81 loc) · 2.37 KB
/
data_source_ip_ranges.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cloudflare
import (
"fmt"
"io/ioutil"
"log"
"sort"
"strconv"
"strings"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
const (
urlIPV4s = "https://www.cloudflare.com/ips-v4"
urlIPV6s = "https://www.cloudflare.com/ips-v6"
)
func dataSourceCloudflareIPRanges() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudflareIPRangesRead,
Schema: map[string]*schema.Schema{
"cidr_blocks": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ipv4_cidr_blocks": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ipv6_cidr_blocks": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceCloudflareIPRangesRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading IPv4 ranges")
ipv4s, err := dataSourceCloudflareIPRangesGet(urlIPV4s)
if err != nil {
return fmt.Errorf("Error listing IPV4 ranges: %s", err)
}
sort.Strings(ipv4s)
log.Printf("[DEBUG] Reading IPv6 ranges")
ipv6s, err := dataSourceCloudflareIPRangesGet(urlIPV6s)
if err != nil {
return fmt.Errorf("Error listing IPV6 ranges: %s", err)
}
sort.Strings(ipv6s)
all := append([]string{}, ipv4s...)
all = append(all, ipv6s...)
d.SetId(strconv.Itoa(hashcode.String(strings.Join(all, "|"))))
if err := d.Set("cidr_blocks", all); err != nil {
return fmt.Errorf("Error setting all cidr blocks: %s", err)
}
if err := d.Set("ipv4_cidr_blocks", ipv4s); err != nil {
return fmt.Errorf("Error setting ipv4 cidr blocks: %s", err)
}
if err := d.Set("ipv6_cidr_blocks", ipv6s); err != nil {
return fmt.Errorf("Error setting ipv6 cidr blocks: %s", err)
}
return nil
}
// dataSourceCloudflareIPRangesGet performs an HTTP GET on the given URL and
// parses each line as an IP address.
func dataSourceCloudflareIPRangesGet(url string) ([]string, error) {
conn := cleanhttp.DefaultClient()
res, err := conn.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
list := strings.Split(strings.TrimSpace(string(body)), "\n")
return list, nil
}