Skip to content

Commit

Permalink
Add option to allow no results when querying for instances data
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Cooper committed Jul 4, 2018
1 parent f86cbff commit 9c81e9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion aws/data_source_aws_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func dataSourceAwsInstances() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"allow_no_results": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand All @@ -58,6 +63,7 @@ func dataSourceAwsInstancesRead(d *schema.ResourceData, meta interface{}) error

filters, filtersOk := d.GetOk("filter")
tags, tagsOk := d.GetOk("instance_tags")
allowNoResults := d.Get("allow_no_results").(bool)

if !filtersOk && !tagsOk {
return fmt.Errorf("One of filters or instance_tags must be assigned")
Expand Down Expand Up @@ -107,7 +113,7 @@ func dataSourceAwsInstancesRead(d *schema.ResourceData, meta interface{}) error
return err
}

if len(instanceIds) < 1 {
if len(instanceIds) < 1 && allowNoResults == false {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}

Expand Down
28 changes: 28 additions & 0 deletions aws/data_source_aws_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) {
})
}

func TestAccAWSInstancesDataSource_noResults(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccInstancesDataSourceConfig_noResults,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "0"),
resource.TestCheckResourceAttr("data.aws_instances.test", "private_ips.#", "0"),
resource.TestCheckResourceAttr("data.aws_instances.test", "public_ips.#", "0"),
),
},
},
})
}

const testAccInstancesDataSourceConfig_ids = `
data "aws_ami" "ubuntu" {
most_recent = true
Expand Down Expand Up @@ -167,3 +184,14 @@ data "aws_instances" "test" {
}
`, rInt)
}

const testAccInstancesDataSourceConfig_noResults = `
data "aws_instances" "test" {
filter {
name = "instance-id"
values = ["does", "not", "exist"]
}
allow_no_results = true
}
`
2 changes: 2 additions & 0 deletions website/docs/d/instances.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ exactly match a pair on desired instances.
several valid keys, for a full reference, check out
[describe-instances in the AWS CLI reference][1].

* `allow_no_results` - (Optional) Defaults to false. If true, empty lists could be returned for `ids`, `private_ips` and `public_ips`. This is useful when querying the count of ephemeral instances (e.g. managed via autoscaling group) which may not be created yet.

## Attributes Reference

* `ids` - IDs of instances found through the filter
Expand Down

0 comments on commit 9c81e9d

Please sign in to comment.