provider/aws: Fixed the data source ami enforcing non-nil values

This commit is contained in:
Ninir 2016-12-15 19:18:57 +01:00
parent 1d794d313b
commit a8a37b8f84
4 changed files with 57 additions and 7 deletions

View File

@ -193,7 +193,11 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
}
if ownersOk {
params.Owners = expandStringList(owners.([]interface{}))
o := expandStringList(owners.([]interface{}))
if len(o) > 0 {
params.Owners = o
}
}
resp, err := conn.DescribeImages(params)

View File

@ -14,7 +14,7 @@ func TestAccAWSAmiDataSource_natInstance(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccCheckAwsAmiDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.nat_ami"),
@ -57,7 +57,7 @@ func TestAccAWSAmiDataSource_windowsInstance(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccCheckAwsAmiDataSourceWindowsConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.windows_ami"),
@ -95,7 +95,7 @@ func TestAccAWSAmiDataSource_instanceStore(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccCheckAwsAmiDataSourceInstanceStoreConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.instance_store_ami"),
@ -129,7 +129,7 @@ func TestAccAWSAmiDataSource_owners(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccCheckAwsAmiDataSourceOwnersConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"),
@ -139,12 +139,28 @@ func TestAccAWSAmiDataSource_owners(t *testing.T) {
})
}
// Acceptance test for: https://github.com/hashicorp/terraform/issues/10758
func TestAccAWSAmiDataSource_ownersEmpty(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsAmiDataSourceEmptyOwnersConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"),
),
},
},
})
}
func TestAccAWSAmiDataSource_localNameFilter(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccCheckAwsAmiDataSourceNameRegexConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.name_regex_filtered_ami"),
@ -313,6 +329,13 @@ data "aws_ami" "amazon_ami" {
}
`
const testAccCheckAwsAmiDataSourceEmptyOwnersConfig = `
data "aws_ami" "amazon_ami" {
most_recent = true
owners = [""]
}
`
// Testing name_regex parameter
const testAccCheckAwsAmiDataSourceNameRegexConfig = `
data "aws_ami" "name_regex_filtered_ami" {

View File

@ -695,7 +695,10 @@ func flattenElastiCacheParameters(list []*elasticache.Parameter) []map[string]in
func expandStringList(configured []interface{}) []*string {
vs := make([]*string, 0, len(configured))
for _, v := range configured {
vs = append(vs, aws.String(v.(string)))
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, aws.String(v.(string)))
}
}
return vs
}

View File

@ -447,7 +447,27 @@ func TestExpandStringList(t *testing.T) {
stringList,
expected)
}
}
func TestExpandStringListEmptyItems(t *testing.T) {
initialList := []string{"foo", "bar", "", "baz"}
l := make([]interface{}, len(initialList))
for i, v := range initialList {
l[i] = v
}
stringList := expandStringList(l)
expected := []*string{
aws.String("foo"),
aws.String("bar"),
aws.String("baz"),
}
if !reflect.DeepEqual(stringList, expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
stringList,
expected)
}
}
func TestExpandParameters(t *testing.T) {