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

provider/aws: Adds aws_network_interface_attachment resource #13861

Merged
merged 4 commits into from
Apr 24, 2017
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
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ func Provider() terraform.ResourceProvider {
"aws_default_route_table": resourceAwsDefaultRouteTable(),
"aws_network_acl_rule": resourceAwsNetworkAclRule(),
"aws_network_interface": resourceAwsNetworkInterface(),
"aws_network_interface_attachment": resourceAwsNetworkInterfaceAttachment(),
"aws_opsworks_application": resourceAwsOpsworksApplication(),
"aws_opsworks_stack": resourceAwsOpsworksStack(),
"aws_opsworks_java_app_layer": resourceAwsOpsworksJavaAppLayer(),
Expand Down
166 changes: 166 additions & 0 deletions builtin/providers/aws/resource_aws_network_interface_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsNetworkInterfaceAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsNetworkInterfaceAttachmentCreate,
Read: resourceAwsNetworkInterfaceAttachmentRead,
Delete: resourceAwsNetworkInterfaceAttachmentDelete,

Schema: map[string]*schema.Schema{
"device_index": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},

"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"network_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"attachment_id": {
Type: schema.TypeString,
Computed: true,
},

"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsNetworkInterfaceAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

device_index := d.Get("device_index").(int)
instance_id := d.Get("instance_id").(string)
network_interface_id := d.Get("network_interface_id").(string)

opts := &ec2.AttachNetworkInterfaceInput{
DeviceIndex: aws.Int64(int64(device_index)),
InstanceId: aws.String(instance_id),
NetworkInterfaceId: aws.String(network_interface_id),
}

log.Printf("[DEBUG] Attaching network interface (%s) to instance (%s)", network_interface_id, instance_id)
resp, err := conn.AttachNetworkInterface(opts)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
return fmt.Errorf("Error attaching network interface (%s) to instance (%s), message: \"%s\", code: \"%s\"",
network_interface_id, instance_id, awsErr.Message(), awsErr.Code())
}
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{"false"},
Target: []string{"true"},
Refresh: networkInterfaceAttachmentRefreshFunc(conn, network_interface_id),
Timeout: 5 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for Volume (%s) to attach to Instance: %s, error: %s", network_interface_id, instance_id, err)
}

d.SetId(*resp.AttachmentId)
return resourceAwsNetworkInterfaceAttachmentRead(d, meta)
}

func resourceAwsNetworkInterfaceAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

interfaceId := d.Get("network_interface_id").(string)

req := &ec2.DescribeNetworkInterfacesInput{
NetworkInterfaceIds: []*string{aws.String(interfaceId)},
}

resp, err := conn.DescribeNetworkInterfaces(req)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" {
// The ENI is gone now, so just remove the attachment from the state
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving ENI: %s", err)
}
if len(resp.NetworkInterfaces) != 1 {
return fmt.Errorf("Unable to find ENI (%s): %#v", interfaceId, resp.NetworkInterfaces)
}

eni := resp.NetworkInterfaces[0]

if eni.Attachment == nil {
// Interface is no longer attached, remove from state
d.SetId("")
return nil
}

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

return nil
}

func resourceAwsNetworkInterfaceAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

interfaceId := d.Get("network_interface_id").(string)

detach_request := &ec2.DetachNetworkInterfaceInput{
AttachmentId: aws.String(d.Id()),
Force: aws.Bool(true),
}

_, detach_err := conn.DetachNetworkInterface(detach_request)
if detach_err != nil {
if awsErr, _ := detach_err.(awserr.Error); awsErr.Code() != "InvalidAttachmentID.NotFound" {
return fmt.Errorf("Error detaching ENI: %s", detach_err)
}
}

log.Printf("[DEBUG] Waiting for ENI (%s) to become dettached", interfaceId)
stateConf := &resource.StateChangeConf{
Pending: []string{"true"},
Target: []string{"false"},
Refresh: networkInterfaceAttachmentRefreshFunc(conn, interfaceId),
Timeout: 10 * time.Minute,
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for ENI (%s) to become dettached: %s", interfaceId, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSNetworkInterfaceAttachment_basic(t *testing.T) {
var conf ec2.NetworkInterface
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_network_interface.bar",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSENIDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSNetworkInterfaceAttachmentConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
resource.TestCheckResourceAttr(
"aws_network_interface_attachment.test", "device_index", "1"),
resource.TestCheckResourceAttrSet(
"aws_network_interface_attachment.test", "instance_id"),
resource.TestCheckResourceAttrSet(
"aws_network_interface_attachment.test", "network_interface_id"),
resource.TestCheckResourceAttrSet(
"aws_network_interface_attachment.test", "attachment_id"),
resource.TestCheckResourceAttrSet(
"aws_network_interface_attachment.test", "status"),
),
},
},
})
}

func testAccAWSNetworkInterfaceAttachmentConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "foo" {
cidr_block = "172.16.0.0/16"
}

resource "aws_subnet" "foo" {
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "172.16.10.0/24"
availability_zone = "us-west-2a"
}

resource "aws_security_group" "foo" {
vpc_id = "${aws_vpc.foo.id}"
description = "foo"
name = "foo-%d"

egress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
}

resource "aws_network_interface" "bar" {
subnet_id = "${aws_subnet.foo.id}"
private_ips = ["172.16.10.100"]
security_groups = ["${aws_security_group.foo.id}"]
description = "Managed by Terraform"
tags {
Name = "bar_interface"
}
}

resource "aws_instance" "foo" {
ami = "ami-c5eabbf5"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.foo.id}"
tags {
Name = "foo-%d"
}
}

resource "aws_network_interface_attachment" "test" {
device_index = 1
instance_id = "${aws_instance.foo.id}"
network_interface_id = "${aws_network_interface.bar.id}"
}
`, rInt, rInt)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "aws"
page_title: "AWS: aws_network_interface_attachment"
sidebar_current: "docs-aws-resource-network-interface-attachment"
description: |-
Attach an Elastic network interface (ENI) resource with EC2 instance.
---

# aws\_network\_interface\_attachment

Attach an Elastic network interface (ENI) resource with EC2 instance.

## Example Usage

```
resource "aws_network_interface_attachment" "test" {
instance_id = "${aws_instance.test.id}"
network_interface_id = "${aws_network_interface.test.id}"
device_index = 0
}
```

## Argument Reference

The following arguments are supported:

* `instance_id` - (Required) Instance ID to attach.
* `network_interface_id` - (Required) ENI ID to attach.
* `device_index` - (Required) Network interface index (int).

## Attributes Reference

The following attributes are exported:

* `instance_id` - Instance ID.
* `network_interface_id` - Network interface ID.
* `attachment_id` - The ENI Attachment ID.
* `status` - The status of the Network Interface Attachment.