-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
New Datasource: aws_network_interface #2316
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsNetworkInterface() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsNetworkInterfaceRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"association": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"allocation_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"association_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ip_owner_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_dns_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_ip": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"attachment": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there's only ever going to be a single attachment I think this field should be TypeList so it's possible to reference it like |
||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"attachment_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"device_index": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"instance_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"instance_owner_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"availability_zone": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"security_groups": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"interface_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ipv6_addresses": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"mac_address": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"owner_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"private_dns_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"private_ip": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"private_ips": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to keep this list of IPs Set? It looks like a field people would like to reference and Set would prevent them in doing so due to computed index. That said I'm not sure if the ordering of IPs has any meaning - i.e. if they're sorted by networking interfaces (primary first, then others)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used |
||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"requester_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"subnet_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchemaComputed(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
input := &ec2.DescribeNetworkInterfacesInput{ | ||
NetworkInterfaceIds: []*string{aws.String(d.Get("id").(string))}, | ||
} | ||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mind improving the error message here to also include the ID we looked for? e.g. |
||
} | ||
|
||
eni := resp.NetworkInterfaces[0] | ||
|
||
d.SetId(*eni.NetworkInterfaceId) | ||
if eni.Association != nil { | ||
d.Set("association", flattenEc2NetworkInterfaceAssociation(eni.Association)) | ||
} | ||
if eni.Attachment != nil { | ||
attachment := []map[string]interface{}{flattenAttachment(eni.Attachment)} | ||
d.Set("attachment", attachment) | ||
} | ||
d.Set("availability_zone", eni.AvailabilityZone) | ||
d.Set("description", eni.Description) | ||
d.Set("security_groups", flattenGroupIdentifiers(eni.Groups)) | ||
d.Set("interface_type", eni.InterfaceType) | ||
d.Set("ipv6_addresses", flattenEc2NetworkInterfaceIpv6Address(eni.Ipv6Addresses)) | ||
d.Set("mac_address", eni.MacAddress) | ||
d.Set("owner_id", eni.OwnerId) | ||
d.Set("private_dns_name", eni.PrivateDnsName) | ||
d.Set("private_id", eni.PrivateIpAddress) | ||
d.Set("private_ips", flattenNetworkInterfacesPrivateIPAddresses(eni.PrivateIpAddresses)) | ||
d.Set("requester_id", eni.RequesterId) | ||
d.Set("subnet_id", eni.SubnetId) | ||
d.Set("vpc_id", eni.VpcId) | ||
d.Set("tags", tagsToMap(eni.TagSet)) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAwsNetworkInterface_basic(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsNetworkInterface_basic(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_basic(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.50"] | ||
security_groups = ["${aws_security_group.test.id}"] | ||
} | ||
|
||
data "aws_network_interface" "test" { | ||
id = "${aws_network_interface.test.id}" | ||
} | ||
`, rName) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_network_interface" | ||
sidebar_current: "docs-aws-datasource-network-interface" | ||
description: |- | ||
Get information on anNetwork Interface resource. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on a Network Interface |
||
--- | ||
|
||
# aws_network_interface | ||
|
||
Use this data source to get information about a Network Interface. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_network_interface" "bar" { | ||
id = "eni-01234567" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `id` – (Required) The identifier for the network interface. | ||
|
||
## Attributes Reference | ||
|
||
See the [Network Interface](/docs/providers/aws/r/network_interface.html) for details on the returned attributes. | ||
|
||
Additionally, the following attributes are exported: | ||
|
||
* `association` - The association information for an Elastic IP address (IPv4) associated with the network interface. See supported fields below. | ||
* `availability_zone` - The Availability Zone. | ||
* `interface_type` - The type of interface. | ||
* `ipv6_addresses` - List of IPv6 addresses to assign to the ENI. | ||
* `mac_address` - The MAC address. | ||
* `owner_id` - The AWS account ID of the owner of the network interface. | ||
* `requester_id` - The ID of the entity that launched the instance on your behalf. | ||
|
||
### `association` | ||
|
||
* `allocation_id` - The allocation ID. | ||
* `association_id` - The association ID. | ||
* `ip_owner_id` - The ID of the Elastic IP address owner. | ||
* `public_dns_name` - The public DNS name. | ||
* `public_ip` - The address of the Elastic IP address bound to the network interface. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there's only ever going to be a single association I think this field should be TypeList so it's possible to reference it like
data.aws_network_interface.name.association.0.public_ip
etc.