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

Add option to allow no results when querying aws_instances Data Source #5055

Closed
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
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