2015-03-03 02:33:42 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-03-17 14:00:36 +01:00
|
|
|
"bytes"
|
2015-03-03 02:33:42 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-03-06 16:49:56 +01:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2015-03-17 14:00:36 +01:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-03-17 14:00:36 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2015-03-06 16:49:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-03-03 02:33:42 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsNetworkInterface() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsNetworkInterfaceCreate,
|
|
|
|
Read: resourceAwsNetworkInterfaceRead,
|
|
|
|
Update: resourceAwsNetworkInterfaceUpdate,
|
|
|
|
Delete: resourceAwsNetworkInterfaceDelete,
|
|
|
|
|
2015-03-17 14:00:36 +01:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
2015-03-03 02:33:42 +01:00
|
|
|
"subnet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-03-17 14:00:36 +01:00
|
|
|
Required: true,
|
2015-03-03 02:33:42 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"private_ips": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2015-03-17 14:00:36 +01:00
|
|
|
ForceNew: true,
|
2015-07-15 21:09:35 +02:00
|
|
|
Computed: true,
|
2015-03-03 02:33:42 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2015-03-03 02:33:42 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"security_groups": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2015-03-17 14:00:36 +01:00
|
|
|
Computed: true,
|
2015-03-03 02:33:42 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2015-03-03 02:33:42 +01:00
|
|
|
},
|
2015-03-03 16:30:10 +01:00
|
|
|
|
2015-07-15 20:42:32 +02:00
|
|
|
"source_dest_check": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
|
|
|
|
2015-03-03 16:30:10 +01:00
|
|
|
"attachment": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"instance": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"device_index": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"attachment_id": &schema.Schema{
|
2015-03-17 14:00:36 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
2015-03-03 16:30:10 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsEniAttachmentHash,
|
|
|
|
},
|
|
|
|
|
2015-03-17 14:00:36 +01:00
|
|
|
"tags": tagsSchema(),
|
2015-03-03 02:33:42 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-03-17 14:00:36 +01:00
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-03 02:33:42 +01:00
|
|
|
|
2015-04-07 19:07:12 +02:00
|
|
|
request := &ec2.CreateNetworkInterfaceInput{
|
2015-06-04 17:44:33 +02:00
|
|
|
SubnetID: aws.String(d.Get("subnet_id").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
security_groups := d.Get("security_groups").(*schema.Set).List()
|
|
|
|
if len(security_groups) != 0 {
|
|
|
|
request.Groups = expandStringList(security_groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
private_ips := d.Get("private_ips").(*schema.Set).List()
|
|
|
|
if len(private_ips) != 0 {
|
|
|
|
request.PrivateIPAddresses = expandPrivateIPAddesses(private_ips)
|
2015-03-03 02:33:42 +01:00
|
|
|
}
|
2015-03-17 14:00:36 +01:00
|
|
|
|
2015-03-03 02:33:42 +01:00
|
|
|
log.Printf("[DEBUG] Creating network interface")
|
2015-04-07 19:07:12 +02:00
|
|
|
resp, err := conn.CreateNetworkInterface(request)
|
2015-03-03 02:33:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating ENI: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-17 13:42:05 +01:00
|
|
|
d.SetId(*resp.NetworkInterface.NetworkInterfaceID)
|
2015-03-03 13:57:11 +01:00
|
|
|
log.Printf("[INFO] ENI ID: %s", d.Id())
|
2015-03-17 14:00:36 +01:00
|
|
|
return resourceAwsNetworkInterfaceUpdate(d, meta)
|
2015-03-03 02:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-04-07 19:07:12 +02:00
|
|
|
describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
|
|
|
|
NetworkInterfaceIDs: []*string{aws.String(d.Id())},
|
2015-03-03 02:33:42 +01:00
|
|
|
}
|
2015-04-07 19:07:12 +02:00
|
|
|
describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
|
2015-03-03 02:33:42 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" {
|
2015-03-03 13:57:11 +01:00
|
|
|
// The ENI is gone now, so just remove it from the state
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-17 14:00:36 +01:00
|
|
|
|
2015-03-03 13:57:11 +01:00
|
|
|
return fmt.Errorf("Error retrieving ENI: %s", err)
|
2015-03-03 02:33:42 +01:00
|
|
|
}
|
2015-03-03 13:57:11 +01:00
|
|
|
if len(describeResp.NetworkInterfaces) != 1 {
|
|
|
|
return fmt.Errorf("Unable to find ENI: %#v", describeResp.NetworkInterfaces)
|
2015-03-03 02:33:42 +01:00
|
|
|
}
|
|
|
|
|
2015-03-03 13:57:11 +01:00
|
|
|
eni := describeResp.NetworkInterfaces[0]
|
|
|
|
d.Set("subnet_id", eni.SubnetID)
|
2015-04-16 22:28:18 +02:00
|
|
|
d.Set("private_ips", flattenNetworkInterfacesPrivateIPAddesses(eni.PrivateIPAddresses))
|
|
|
|
d.Set("security_groups", flattenGroupIdentifiers(eni.Groups))
|
2015-07-15 20:42:32 +02:00
|
|
|
d.Set("source_dest_check", eni.SourceDestCheck)
|
2015-03-03 16:30:10 +01:00
|
|
|
|
2015-03-19 17:07:01 +01:00
|
|
|
// Tags
|
2015-05-12 21:58:10 +02:00
|
|
|
d.Set("tags", tagsToMap(eni.TagSet))
|
2015-03-19 17:07:01 +01:00
|
|
|
|
2015-03-03 16:30:10 +01:00
|
|
|
if eni.Attachment != nil {
|
2015-04-16 22:28:18 +02:00
|
|
|
attachment := []map[string]interface{}{flattenAttachment(eni.Attachment)}
|
2015-03-17 14:00:36 +01:00
|
|
|
d.Set("attachment", attachment)
|
2015-03-03 16:30:10 +01:00
|
|
|
} else {
|
|
|
|
d.Set("attachment", nil)
|
|
|
|
}
|
2015-03-17 14:00:36 +01:00
|
|
|
|
2015-03-03 02:33:42 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:07:12 +02:00
|
|
|
func networkInterfaceAttachmentRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
2015-03-06 16:49:56 +01:00
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
2015-04-07 19:07:12 +02:00
|
|
|
describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
|
|
|
|
NetworkInterfaceIDs: []*string{aws.String(id)},
|
2015-03-06 16:49:56 +01:00
|
|
|
}
|
2015-04-07 19:07:12 +02:00
|
|
|
describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
|
2015-03-06 16:49:56 +01:00
|
|
|
|
2015-03-17 14:00:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Could not find network interface %s. %s", id, err)
|
2015-03-06 16:49:56 +01:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
eni := describeResp.NetworkInterfaces[0]
|
2015-03-17 14:00:36 +01:00
|
|
|
hasAttachment := strconv.FormatBool(eni.Attachment != nil)
|
|
|
|
log.Printf("[DEBUG] ENI %s has attachment state %s", id, hasAttachment)
|
2015-03-06 16:49:56 +01:00
|
|
|
return eni, hasAttachment, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsNetworkInterfaceDetach(oa *schema.Set, meta interface{}, eniId string) error {
|
2015-03-06 10:29:25 +01:00
|
|
|
// if there was an old attachment, remove it
|
2015-03-17 14:00:36 +01:00
|
|
|
if oa != nil && len(oa.List()) > 0 {
|
2015-03-06 10:29:25 +01:00
|
|
|
old_attachment := oa.List()[0].(map[string]interface{})
|
2015-04-07 19:07:12 +02:00
|
|
|
detach_request := &ec2.DetachNetworkInterfaceInput{
|
2015-03-17 14:00:36 +01:00
|
|
|
AttachmentID: aws.String(old_attachment["attachment_id"].(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
Force: aws.Bool(true),
|
2015-03-06 10:29:25 +01:00
|
|
|
}
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-04-07 19:07:12 +02:00
|
|
|
_, detach_err := conn.DetachNetworkInterface(detach_request)
|
2015-03-06 10:29:25 +01:00
|
|
|
if detach_err != nil {
|
|
|
|
return fmt.Errorf("Error detaching ENI: %s", detach_err)
|
|
|
|
}
|
2015-03-06 16:49:56 +01:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for ENI (%s) to become dettached", eniId)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"true"},
|
|
|
|
Target: "false",
|
2015-04-07 19:07:12 +02:00
|
|
|
Refresh: networkInterfaceAttachmentRefreshFunc(conn, eniId),
|
2015-03-06 16:49:56 +01:00
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
2015-03-17 14:00:36 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for ENI (%s) to become dettached: %s", eniId, err)
|
|
|
|
}
|
2015-03-06 10:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-03 02:33:42 +01:00
|
|
|
func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-03 13:57:11 +01:00
|
|
|
d.Partial(true)
|
|
|
|
|
2015-03-03 16:30:10 +01:00
|
|
|
if d.HasChange("attachment") {
|
2015-03-17 14:00:36 +01:00
|
|
|
oa, na := d.GetChange("attachment")
|
|
|
|
|
2015-03-06 16:49:56 +01:00
|
|
|
detach_err := resourceAwsNetworkInterfaceDetach(oa.(*schema.Set), meta, d.Id())
|
2015-03-06 10:29:25 +01:00
|
|
|
if detach_err != nil {
|
|
|
|
return detach_err
|
2015-03-03 16:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a new attachment, attach it
|
2015-03-17 14:00:36 +01:00
|
|
|
if na != nil && len(na.(*schema.Set).List()) > 0 {
|
2015-03-03 16:30:10 +01:00
|
|
|
new_attachment := na.(*schema.Set).List()[0].(map[string]interface{})
|
2015-04-07 19:07:12 +02:00
|
|
|
di := new_attachment["device_index"].(int)
|
|
|
|
attach_request := &ec2.AttachNetworkInterfaceInput{
|
2015-07-28 22:29:46 +02:00
|
|
|
DeviceIndex: aws.Int64(int64(di)),
|
2015-03-17 14:00:36 +01:00
|
|
|
InstanceID: aws.String(new_attachment["instance"].(string)),
|
|
|
|
NetworkInterfaceID: aws.String(d.Id()),
|
2015-03-03 16:30:10 +01:00
|
|
|
}
|
2015-04-07 19:07:12 +02:00
|
|
|
_, attach_err := conn.AttachNetworkInterface(attach_request)
|
2015-03-03 16:30:10 +01:00
|
|
|
if attach_err != nil {
|
|
|
|
return fmt.Errorf("Error attaching ENI: %s", attach_err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("attachment")
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:42:32 +02:00
|
|
|
request := &ec2.ModifyNetworkInterfaceAttributeInput{
|
|
|
|
NetworkInterfaceID: aws.String(d.Id()),
|
2015-07-28 22:29:46 +02:00
|
|
|
SourceDestCheck: &ec2.AttributeBooleanValue{Value: aws.Bool(d.Get("source_dest_check").(bool))},
|
2015-07-15 20:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.ModifyNetworkInterfaceAttribute(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure updating ENI: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("source_dest_check")
|
|
|
|
|
2015-03-03 13:57:11 +01:00
|
|
|
if d.HasChange("security_groups") {
|
2015-04-07 19:07:12 +02:00
|
|
|
request := &ec2.ModifyNetworkInterfaceAttributeInput{
|
2015-03-17 14:00:36 +01:00
|
|
|
NetworkInterfaceID: aws.String(d.Id()),
|
2015-04-16 22:28:18 +02:00
|
|
|
Groups: expandStringList(d.Get("security_groups").(*schema.Set).List()),
|
2015-03-03 13:57:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 19:07:12 +02:00
|
|
|
_, err := conn.ModifyNetworkInterfaceAttribute(request)
|
2015-03-03 13:57:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure updating ENI: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("security_groups")
|
|
|
|
}
|
|
|
|
|
2015-05-12 21:58:10 +02:00
|
|
|
if err := setTags(conn, d); err != nil {
|
2015-03-19 17:07:01 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
|
2015-03-03 13:57:11 +01:00
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceAwsNetworkInterfaceRead(d, meta)
|
2015-03-03 02:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsNetworkInterfaceDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-03 13:57:11 +01:00
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting ENI: %s", d.Id())
|
|
|
|
|
2015-03-06 16:49:56 +01:00
|
|
|
detach_err := resourceAwsNetworkInterfaceDetach(d.Get("attachment").(*schema.Set), meta, d.Id())
|
2015-03-06 10:29:25 +01:00
|
|
|
if detach_err != nil {
|
|
|
|
return detach_err
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:07:12 +02:00
|
|
|
deleteEniOpts := ec2.DeleteNetworkInterfaceInput{
|
2015-03-03 13:57:11 +01:00
|
|
|
NetworkInterfaceID: aws.String(d.Id()),
|
|
|
|
}
|
2015-04-07 19:07:12 +02:00
|
|
|
if _, err := conn.DeleteNetworkInterface(&deleteEniOpts); err != nil {
|
2015-03-03 13:57:11 +01:00
|
|
|
return fmt.Errorf("Error deleting ENI: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-03 02:33:42 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-03 16:30:10 +01:00
|
|
|
func resourceAwsEniAttachmentHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
2015-03-06 10:29:25 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["instance"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["device_index"].(int)))
|
2015-03-03 16:30:10 +01:00
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|