From 6ddd93c0fc0036db9c48decf29ef76adf02a14ab Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Fri, 7 Dec 2018 02:32:44 -0500 Subject: [PATCH] data-source/aws_ip_ranges: Add url argument The original hardcoded URL (https://ip-ranges.amazonaws.com/ip-ranges.json) is only for the AWS Commercial partition. Other AWS partitions have privately available IP Range JSON files at known URLs. Output from acceptance testing: ``` --- PASS: TestAccAWSIPRanges_basic (9.89s) --- PASS: TestAccAWSIPRanges_Url (10.13s) ``` --- aws/data_source_aws_ip_ranges.go | 18 +++++++++++------ aws/data_source_aws_ip_ranges_test.go | 27 +++++++++++++++++++++++++- website/docs/d/ip_ranges.html.markdown | 6 ++++-- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/aws/data_source_aws_ip_ranges.go b/aws/data_source_aws_ip_ranges.go index aec2529e16f..3706ec8ba1b 100644 --- a/aws/data_source_aws_ip_ranges.go +++ b/aws/data_source_aws_ip_ranges.go @@ -65,6 +65,11 @@ func dataSourceAwsIPRanges() *schema.Resource { Type: schema.TypeInt, Computed: true, }, + "url": { + Type: schema.TypeString, + Optional: true, + Default: "https://ip-ranges.amazonaws.com/ip-ranges.json", + }, }, } } @@ -72,13 +77,14 @@ func dataSourceAwsIPRanges() *schema.Resource { func dataSourceAwsIPRangesRead(d *schema.ResourceData, meta interface{}) error { conn := cleanhttp.DefaultClient() + url := d.Get("url").(string) - log.Printf("[DEBUG] Reading IP ranges") + log.Printf("[DEBUG] Reading IP ranges from %s", url) - res, err := conn.Get("https://ip-ranges.amazonaws.com/ip-ranges.json") + res, err := conn.Get(url) if err != nil { - return fmt.Errorf("Error listing IP ranges: %s", err) + return fmt.Errorf("Error listing IP ranges from (%s): %s", url, err) } defer res.Body.Close() @@ -86,13 +92,13 @@ func dataSourceAwsIPRangesRead(d *schema.ResourceData, meta interface{}) error { data, err := ioutil.ReadAll(res.Body) if err != nil { - return fmt.Errorf("Error reading response body: %s", err) + return fmt.Errorf("Error reading response body from (%s): %s", url, err) } result := new(dataSourceAwsIPRangesResult) if err := json.Unmarshal(data, result); err != nil { - return fmt.Errorf("Error parsing result: %s", err) + return fmt.Errorf("Error parsing result from (%s): %s", url, err) } if err := d.Set("create_date", result.CreateDate); err != nil { @@ -155,7 +161,7 @@ func dataSourceAwsIPRangesRead(d *schema.ResourceData, meta interface{}) error { } if len(ipPrefixes) == 0 && len(ipv6Prefixes) == 0 { - return fmt.Errorf("No IP ranges result from filters") + return fmt.Errorf("No IP ranges result from filters from (%s)", url) } sort.Strings(ipPrefixes) diff --git a/aws/data_source_aws_ip_ranges_test.go b/aws/data_source_aws_ip_ranges_test.go index 2b54869adc0..8c9676f869c 100644 --- a/aws/data_source_aws_ip_ranges_test.go +++ b/aws/data_source_aws_ip_ranges_test.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSIPRanges(t *testing.T) { +func TestAccAWSIPRanges_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -30,6 +30,23 @@ func TestAccAWSIPRanges(t *testing.T) { }) } +func TestAccAWSIPRanges_Url(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAWSIPRangesConfigUrl, + Check: resource.ComposeTestCheckFunc( + testAccAWSIPRangesCheckAttributes("data.aws_ip_ranges.some"), + testAccAWSIPRangesCheckCidrBlocksAttribute("data.aws_ip_ranges.some", "cidr_blocks"), + testAccAWSIPRangesCheckCidrBlocksAttribute("data.aws_ip_ranges.some", "ipv6_cidr_blocks"), + ), + }, + }, + }) +} + func testAccAWSIPRangesCheckAttributes(n string) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -141,3 +158,11 @@ data "aws_ip_ranges" "some" { services = [ "ec2" ] } ` + +const testAccAWSIPRangesConfigUrl = ` +data "aws_ip_ranges" "some" { + regions = [ "eu-west-1", "eu-central-1" ] + services = [ "ec2" ] + url = "https://ip-ranges.amazonaws.com/ip-ranges.json" +} +` diff --git a/website/docs/d/ip_ranges.html.markdown b/website/docs/d/ip_ranges.html.markdown index 4636cc1341a..0e7ec817b8e 100644 --- a/website/docs/d/ip_ranges.html.markdown +++ b/website/docs/d/ip_ranges.html.markdown @@ -8,7 +8,7 @@ description: |- # Data Source: aws_ip_ranges -Use this data source to get the [IP ranges][1] of various AWS products and services. +Use this data source to get the IP ranges of various AWS products and services. For more information about the contents of this data source and required JSON syntax if referencing a custom URL, see the [AWS IP Address Ranges documention][1]. ## Example Usage @@ -48,6 +48,8 @@ omitted). Valid items are `global` (for `cloudfront`) as well as all AWS regions ~> **NOTE:** If the specified combination of regions and services does not yield any CIDR blocks, Terraform will fail. +* `url` - (Optional) Custom URL for source JSON file. Syntax must match [AWS IP Address Ranges documention][1]. Defaults to `https://ip-ranges.amazonaws.com/ip-ranges.json`. + ## Attributes Reference * `cidr_blocks` - The lexically ordered list of CIDR blocks. @@ -56,4 +58,4 @@ CIDR blocks, Terraform will fail. * `sync_token` - The publication time of the IP ranges, in Unix epoch time format (e.g. `1470267965`). -[1]: http://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html +[1]: https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html