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
|
|
|
|
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"
|
|
|
|
//"github.com/hashicorp/terraform/terraform"
|
2014-07-07 21:19:40 +02:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
func resourceAwsEip() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsEipCreate,
|
|
|
|
Read: resourceAwsEipRead,
|
|
|
|
Update: resourceAwsEipUpdate,
|
|
|
|
Delete: resourceAwsEipDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"vpc": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
"allocation_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 21:19:40 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
|
|
|
|
allocOpts := ec2.AllocateAddress{
|
|
|
|
Domain: domainOpt,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] EIP create configuration: %#v", allocOpts)
|
|
|
|
allocResp, err := ec2conn.AllocateAddress(&allocOpts)
|
|
|
|
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" {
|
2014-08-20 01:56:23 +02:00
|
|
|
d.SetId(allocResp.AllocationId)
|
2014-07-07 21:19:40 +02:00
|
|
|
} else {
|
2014-08-20 01:56:23 +02:00
|
|
|
d.SetId(allocResp.PublicIp)
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), allocResp.Domain)
|
|
|
|
return resourceAwsEipUpdate(d, meta)
|
2014-07-27 00:53:26 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
|
2014-07-27 00:53:26 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
domain := resourceAwsEipDomain(d)
|
2014-07-07 21:19:40 +02:00
|
|
|
|
|
|
|
// Only register with an instance if we have one
|
2014-09-09 23:15:08 +02:00
|
|
|
if v, ok := d.GetOk("instance"); ok {
|
2014-08-20 01:56:23 +02:00
|
|
|
instanceId := v.(string)
|
|
|
|
|
2014-07-07 21:19:40 +02:00
|
|
|
assocOpts := ec2.AssociateAddress{
|
|
|
|
InstanceId: instanceId,
|
2014-08-20 01:56:23 +02:00
|
|
|
PublicIp: 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" {
|
2014-07-07 21:19:40 +02:00
|
|
|
assocOpts = ec2.AssociateAddress{
|
|
|
|
InstanceId: instanceId,
|
2014-08-20 01:56:23 +02:00
|
|
|
AllocationId: d.Id(),
|
2014-07-28 18:04:58 +02:00
|
|
|
PublicIp: "",
|
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)
|
2014-07-07 21:19:40 +02:00
|
|
|
_, err := ec2conn.AssociateAddress(&assocOpts)
|
|
|
|
if err != nil {
|
2014-08-20 01:56:23 +02:00
|
|
|
return fmt.Errorf("Failure associating instances: %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 {
|
2014-09-09 23:18:53 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "destroyed",
|
|
|
|
Refresh: resourceEipDeleteRefreshFunc(d, meta),
|
|
|
|
Timeout: 3 * time.Minute,
|
|
|
|
MinTimeout: 1 * time.Second,
|
2014-07-15 00:30:31 +02:00
|
|
|
}
|
2014-09-09 23:18:53 +02:00
|
|
|
_, err := stateConf.WaitForState()
|
2014-08-25 06:50:35 +02:00
|
|
|
return err
|
2014-07-07 21:19:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 21:37:09 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-08-20 02:22:25 +02:00
|
|
|
domain := resourceAwsEipDomain(d)
|
2014-08-20 01:56:23 +02:00
|
|
|
id := d.Id()
|
2014-07-07 21:19:40 +02:00
|
|
|
|
2014-07-07 23:38:35 +02:00
|
|
|
assocIds := []string{}
|
|
|
|
publicIps := []string{}
|
2014-08-20 02:22:25 +02:00
|
|
|
if domain == "vpc" {
|
2014-07-07 23:38:35 +02:00
|
|
|
assocIds = []string{id}
|
|
|
|
} else {
|
|
|
|
publicIps = []string{id}
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
log.Printf(
|
2014-08-20 02:22:25 +02:00
|
|
|
"[DEBUG] EIP describe configuration: %#v, %#v (domain: %s)",
|
|
|
|
assocIds, publicIps, domain)
|
2014-07-07 23:38:35 +02:00
|
|
|
|
|
|
|
describeAddresses, err := ec2conn.Addresses(publicIps, assocIds, nil)
|
|
|
|
if err != nil {
|
2014-09-09 23:15:33 +02:00
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidAllocationID.NotFound" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
return fmt.Errorf("Error retrieving EIP: %s", err)
|
2014-07-07 23:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify AWS returned our EIP
|
|
|
|
if len(describeAddresses.Addresses) != 1 ||
|
|
|
|
describeAddresses.Addresses[0].AllocationId != id ||
|
|
|
|
describeAddresses.Addresses[0].PublicIp != id {
|
|
|
|
if err != nil {
|
2014-08-20 01:56:23 +02:00
|
|
|
return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses)
|
2014-07-07 23:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
address := describeAddresses.Addresses[0]
|
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
d.Set("instance", address.InstanceId)
|
|
|
|
d.Set("public_ip", address.PublicIp)
|
|
|
|
d.Set("private_ip", address.PrivateIpAddress)
|
2014-07-15 18:18:36 +02:00
|
|
|
|
2014-08-20 01:56:23 +02:00
|
|
|
return nil
|
2014-07-15 18:18:36 +02:00
|
|
|
}
|
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"
|
|
|
|
}
|
2014-09-09 23:18:53 +02:00
|
|
|
|
|
|
|
func resourceEipDeleteRefreshFunc(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{}) resource.StateRefreshFunc {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
domain := resourceAwsEipDomain(d)
|
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
var err error
|
|
|
|
if domain == "vpc" {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] EIP release (destroy) address allocation: %v",
|
|
|
|
d.Id())
|
|
|
|
_, err = ec2conn.ReleaseAddress(d.Id())
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] EIP release (destroy) address: %v", d.Id())
|
|
|
|
_, err = ec2conn.ReleasePublicAddress(d.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return d, "destroyed", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
if !ok {
|
|
|
|
return d, "error", err
|
|
|
|
}
|
|
|
|
if ec2err.Code != "InvalidIPAddress.InUse" {
|
|
|
|
return d, "error", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, "pending", nil
|
|
|
|
}
|
|
|
|
}
|