tweaks in new aws network interface

This commit is contained in:
Peter Beams 2015-03-06 09:29:25 +00:00
parent 42aaee3e92
commit 39346e6f16
1 changed files with 33 additions and 23 deletions

View File

@ -2,17 +2,11 @@ package aws
import ( import (
"bytes" "bytes"
//"crypto/sha1"
//"encoding/hex"
"fmt" "fmt"
"log" "log"
//"strconv"
//"strings"
//"time"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/hashcode"
//"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/hashicorp/aws-sdk-go/gen/ec2"
) )
@ -67,6 +61,7 @@ func resourceAwsNetworkInterface() *schema.Resource {
}, },
"attachment_id": &schema.Schema{ "attachment_id": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Computed: true,
}, },
}, },
}, },
@ -136,6 +131,24 @@ func resourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
return nil return nil
} }
func resourceAwsNetworkInterfaceDetach(oa *schema.Set, meta interface{}) error {
// if there was an old attachment, remove it
if oa != nil && len(oa.List()) > 0 {
old_attachment := oa.List()[0].(map[string]interface{})
detach_request := &ec2.DetachNetworkInterfaceRequest{
AttachmentID: aws.String(old_attachment["attachment_id"].(string)),
Force: aws.Boolean(true),
}
ec2conn := meta.(*AWSClient).ec2conn2
detach_err := ec2conn.DetachNetworkInterface(detach_request)
if detach_err != nil {
return fmt.Errorf("Error detaching ENI: %s", detach_err)
}
}
return nil
}
func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(true) d.Partial(true)
@ -144,17 +157,9 @@ func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{})
ec2conn := meta.(*AWSClient).ec2conn2 ec2conn := meta.(*AWSClient).ec2conn2
oa, na := d.GetChange("attachment") oa, na := d.GetChange("attachment")
// if there was an old attachment, remove it detach_err := resourceAwsNetworkInterfaceDetach(oa.(*schema.Set), meta)
if oa != nil && len(oa.(*schema.Set).List()) > 0 { if detach_err != nil {
old_attachment := oa.(*schema.Set).List()[0].(map[string]interface{}) return detach_err
detach_request := &ec2.DetachNetworkInterfaceRequest{
AttachmentID: aws.String(old_attachment["attachment_id"].(string)),
Force: aws.Boolean(true),
}
detach_err := ec2conn.DetachNetworkInterface(detach_request)
if detach_err != nil {
return fmt.Errorf("Error detaching ENI: %s", detach_err)
}
} }
// if there is a new attachment, attach it // if there is a new attachment, attach it
@ -199,6 +204,11 @@ func resourceAwsNetworkInterfaceDelete(d *schema.ResourceData, meta interface{})
log.Printf("[INFO] Deleting ENI: %s", d.Id()) log.Printf("[INFO] Deleting ENI: %s", d.Id())
detach_err := resourceAwsNetworkInterfaceDetach(d.Get("attachment").(*schema.Set), meta)
if detach_err != nil {
return detach_err
}
deleteEniOpts := ec2.DeleteNetworkInterfaceRequest{ deleteEniOpts := ec2.DeleteNetworkInterfaceRequest{
NetworkInterfaceID: aws.String(d.Id()), NetworkInterfaceID: aws.String(d.Id()),
} }
@ -246,8 +256,8 @@ func convertToPrivateIPAddresses(ips []interface{}) []ec2.PrivateIPAddressSpecif
func resourceAwsEniAttachmentHash(v interface{}) int { func resourceAwsEniAttachmentHash(v interface{}) int {
var buf bytes.Buffer var buf bytes.Buffer
m := v.(map[string]interface{}) m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%d-", m["instance"].(string))) buf.WriteString(fmt.Sprintf("%s-", m["instance"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["device_index"].(int))) buf.WriteString(fmt.Sprintf("%d-", m["device_index"].(int)))
return hashcode.String(buf.String()) return hashcode.String(buf.String())
} }