|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/hashicorp/terraform/helper/schema" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceGoogleNetblockIpRanges() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceGoogleNetblockIpRangesRead, |
| 14 | + |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "cidr_blocks": { |
| 17 | + Type: schema.TypeList, |
| 18 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 19 | + Computed: true, |
| 20 | + }, |
| 21 | + "cidr_blocks_ipv4": { |
| 22 | + Type: schema.TypeList, |
| 23 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + "cidr_blocks_ipv6": { |
| 27 | + Type: schema.TypeList, |
| 28 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func dataSourceGoogleNetblockIpRangesRead(d *schema.ResourceData, meta interface{}) error { |
| 36 | + d.SetId("netblock-ip-ranges") |
| 37 | + |
| 38 | + // https://cloud.google.com/compute/docs/faq#where_can_i_find_product_name_short_ip_ranges |
| 39 | + CidrBlocks, err := getCidrBlocks() |
| 40 | + |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + d.Set("cidr_blocks", CidrBlocks["cidr_blocks"]) |
| 46 | + d.Set("cidr_blocks_ipv4", CidrBlocks["cidr_blocks_ipv4"]) |
| 47 | + d.Set("cidr_blocks_ipv6", CidrBlocks["cidr_blocks_ipv6"]) |
| 48 | + |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func netblock_request(name string) (string, error) { |
| 53 | + const DNS_URL = "https://dns.google.com/resolve?name=%s&type=TXT" |
| 54 | + |
| 55 | + response, err := http.Get(fmt.Sprintf("https://dns.google.com/resolve?name=%s&type=TXT", name)) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + return "", fmt.Errorf("Error from _cloud-netblocks: %s", err) |
| 59 | + } |
| 60 | + |
| 61 | + defer response.Body.Close() |
| 62 | + body, err := ioutil.ReadAll(response.Body) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + return "", fmt.Errorf("Error to retrieve the domains list: %s", err) |
| 66 | + } |
| 67 | + |
| 68 | + return string(body), nil |
| 69 | +} |
| 70 | + |
| 71 | +func getCidrBlocks() (map[string][]string, error) { |
| 72 | + const INITIAL_NETBLOCK_DNS = "_cloud-netblocks.googleusercontent.com" |
| 73 | + var dnsNetblockList []string |
| 74 | + cidrBlocks := make(map[string][]string) |
| 75 | + |
| 76 | + response, err := netblock_request(INITIAL_NETBLOCK_DNS) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + splitedResponse := strings.Split(string(response), " ") |
| 83 | + |
| 84 | + for _, sp := range splitedResponse { |
| 85 | + if strings.HasPrefix(sp, "include:") { |
| 86 | + dnsNetblock := strings.Replace(sp, "include:", "", 1) |
| 87 | + dnsNetblockList = append(dnsNetblockList, dnsNetblock) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for len(dnsNetblockList) > 0 { |
| 92 | + |
| 93 | + dnsNetblock := dnsNetblockList[0] |
| 94 | + |
| 95 | + dnsNetblockList[0] = "" |
| 96 | + dnsNetblockList = dnsNetblockList[1:len(dnsNetblockList)] |
| 97 | + |
| 98 | + response, err = netblock_request(dnsNetblock) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + splitedResponse = strings.Split(string(response), " ") |
| 105 | + |
| 106 | + for _, sp := range splitedResponse { |
| 107 | + if strings.HasPrefix(sp, "ip") { |
| 108 | + |
| 109 | + cdrBlock := strings.Split(sp, ":")[1] |
| 110 | + cidrBlocks["cidr_blocks"] = append(cidrBlocks["cidr_blocks"], cdrBlock) |
| 111 | + |
| 112 | + if strings.HasPrefix(sp, "ip4") { |
| 113 | + cdrBlock := strings.Replace(sp, "ip4:", "", 1) |
| 114 | + cidrBlocks["cidr_blocks_ipv4"] = append(cidrBlocks["cidr_blocks_ipv4"], cdrBlock) |
| 115 | + |
| 116 | + } else if strings.HasPrefix(sp, "ip6") { |
| 117 | + cdrBlock := strings.Replace(sp, "ip6:", "", 1) |
| 118 | + cidrBlocks["cidr_blocks_ipv6"] = append(cidrBlocks["cidr_blocks_ipv6"], cdrBlock) |
| 119 | + } |
| 120 | + } else if strings.HasPrefix(sp, "include:") { |
| 121 | + cidr_block := strings.Replace(sp, "include:", "", 1) |
| 122 | + dnsNetblockList = append(dnsNetblockList, cidr_block) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return cidrBlocks, nil |
| 128 | +} |
0 commit comments