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

d/aws_prefix_list: fixes #16739

Merged
merged 3 commits into from
Dec 15, 2020
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
11 changes: 6 additions & 5 deletions aws/data_source_aws_prefix_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ 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) != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: (Will be fixed in later linter refactoring)

Suggested change
if prefixListName := d.Get("name"); prefixListName.(string) != "" {
if prefixListName, ok := d.GetOk("name"); ok {

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)
Expand Down
78 changes: 68 additions & 10 deletions aws/data_source_aws_prefix_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package aws

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

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -41,7 +44,41 @@ func TestAccDataSourceAwsPrefixList_filter(t *testing.T) {
})
}

func TestAccDataSourceAwsPrefixList_nameDoesNotOverrideFilter(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter,
ExpectError: regexp.MustCompile(`no matching prefix list found`),
},
},
})
}

func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {
getPrefixListId := func(name string) (string, error) {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

input := ec2.DescribePrefixListsInput{
Filters: buildEC2AttributeFilterList(map[string]string{
"prefix-list-name": name,
}),
}

output, err := conn.DescribePrefixLists(&input)
if err != nil {
return "", err
}

if len(output.PrefixLists) != 1 {
return "", fmt.Errorf("prefix list %s not found", name)
}

return aws.StringValue(output.PrefixLists[0].PrefixListId), nil
}

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
Expand All @@ -50,17 +87,21 @@ func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {

attr := rs.Primary.Attributes

if attr["name"] != "com.amazonaws.us-west-2.s3" {
region := testAccGetRegion()
prefixListName := fmt.Sprintf("com.amazonaws.%s.s3", region)
prefixListId, err := getPrefixListId(prefixListName)
if err != nil {
return err
}

if attr["name"] != prefixListName {
return fmt.Errorf("bad name %s", attr["name"])
}
if attr["id"] != "pl-68a54001" {
if attr["id"] != prefixListId {
return fmt.Errorf("bad id %s", attr["id"])
}

var (
cidrBlockSize int
err error
)
var cidrBlockSize int

if cidrBlockSize, err = strconv.Atoi(attr["cidr_blocks.#"]); err != nil {
return err
Expand All @@ -74,27 +115,44 @@ func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {
}

const testAccDataSourceAwsPrefixListConfig = `
data "aws_region" "current" {}

data "aws_prefix_list" "s3_by_id" {
prefix_list_id = "pl-68a54001"
prefix_list_id = data.aws_prefix_list.s3_by_name.id
}

data "aws_prefix_list" "s3_by_name" {
name = "com.amazonaws.us-west-2.s3"
name = "com.amazonaws.${data.aws_region.current.name}.s3"
}
`

const testAccDataSourceAwsPrefixListConfigFilter = `
data "aws_region" "current" {}

data "aws_prefix_list" "s3_by_name" {
filter {
name = "prefix-list-name"
values = ["com.amazonaws.us-west-2.s3"]
values = ["com.amazonaws.${data.aws_region.current.name}.s3"]
}
}

data "aws_prefix_list" "s3_by_id" {
filter {
name = "prefix-list-id"
values = ["pl-68a54001"]
values = [data.aws_prefix_list.s3_by_name.id]
}
}
`

const testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter = `
data "aws_region" "current" {}

data "aws_prefix_list" "test" {
name = "com.amazonaws.${data.aws_region.current.name}.dynamodb"

filter {
name = "prefix-list-name"
values = ["com.amazonaws.${data.aws_region.current.name}.s3"]
}
}
`
2 changes: 1 addition & 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