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

data-source/aws_network_interface: Add filter attribute #2851

Merged
merged 2 commits into from
Jan 29, 2018
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
17 changes: 13 additions & 4 deletions aws/data_source_aws_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func dataSourceAwsNetworkInterface() *schema.Resource {
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
},
"filter": dataSourceFiltersSchema(),
"association": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -132,20 +134,27 @@ func dataSourceAwsNetworkInterface() *schema.Resource {
func dataSourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

input := &ec2.DescribeNetworkInterfacesInput{
NetworkInterfaceIds: []*string{aws.String(d.Get("id").(string))},
input := &ec2.DescribeNetworkInterfacesInput{}
if v, ok := d.GetOk("id"); ok {
input.NetworkInterfaceIds = []*string{aws.String(v.(string))}
}

if v, ok := d.GetOk("filter"); ok {
input.Filters = buildAwsDataSourceFilters(v.(*schema.Set))
}

log.Printf("[DEBUG] Reading Network Interface: %s", input)
resp, err := conn.DescribeNetworkInterfaces(input)
if err != nil {
return err
}

if resp == nil || len(resp.NetworkInterfaces) == 0 {
return fmt.Errorf("no matching network interface found")
}

if len(resp.NetworkInterfaces) > 1 {
return fmt.Errorf("multiple network interfaces matched %s", d.Id())
return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria")
}

eni := resp.NetworkInterfaces[0]
Expand Down
51 changes: 51 additions & 0 deletions aws/data_source_aws_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,54 @@ data "aws_network_interface" "test" {
}
`, rName)
}

func TestAccDataSourceAwsNetworkInterface_filters(t *testing.T) {
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterface_filters(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_network_interface.test", "private_ips.#", "1"),
resource.TestCheckResourceAttr("data.aws_network_interface.test", "security_groups.#", "1"),
),
},
},
})
}

func testAccDataSourceAwsNetworkInterface_filters(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "test" {
cidr_block = "10.0.0.0/24"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_security_group" "test" {
name = "tf-sg-%s"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_network_interface" "test" {
subnet_id = "${aws_subnet.test.id}"
private_ips = ["10.0.0.60"]
security_groups = ["${aws_security_group.test.id}"]
}

data "aws_network_interface" "test" {
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs depends_on = ["aws_network_interface.test"] otherwise it fails due to dependency ordering:

--- FAIL: TestAccDataSourceAwsNetworkInterface_filters (47.96s)
	testing.go:503: Step 0 error: Error applying: 1 error(s) occurred:

		* data.aws_network_interface.test: data.aws_network_interface.test: no matching network interface found

filter {
name = "network-interface-id"
values = ["${aws_network_interface.test.id}"]
}
}
`, rName)
}
3 changes: 2 additions & 1 deletion website/docs/d/network_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ data "aws_network_interface" "bar" {

The following arguments are supported:

* `id` – (Required) The identifier for the network interface.
* `id` – (Optional) The identifier for the network interface.
* `filter` – (Optional) One or more name/value pairs to filter off of. There are several valid keys, for a full reference, check out [describe-network-interfaces](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-network-interfaces.html) in the AWS CLI reference.

## Attributes Reference

Expand Down