Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-source/aws_ip_ranges: Add url argument #6756

Merged
merged 1 commit into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions aws/data_source_aws_ip_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,40 @@ func dataSourceAwsIPRanges() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"url": {
Type: schema.TypeString,
Optional: true,
Default: "https://ip-ranges.amazonaws.com/ip-ranges.json",
},
},
}
}

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()

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 {
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion aws/data_source_aws_ip_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {

Expand Down Expand Up @@ -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"
}
`
6 changes: 4 additions & 2 deletions website/docs/d/ip_ranges.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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