-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 and enhancements #14109
Changes from all commits
37c49d9
f259a03
3d2ca8e
a35c88c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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") | ||
Comment on lines
+63
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this is a perfectly valid change for consistency with most other data sources, we should only perform this change during a major version upgrade to align with practitioner versioning expectations, which the next one will be a few months away. It would be best to remove this change here and create a separate GitHub bug report so we can mark it for that future milestone. 👍 You're more than welcome to submit this change as well separately. |
||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here with respect to introducing a breaking change which should be removed for now. In this case though, if the ordering of the CIDR Blocks is not significant (which it likely is not), the At some point in the future we may go back through many of the |
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package aws | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of our newer codebase linting will likely pick this up on rebase/merge for not being AWS Region agnostic. Similar to the website example you added (👍 ) we should use the data "aws_region" "current" {}
data "aws_prefix_list" "dynamodb" {
name = "com.amazonaws.${data.aws_region.current.name}.dynamodb"
}
data "aws_prefix_list" "test" {
name = "com.amazonaws.${data.aws_region.current.name}.s3"
filter {
name = "prefix-list-id"
values = [data.aws_prefix_list.dynamodb.id]
}
} |
||
filter { | ||
name = "prefix-list-id" | ||
values = ["pl-00a54069"] # com.amazonaws.us-west-2.dynamodb | ||
} | ||
} | ||
` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The predominate coding style for the codebase is
if
conditionals with earlyreturn
s rather than bareswitch
conditionals. Can you please change this back rather than introducing a different coding style? Thanks!