Skip to content

Commit

Permalink
Add filters in network interface data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Gufran committed Jan 29, 2018
1 parent cfa90cd commit d70c1b9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
27 changes: 23 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,9 @@ func dataSourceAwsNetworkInterface() *schema.Resource {
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"filter": dataSourceFiltersSchema(),
"association": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -132,20 +133,38 @@ 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))},
filters, filtersOk := d.GetOk("filter")
id, idOk := d.GetOk("id")

if !idOk && !filtersOk {
return fmt.Errorf("One of id or filter must be assigned")
}

if idOk && !filtersOk && id.(string) == "" {
return fmt.Errorf("One of id or filter must be assigned")
}

input := &ec2.DescribeNetworkInterfacesInput{}
if idOk {
input.NetworkInterfaceIds = []*string{aws.String(id.(string))}
}

if filtersOk {
input.Filters = buildAwsDataSourceFilters(filters.(*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
94 changes: 94 additions & 0 deletions aws/data_source_aws_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -55,3 +56,96 @@ 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" {
filter {
name = "subnet-id"
values = ["${aws_subnet.test.id}"]
}
filter {
name = "group-id"
values = ["${aws_security_group.test.id}"]
}
depends_on = ["aws_network_interface.test"]
}
`, rName)
}

func TestAccDataSourceAwsNetworkInterface_error_required(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterface_error_required(),
ExpectError: regexp.MustCompile("One of id or filter must be assigned"),
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterface_error_required_non_empty_id(),
ExpectError: regexp.MustCompile("One of id or filter must be assigned"),
},
},
})
}

func testAccDataSourceAwsNetworkInterface_error_required() string {
return `data "aws_network_interface" "test" {}`
}

func testAccDataSourceAwsNetworkInterface_error_required_non_empty_id() string {
return `
data "aws_network_interface" "test" {
id = ""
}`
}
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

0 comments on commit d70c1b9

Please sign in to comment.