Skip to content

Commit

Permalink
Merge pull request #27364 from yfauser/f-aws_vpc_network_interface_at…
Browse files Browse the repository at this point in the history
…achment-import

f/aws vpc network interface attachment import
  • Loading branch information
gdavison authored Oct 21, 2022
2 parents e997ba3 + 2e32d32 commit 246a7d8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/27364.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_network_interface_attachment: Added import capabilities for the resource
```
20 changes: 20 additions & 0 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,26 @@ func FindNetworkInterfacesByAttachmentInstanceOwnerIDAndDescriptionWithContext(c
return FindNetworkInterfacesWithContext(ctx, conn, input)
}

func FindNetworkInterfaceByAttachmentID(ctx context.Context, conn *ec2.EC2, id string) (*ec2.NetworkInterface, error) {
input := &ec2.DescribeNetworkInterfacesInput{
Filters: BuildAttributeFilterList(map[string]string{
"attachment.attachment-id": id,
}),
}

networkInterface, err := FindNetworkInterfaceWithContext(ctx, conn, input)

if err != nil {
return nil, err
}

if networkInterface == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return networkInterface, nil
}

func FindNetworkInterfaceAttachmentByID(ctx context.Context, conn *ec2.EC2, id string) (*ec2.NetworkInterfaceAttachment, error) {
input := &ec2.DescribeNetworkInterfacesInput{
Filters: BuildAttributeFilterList(map[string]string{
Expand Down
14 changes: 9 additions & 5 deletions internal/service/ec2/vpc_network_interface_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func ResourceNetworkInterfaceAttachment() *schema.Resource {
Create: resourceNetworkInterfaceAttachmentCreate,
Read: resourceNetworkInterfaceAttachmentRead,
Delete: resourceNetworkInterfaceAttachmentDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"attachment_id": {
Expand Down Expand Up @@ -69,7 +72,7 @@ func resourceNetworkInterfaceAttachmentCreate(d *schema.ResourceData, meta inter
func resourceNetworkInterfaceAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EC2Conn

attachment, err := FindNetworkInterfaceAttachmentByID(context.TODO(), conn, d.Id())
network_interface, err := FindNetworkInterfaceByAttachmentID(context.TODO(), conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EC2 Network Interface Attachment (%s) not found, removing from state", d.Id())
Expand All @@ -81,10 +84,11 @@ func resourceNetworkInterfaceAttachmentRead(d *schema.ResourceData, meta interfa
return fmt.Errorf("error reading EC2 Network Interface Attachment (%s): %w", d.Id(), err)
}

d.Set("attachment_id", attachment.AttachmentId)
d.Set("device_index", attachment.DeviceIndex)
d.Set("instance_id", attachment.InstanceId)
d.Set("status", attachment.Status)
d.Set("network_interface_id", network_interface.NetworkInterfaceId)
d.Set("attachment_id", network_interface.Attachment.AttachmentId)
d.Set("device_index", network_interface.Attachment.DeviceIndex)
d.Set("instance_id", network_interface.Attachment.InstanceId)
d.Set("status", network_interface.Attachment.Status)

return nil
}
Expand Down
5 changes: 5 additions & 0 deletions internal/service/ec2/vpc_network_interface_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func TestAccVPCNetworkInterfaceAttachment_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "status"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/network_interface_attachment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ In addition to all arguments above, the following attributes are exported:
* `network_interface_id` - Network interface ID.
* `attachment_id` - The ENI Attachment ID.
* `status` - The status of the Network Interface Attachment.

## Import

Elastic network interface (ENI) Attachments can be imported using its Attachment ID e.g.,

```
terraform import aws_network_interface_attachment.secondary_nic eni-attach-0a33842b4ec347c4c
```

0 comments on commit 246a7d8

Please sign in to comment.