From 411db71d7018891b8370eaf59b66b47e4617a40c Mon Sep 17 00:00:00 2001 From: Michal Jankowski Date: Wed, 7 Dec 2016 17:55:11 -0800 Subject: [PATCH 1/3] - Add simple resource to attach ENI with instance - Add proper documentation --- builtin/providers/aws/provider.go | 1 + ...source_aws_network_interface_attachment.go | 116 ++++++++++++++++++ .../r/network_interface_attachment.markdown | 36 ++++++ 3 files changed, 153 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_network_interface_attachment.go create mode 100644 website/source/docs/providers/aws/r/network_interface_attachment.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 7b7aaabd5f0e..19367de98e02 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -298,6 +298,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(), diff --git a/builtin/providers/aws/resource_aws_network_interface_attachment.go b/builtin/providers/aws/resource_aws_network_interface_attachment.go new file mode 100644 index 000000000000..a3bd3342528c --- /dev/null +++ b/builtin/providers/aws/resource_aws_network_interface_attachment.go @@ -0,0 +1,116 @@ +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: resourceAwsNetworkInterfaceRead, + Delete: resourceAwsNetworkInterfaceAttachmentDelete, + + Schema: map[string]*schema.Schema{ + "device_index": &schema.Schema{ + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + + "instance_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "network_interface_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: 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("[WARN] 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 resourceAwsNetworkInterfaceRead(d, meta) +} + +func resourceAwsNetworkInterfaceAttachmentDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + network_interface_id := 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", network_interface_id) + stateConf := &resource.StateChangeConf{ + Pending: []string{"true"}, + Target: []string{"false"}, + Refresh: networkInterfaceAttachmentRefreshFunc(conn, network_interface_id), + 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) + } + + return nil +} diff --git a/website/source/docs/providers/aws/r/network_interface_attachment.markdown b/website/source/docs/providers/aws/r/network_interface_attachment.markdown new file mode 100644 index 000000000000..ae56d338bb98 --- /dev/null +++ b/website/source/docs/providers/aws/r/network_interface_attachment.markdown @@ -0,0 +1,36 @@ +--- +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. From 5f8b6091de381462c340bc946bfb317feb06ebd5 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Fri, 21 Apr 2017 15:42:18 -0400 Subject: [PATCH 2/3] 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 ``` --- ...source_aws_network_interface_attachment.go | 70 ++++++++++++-- ...ce_aws_network_interface_attacment_test.go | 92 +++++++++++++++++++ 2 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_network_interface_attacment_test.go diff --git a/builtin/providers/aws/resource_aws_network_interface_attachment.go b/builtin/providers/aws/resource_aws_network_interface_attachment.go index a3bd3342528c..c37b0d18fedc 100644 --- a/builtin/providers/aws/resource_aws_network_interface_attachment.go +++ b/builtin/providers/aws/resource_aws_network_interface_attachment.go @@ -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 diff --git a/builtin/providers/aws/resource_aws_network_interface_attacment_test.go b/builtin/providers/aws/resource_aws_network_interface_attacment_test.go new file mode 100644 index 000000000000..b6b1aa0eb4fa --- /dev/null +++ b/builtin/providers/aws/resource_aws_network_interface_attacment_test.go @@ -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) +} From 44714c094bc4dc2c80044011c16728ae9afe3a22 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Fri, 21 Apr 2017 15:45:33 -0400 Subject: [PATCH 3/3] expand docs --- .../docs/providers/aws/r/network_interface_attachment.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/source/docs/providers/aws/r/network_interface_attachment.markdown b/website/source/docs/providers/aws/r/network_interface_attachment.markdown index ae56d338bb98..f02978ab9e28 100644 --- a/website/source/docs/providers/aws/r/network_interface_attachment.markdown +++ b/website/source/docs/providers/aws/r/network_interface_attachment.markdown @@ -34,3 +34,5 @@ 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.