Skip to content

Commit

Permalink
Merge branch 'b-aws_prefix_list' into f-aws_prefix_list-data-source-m…
Browse files Browse the repository at this point in the history
…anaged-prefix-list
  • Loading branch information
roberth-k committed Jul 21, 2020
2 parents 49921b1 + a35c88c commit e46e4e4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
28 changes: 16 additions & 12 deletions aws/data_source_aws_prefix_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -45,31 +46,34 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error
if prefixListID := d.Get("prefix_list_id"); prefixListID != "" {
req.PrefixListIds = aws.StringSlice([]string{prefixListID.(string)})
}
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"prefix-list-name": d.Get("name").(string),
},
)
if prefixListName := d.Get("name"); prefixListName.(string) != "" {
req.Filters = append(req.Filters, &ec2.Filter{
Name: aws.String("prefix-list-name"),
Values: aws.StringSlice([]string{prefixListName.(string)}),
})
}

log.Printf("[DEBUG] Reading Prefix List: %s", req)
resp, err := conn.DescribePrefixLists(req)
if err != nil {
switch {
case err != nil:
return err
}
if resp == nil || len(resp.PrefixLists) == 0 {
case resp == nil || len(resp.PrefixLists) == 0:
return fmt.Errorf("no matching prefix list found; the prefix list ID or name may be invalid or not exist in the current region")
case len(resp.PrefixLists) > 1:
return fmt.Errorf("more than one prefix list matched the given set of criteria")
}

pl := resp.PrefixLists[0]

d.SetId(*pl.PrefixListId)
d.Set("name", pl.PrefixListName)

cidrs := make([]string, len(pl.Cidrs))
for i, v := range pl.Cidrs {
cidrs[i] = *v
cidrs := aws.StringValueSlice(pl.Cidrs)
sort.Strings(cidrs)
if err := d.Set("cidr_blocks", cidrs); err != nil {
return fmt.Errorf("failed to set cidr blocks of prefix list %s: %s", d.Id(), err)
}
d.Set("cidr_blocks", cidrs)

return nil
}
45 changes: 45 additions & 0 deletions aws/data_source_aws_prefix_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"strconv"
"testing"

Expand Down Expand Up @@ -98,3 +99,47 @@ data "aws_prefix_list" "s3_by_id" {
}
}
`

func TestAccDataSourceAwsPrefixList_matchesTooMany(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPrefixListConfig_matchesTooMany,
ExpectError: regexp.MustCompile(`more than one prefix list matched the given set of criteria`),
},
},
})
}

const testAccDataSourceAwsPrefixListConfig_matchesTooMany = `
data "aws_prefix_list" "test" {}
`

func TestAccDataSourceAwsPrefixList_nameDoesNotOverrideFilter(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
// The vanilla DescribePrefixLists API only supports filtering by
// id and name. In this case, the `name` attribute and `prefix-list-id`
// filter have been set up such that they conflict, thus proving
// that both criteria took effect.
Config: testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter,
ExpectError: regexp.MustCompile(`no matching prefix list found`),
},
},
})
}

const testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter = `
data "aws_prefix_list" "test" {
name = "com.amazonaws.us-west-2.s3"
filter {
name = "prefix-list-id"
values = ["pl-00a54069"] # com.amazonaws.us-west-2.dynamodb
}
}
`
11 changes: 10 additions & 1 deletion website/docs/d/prefix_list.html.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
subcategory: "VPC"
layout: "aws"
page_title: "AWS: aws_prefix-list"
page_title: "AWS: aws_prefix_list"
description: |-
Provides details about a specific prefix list
---
Expand Down Expand Up @@ -44,6 +44,15 @@ resource "aws_network_acl_rule" "private_s3" {
}
```

### Find the regional DynamoDB prefix list

```hcl
data "aws_region" "current" {}
data "aws_prefix_list" "dynamo" {
name = "com.amazonaws.${data.aws_region.current.name}.dynamodb"
}
```

### Filter

```hcl
Expand Down

0 comments on commit e46e4e4

Please sign in to comment.