2014-07-07 21:19:40 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-07-28 18:04:58 +02:00
|
|
|
"strings"
|
2014-09-09 23:18:53 +02:00
|
|
|
"time"
|
2014-07-07 21:19:40 +02: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"
|
2014-09-09 23:18:53 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-08-20 01:56:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-07 21:19:40 +02:00
|
|
|
)
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
func resourceAwsEip() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsEipCreate,
|
|
|
|
Read: resourceAwsEipRead,
|
|
|
|
Update: resourceAwsEipUpdate,
|
|
|
|
Delete: resourceAwsEipDelete,
|
2016-05-04 20:24:45 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourceAwsEipImportState,
|
|
|
|
},
|
2014-08-20 01:56:23 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"vpc": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-10-12 22:50:04 +02:00
|
|
|
Computed: true,
|
2014-08-20 01:56:23 +02:00
|
|
|
},
|
|
|
|
|
2015-04-26 04:30:37 +02:00
|
|
|
"network_interface": &schema.Schema{
|
2015-10-12 22:50:04 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-04-26 04:30:37 +02:00
|
|
|
},
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
"allocation_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-10-19 00:32:33 +02:00
|
|
|
"association_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
"domain": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
"public_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"private_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-05-06 22:38:39 +02:00
|
|
|
|
|
|
|
"associate_with_private_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2014-08-20 01:56:23 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-07 21:19:40 +02:00
|
|
|
|
|
|
|
// By default, we're not in a VPC
|
|
|
|
domainOpt := ""
|
2014-08-20 01:56:23 +02:00
|
|
|
if v := d.Get("vpc"); v != nil && v.(bool) {
|
2014-07-07 21:19:40 +02:00
|
|
|
domainOpt = "vpc"
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:37:17 +02:00
|
|
|
allocOpts := &ec2.AllocateAddressInput{
|
2015-03-03 18:45:27 +01:00
|
|
|
Domain: aws.String(domainOpt),
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] EIP create configuration: %#v", allocOpts)
|
2015-03-03 18:45:27 +01:00
|
|
|
allocResp, err := ec2conn.AllocateAddress(allocOpts)
|
2014-07-07 21:19:40 +02:00
|
|
|
if err != nil {
|
2014-08-20 01:56:23 +02:00
|
|
|
return fmt.Errorf("Error creating EIP: %s", err)
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
// The domain tells us if we're in a VPC or not
|
|
|
|
d.Set("domain", allocResp.Domain)
|
|
|
|
|
2014-07-07 21:19:40 +02:00
|
|
|
// Assign the eips (unique) allocation id for use later
|
|
|
|
// the EIP api has a conditional unique ID (really), so
|
|
|
|
// if we're in a VPC we need to save the ID as such, otherwise
|
|
|
|
// it defaults to using the public IP
|
2014-07-28 18:04:58 +02:00
|
|
|
log.Printf("[DEBUG] EIP Allocate: %#v", allocResp)
|
2014-08-20 02:22:25 +02:00
|
|
|
if d.Get("domain").(string) == "vpc" {
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*allocResp.AllocationId)
|
2014-07-07 21:19:40 +02:00
|
|
|
} else {
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*allocResp.PublicIp)
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:45:27 +01:00
|
|
|
log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), *allocResp.Domain)
|
2014-08-20 02:22:25 +02:00
|
|
|
return resourceAwsEipUpdate(d, meta)
|
2014-07-27 00:53:26 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
domain := resourceAwsEipDomain(d)
|
|
|
|
id := d.Id()
|
|
|
|
|
2015-04-26 04:30:37 +02:00
|
|
|
req := &ec2.DescribeAddressesInput{}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if domain == "vpc" {
|
2015-08-17 20:27:16 +02:00
|
|
|
req.AllocationIds = []*string{aws.String(id)}
|
2014-11-21 17:58:34 +01:00
|
|
|
} else {
|
2015-08-17 20:27:16 +02:00
|
|
|
req.PublicIps = []*string{aws.String(id)}
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
2015-04-27 23:26:52 +02:00
|
|
|
"[DEBUG] EIP describe configuration: %#v (domain: %s)",
|
|
|
|
req, domain)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2015-03-03 18:45:27 +01:00
|
|
|
describeAddresses, err := ec2conn.DescribeAddresses(req)
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAllocationID.NotFound" {
|
2014-11-21 17:58:34 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error retrieving EIP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify AWS returned our EIP
|
|
|
|
if len(describeAddresses.Addresses) != 1 ||
|
2015-10-08 14:48:04 +02:00
|
|
|
domain == "vpc" && *describeAddresses.Addresses[0].AllocationId != id ||
|
2015-08-17 20:27:16 +02:00
|
|
|
*describeAddresses.Addresses[0].PublicIp != id {
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
address := describeAddresses.Addresses[0]
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("association_id", address.AssociationId)
|
|
|
|
if address.InstanceId != nil {
|
|
|
|
d.Set("instance", address.InstanceId)
|
2015-12-16 23:29:40 +01:00
|
|
|
} else {
|
|
|
|
d.Set("instance", "")
|
2015-05-02 00:33:41 +02:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if address.NetworkInterfaceId != nil {
|
|
|
|
d.Set("network_interface", address.NetworkInterfaceId)
|
2015-12-16 23:29:40 +01:00
|
|
|
} else {
|
|
|
|
d.Set("network_interface", "")
|
2015-05-05 22:24:50 +02:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("private_ip", address.PrivateIpAddress)
|
|
|
|
d.Set("public_ip", address.PublicIp)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2016-04-22 07:17:14 +02:00
|
|
|
// On import (domain never set, which it must've been if we created),
|
|
|
|
// set the 'vpc' attribute depending on if we're in a VPC.
|
|
|
|
if _, ok := d.GetOk("domain"); !ok {
|
|
|
|
d.Set("vpc", *address.Domain == "vpc")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("domain", address.Domain)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-27 00:53:26 +02:00
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
domain := resourceAwsEipDomain(d)
|
2014-07-07 21:19:40 +02:00
|
|
|
|
2015-04-26 04:30:37 +02:00
|
|
|
// Associate to instance or interface if specified
|
|
|
|
v_instance, ok_instance := d.GetOk("instance")
|
|
|
|
v_interface, ok_interface := d.GetOk("network_interface")
|
|
|
|
|
|
|
|
if ok_instance || ok_interface {
|
|
|
|
instanceId := v_instance.(string)
|
|
|
|
networkInterfaceId := v_interface.(string)
|
2014-08-20 01:56:23 +02:00
|
|
|
|
2015-04-07 17:37:17 +02:00
|
|
|
assocOpts := &ec2.AssociateAddressInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
InstanceId: aws.String(instanceId),
|
|
|
|
PublicIp: aws.String(d.Id()),
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
2014-07-27 00:53:26 +02:00
|
|
|
|
2014-07-07 21:19:40 +02:00
|
|
|
// more unique ID conditionals
|
2014-08-20 02:22:25 +02:00
|
|
|
if domain == "vpc" {
|
2016-04-07 19:15:00 +02:00
|
|
|
var privateIpAddress *string
|
2016-05-06 22:38:39 +02:00
|
|
|
if v := d.Get("associate_with_private_ip").(string); v != "" {
|
2016-04-07 19:15:00 +02:00
|
|
|
privateIpAddress = aws.String(v)
|
|
|
|
}
|
2015-04-07 17:37:17 +02:00
|
|
|
assocOpts = &ec2.AssociateAddressInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
NetworkInterfaceId: aws.String(networkInterfaceId),
|
|
|
|
InstanceId: aws.String(instanceId),
|
|
|
|
AllocationId: aws.String(d.Id()),
|
2016-04-07 19:15:00 +02:00
|
|
|
PrivateIpAddress: privateIpAddress,
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
log.Printf("[DEBUG] EIP associate configuration: %#v (domain: %v)", assocOpts, domain)
|
2015-03-03 18:45:27 +01:00
|
|
|
_, err := ec2conn.AssociateAddress(assocOpts)
|
2014-07-07 21:19:40 +02:00
|
|
|
if err != nil {
|
2015-05-02 00:33:41 +02:00
|
|
|
// Prevent saving instance if association failed
|
|
|
|
// e.g. missing internet gateway in VPC
|
|
|
|
d.Set("instance", "")
|
2015-05-05 22:24:50 +02:00
|
|
|
d.Set("network_interface", "")
|
2015-04-26 04:30:37 +02:00
|
|
|
return fmt.Errorf("Failure associating EIP: %s", err)
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
return resourceAwsEipRead(d, meta)
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-10-19 00:32:33 +02:00
|
|
|
|
|
|
|
if err := resourceAwsEipRead(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.Id() == "" {
|
|
|
|
// This might happen from the read
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-26 04:30:37 +02:00
|
|
|
// If we are attached to an instance or interface, detach first.
|
|
|
|
if d.Get("instance").(string) != "" || d.Get("association_id").(string) != "" {
|
2014-10-19 00:32:33 +02:00
|
|
|
log.Printf("[DEBUG] Disassociating EIP: %s", d.Id())
|
|
|
|
var err error
|
|
|
|
switch resourceAwsEipDomain(d) {
|
|
|
|
case "vpc":
|
2015-04-07 17:37:17 +02:00
|
|
|
_, err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
AssociationId: aws.String(d.Get("association_id").(string)),
|
2015-03-03 18:45:27 +01:00
|
|
|
})
|
2014-10-19 00:32:33 +02:00
|
|
|
case "standard":
|
2015-04-07 17:37:17 +02:00
|
|
|
_, err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PublicIp: aws.String(d.Get("public_ip").(string)),
|
2015-03-03 18:45:27 +01:00
|
|
|
})
|
2014-10-19 00:32:33 +02:00
|
|
|
}
|
2015-06-29 07:30:43 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// First check if the association ID is not found. If this
|
|
|
|
// is the case, then it was already disassociated somehow,
|
|
|
|
// and that is okay. The most commmon reason for this is that
|
|
|
|
// the instance or ENI it was attached it was destroyed.
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAssociationID.NotFound" {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-19 00:32:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-15 00:30:31 +02:00
|
|
|
}
|
2014-10-19 00:32:33 +02:00
|
|
|
|
|
|
|
domain := resourceAwsEipDomain(d)
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.Retry(3*time.Minute, func() *resource.RetryError {
|
2014-10-19 00:32:33 +02:00
|
|
|
var err error
|
|
|
|
switch domain {
|
|
|
|
case "vpc":
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] EIP release (destroy) address allocation: %v",
|
|
|
|
d.Id())
|
2015-04-07 17:37:17 +02:00
|
|
|
_, err = ec2conn.ReleaseAddress(&ec2.ReleaseAddressInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
AllocationId: aws.String(d.Id()),
|
2015-03-03 18:45:27 +01:00
|
|
|
})
|
2014-10-19 00:32:33 +02:00
|
|
|
case "standard":
|
|
|
|
log.Printf("[DEBUG] EIP release (destroy) address: %v", d.Id())
|
2015-04-07 17:37:17 +02:00
|
|
|
_, err = ec2conn.ReleaseAddress(&ec2.ReleaseAddressInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PublicIp: aws.String(d.Id()),
|
2015-03-03 18:45:27 +01:00
|
|
|
})
|
2014-10-19 00:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if _, ok := err.(awserr.Error); !ok {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2014-10-19 00:32:33 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(err)
|
2014-10-19 00:32:33 +02:00
|
|
|
})
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 20:24:45 +02:00
|
|
|
func resourceAwsEipImportState(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
func resourceAwsEipDomain(d *schema.ResourceData) string {
|
2014-09-09 05:24:43 +02:00
|
|
|
if v, ok := d.GetOk("domain"); ok {
|
2014-08-20 02:22:25 +02:00
|
|
|
return v.(string)
|
|
|
|
} else if strings.Contains(d.Id(), "eipalloc") {
|
|
|
|
// We have to do this for backwards compatibility since TF 0.1
|
|
|
|
// didn't have the "domain" computed attribute.
|
|
|
|
return "vpc"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "standard"
|
|
|
|
}
|