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

f/aws vpc network interface attachment import #27364

Merged
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
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
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
```