provider/aws: Adds aws_network_interface_attachment resource
``` $ make testacc TEST=./builtin/providers/aws TESTARGS="-run=TestAccAWSNetworkInterfaceAttachment_basic" ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/04/21 15:24:58 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSNetworkInterfaceAttachment_basic -timeout 120m === RUN TestAccAWSNetworkInterfaceAttachment_basic --- PASS: TestAccAWSNetworkInterfaceAttachment_basic (273.14s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 273.145s ```
This commit is contained in:
parent
9ef947b0c3
commit
5f8b6091de
|
@ -15,27 +15,37 @@ import (
|
|||
func resourceAwsNetworkInterfaceAttachment() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceAwsNetworkInterfaceAttachmentCreate,
|
||||
Read: resourceAwsNetworkInterfaceRead,
|
||||
Read: resourceAwsNetworkInterfaceAttachmentRead,
|
||||
Delete: resourceAwsNetworkInterfaceAttachmentDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"device_index": &schema.Schema{
|
||||
"device_index": {
|
||||
Type: schema.TypeInt,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"instance_id": &schema.Schema{
|
||||
"instance_id": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"network_interface_id": &schema.Schema{
|
||||
"network_interface_id": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"attachment_id": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"status": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +67,7 @@ func resourceAwsNetworkInterfaceAttachmentCreate(d *schema.ResourceData, meta in
|
|||
resp, err := conn.AttachNetworkInterface(opts)
|
||||
if err != nil {
|
||||
if awsErr, ok := err.(awserr.Error); ok {
|
||||
return fmt.Errorf("[WARN] Error attaching network interface (%s) to instance (%s), message: \"%s\", code: \"%s\"",
|
||||
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
|
||||
|
@ -79,13 +89,53 @@ func resourceAwsNetworkInterfaceAttachmentCreate(d *schema.ResourceData, meta in
|
|||
}
|
||||
|
||||
d.SetId(*resp.AttachmentId)
|
||||
return resourceAwsNetworkInterfaceRead(d, meta)
|
||||
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
|
||||
|
||||
network_interface_id := d.Get("network_interface_id").(string)
|
||||
interfaceId := d.Get("network_interface_id").(string)
|
||||
|
||||
detach_request := &ec2.DetachNetworkInterfaceInput{
|
||||
AttachmentId: aws.String(d.Id()),
|
||||
|
@ -99,17 +149,17 @@ func resourceAwsNetworkInterfaceAttachmentDelete(d *schema.ResourceData, meta in
|
|||
}
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Waiting for ENI (%s) to become dettached", network_interface_id)
|
||||
log.Printf("[DEBUG] Waiting for ENI (%s) to become dettached", interfaceId)
|
||||
stateConf := &resource.StateChangeConf{
|
||||
Pending: []string{"true"},
|
||||
Target: []string{"false"},
|
||||
Refresh: networkInterfaceAttachmentRefreshFunc(conn, network_interface_id),
|
||||
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", network_interface_id, err)
|
||||
"Error waiting for ENI (%s) to become dettached: %s", interfaceId, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue