forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/olvesh/terraform-provider…
…-google * 'master' of https://github.com/olvesh/terraform-provider-google: (24 commits) Cleanup after v1.14.0 release v1.14.0 Update CHANGELOG.md Add new google_compute_regions (hashicorp#1603) Update CHANGELOG.md Fix forwarding rule data source test (hashicorp#1606) Update CHANGELOG.md Fix redis authorized network and tests. The Redis API currently only accepts partial links. The tests weren't failing because they weren't actually using the network (oops). There were a few other test issues that I fixed while I was there. Fixes hashicorp#1571. (hashicorp#1599) update auth docs (hashicorp#1587) Fix network_tier tests. Add documentation for network tier (hashicorp#1593) Warn about ip_version with ip_address in global forwarding rule (hashicorp#616) Update CHANGELOG.md add support for network tiers (hashicorp#1530) Update CHANGELOG.md Allow using in repo configuration for cloudbuild trigger (hashicorp#1557) Update CHANGELOG.md add update support for redis (hashicorp#1590) Update CHANGELOG.md Added GCP Netblock Data Source (hashicorp#1416) (hashicorp#1580) ...
- Loading branch information
Showing
38 changed files
with
1,464 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sort" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
func dataSourceGoogleComputeRegions() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleComputeRegionsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"names": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice([]string{"UP", "DOWN"}, false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleComputeRegionsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
filter := "" | ||
if s, ok := d.GetOk("status"); ok { | ||
filter = fmt.Sprintf(" (status eq %s)", s) | ||
} | ||
|
||
call := config.clientCompute.Regions.List(project).Filter(filter) | ||
|
||
resp, err := call.Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
regions := flattenRegions(resp.Items) | ||
log.Printf("[DEBUG] Received Google Compute Regions: %q", regions) | ||
|
||
d.Set("names", regions) | ||
d.Set("project", project) | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
return nil | ||
} | ||
|
||
func flattenRegions(regions []*compute.Region) []string { | ||
result := make([]string, len(regions), len(regions)) | ||
for i, region := range regions { | ||
result[i] = region.Name | ||
} | ||
sort.Strings(result) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package google | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccComputeRegions_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckGoogleComputeRegionsConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGoogleComputeRegionsMeta("data.google_compute_regions.available"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGoogleComputeRegionsMeta(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find regions data source: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return errors.New("regions data source ID not set.") | ||
} | ||
|
||
count, ok := rs.Primary.Attributes["names.#"] | ||
if !ok { | ||
return errors.New("can't find 'names' attribute") | ||
} | ||
|
||
noOfNames, err := strconv.Atoi(count) | ||
if err != nil { | ||
return errors.New("failed to read number of regions") | ||
} | ||
if noOfNames < 2 { | ||
return fmt.Errorf("expected at least 2 regions, received %d, this is most likely a bug", | ||
noOfNames) | ||
} | ||
|
||
for i := 0; i < noOfNames; i++ { | ||
idx := "names." + strconv.Itoa(i) | ||
v, ok := rs.Primary.Attributes[idx] | ||
if !ok { | ||
return fmt.Errorf("region list is corrupt (%q not found), this is definitely a bug", idx) | ||
} | ||
if len(v) < 1 { | ||
return fmt.Errorf("Empty region name (%q), this is definitely a bug", idx) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccCheckGoogleComputeRegionsConfig = ` | ||
data "google_compute_regions" "available" {} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func dataSourceGoogleNetblockIpRanges() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleNetblockIpRangesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"cidr_blocks": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"cidr_blocks_ipv4": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"cidr_blocks_ipv6": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleNetblockIpRangesRead(d *schema.ResourceData, meta interface{}) error { | ||
d.SetId("netblock-ip-ranges") | ||
|
||
// https://cloud.google.com/compute/docs/faq#where_can_i_find_product_name_short_ip_ranges | ||
CidrBlocks, err := getCidrBlocks() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("cidr_blocks", CidrBlocks["cidr_blocks"]) | ||
d.Set("cidr_blocks_ipv4", CidrBlocks["cidr_blocks_ipv4"]) | ||
d.Set("cidr_blocks_ipv6", CidrBlocks["cidr_blocks_ipv6"]) | ||
|
||
return nil | ||
} | ||
|
||
func netblock_request(name string) (string, error) { | ||
const DNS_URL = "https://dns.google.com/resolve?name=%s&type=TXT" | ||
|
||
response, err := http.Get(fmt.Sprintf("https://dns.google.com/resolve?name=%s&type=TXT", name)) | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("Error from _cloud-netblocks: %s", err) | ||
} | ||
|
||
defer response.Body.Close() | ||
body, err := ioutil.ReadAll(response.Body) | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("Error to retrieve the domains list: %s", err) | ||
} | ||
|
||
return string(body), nil | ||
} | ||
|
||
func getCidrBlocks() (map[string][]string, error) { | ||
const INITIAL_NETBLOCK_DNS = "_cloud-netblocks.googleusercontent.com" | ||
var dnsNetblockList []string | ||
cidrBlocks := make(map[string][]string) | ||
|
||
response, err := netblock_request(INITIAL_NETBLOCK_DNS) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
splitedResponse := strings.Split(string(response), " ") | ||
|
||
for _, sp := range splitedResponse { | ||
if strings.HasPrefix(sp, "include:") { | ||
dnsNetblock := strings.Replace(sp, "include:", "", 1) | ||
dnsNetblockList = append(dnsNetblockList, dnsNetblock) | ||
} | ||
} | ||
|
||
for len(dnsNetblockList) > 0 { | ||
|
||
dnsNetblock := dnsNetblockList[0] | ||
|
||
dnsNetblockList[0] = "" | ||
dnsNetblockList = dnsNetblockList[1:len(dnsNetblockList)] | ||
|
||
response, err = netblock_request(dnsNetblock) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
splitedResponse = strings.Split(string(response), " ") | ||
|
||
for _, sp := range splitedResponse { | ||
if strings.HasPrefix(sp, "ip") { | ||
|
||
cdrBlock := strings.Split(sp, ":")[1] | ||
cidrBlocks["cidr_blocks"] = append(cidrBlocks["cidr_blocks"], cdrBlock) | ||
|
||
if strings.HasPrefix(sp, "ip4") { | ||
cdrBlock := strings.Replace(sp, "ip4:", "", 1) | ||
cidrBlocks["cidr_blocks_ipv4"] = append(cidrBlocks["cidr_blocks_ipv4"], cdrBlock) | ||
|
||
} else if strings.HasPrefix(sp, "ip6") { | ||
cdrBlock := strings.Replace(sp, "ip6:", "", 1) | ||
cidrBlocks["cidr_blocks_ipv6"] = append(cidrBlocks["cidr_blocks_ipv6"], cdrBlock) | ||
} | ||
} else if strings.HasPrefix(sp, "include:") { | ||
cidr_block := strings.Replace(sp, "include:", "", 1) | ||
dnsNetblockList = append(dnsNetblockList, cidr_block) | ||
} | ||
} | ||
} | ||
|
||
return cidrBlocks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package google | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceGoogleNetblockIpRanges_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccNetblockIpRangesConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr("data.google_netblock_ip_ranges.some", | ||
"cidr_blocks.#", regexp.MustCompile(("^[1-9]+[0-9]*$"))), | ||
resource.TestMatchResourceAttr("data.google_netblock_ip_ranges.some", | ||
"cidr_blocks.0", regexp.MustCompile("^[0-9./:]+$")), | ||
resource.TestMatchResourceAttr("data.google_netblock_ip_ranges.some", | ||
"cidr_blocks_ipv4.#", regexp.MustCompile(("^[1-9]+[0-9]*$"))), | ||
resource.TestMatchResourceAttr("data.google_netblock_ip_ranges.some", | ||
"cidr_blocks_ipv4.0", regexp.MustCompile("^[0-9./]+$")), | ||
resource.TestMatchResourceAttr("data.google_netblock_ip_ranges.some", | ||
"cidr_blocks_ipv6.#", regexp.MustCompile(("^[1-9]+[0-9]*$"))), | ||
resource.TestMatchResourceAttr("data.google_netblock_ip_ranges.some", | ||
"cidr_blocks_ipv6.0", regexp.MustCompile("^[0-9./:]+$")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccNetblockIpRangesConfig = ` | ||
data "google_netblock_ip_ranges" "some" {} | ||
` |
Oops, something went wrong.