Merge pull request #1110 from hashicorp/aws-go-eip

provider/aws: Convert AWS EIP to use aws-sdk-go
This commit is contained in:
Clint 2015-03-04 10:20:31 -06:00
commit fa2f8fc19c
2 changed files with 69 additions and 43 deletions

View File

@ -6,9 +6,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/ec2"
) )
func resourceAwsEip() *schema.Resource { func resourceAwsEip() *schema.Resource {
@ -59,7 +60,7 @@ func resourceAwsEip() *schema.Resource {
} }
func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
// By default, we're not in a VPC // By default, we're not in a VPC
domainOpt := "" domainOpt := ""
@ -67,12 +68,12 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
domainOpt = "vpc" domainOpt = "vpc"
} }
allocOpts := ec2.AllocateAddress{ allocOpts := &ec2.AllocateAddressRequest{
Domain: domainOpt, Domain: aws.String(domainOpt),
} }
log.Printf("[DEBUG] EIP create configuration: %#v", allocOpts) log.Printf("[DEBUG] EIP create configuration: %#v", allocOpts)
allocResp, err := ec2conn.AllocateAddress(&allocOpts) allocResp, err := ec2conn.AllocateAddress(allocOpts)
if err != nil { if err != nil {
return fmt.Errorf("Error creating EIP: %s", err) return fmt.Errorf("Error creating EIP: %s", err)
} }
@ -86,17 +87,17 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
// it defaults to using the public IP // it defaults to using the public IP
log.Printf("[DEBUG] EIP Allocate: %#v", allocResp) log.Printf("[DEBUG] EIP Allocate: %#v", allocResp)
if d.Get("domain").(string) == "vpc" { if d.Get("domain").(string) == "vpc" {
d.SetId(allocResp.AllocationId) d.SetId(*allocResp.AllocationID)
} else { } else {
d.SetId(allocResp.PublicIp) d.SetId(*allocResp.PublicIP)
} }
log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), allocResp.Domain) log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), *allocResp.Domain)
return resourceAwsEipUpdate(d, meta) return resourceAwsEipUpdate(d, meta)
} }
func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
domain := resourceAwsEipDomain(d) domain := resourceAwsEipDomain(d)
id := d.Id() id := d.Id()
@ -113,9 +114,13 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
"[DEBUG] EIP describe configuration: %#v, %#v (domain: %s)", "[DEBUG] EIP describe configuration: %#v, %#v (domain: %s)",
assocIds, publicIps, domain) assocIds, publicIps, domain)
describeAddresses, err := ec2conn.Addresses(publicIps, assocIds, nil) req := &ec2.DescribeAddressesRequest{
AllocationIDs: assocIds,
PublicIPs: publicIps,
}
describeAddresses, err := ec2conn.DescribeAddresses(req)
if err != nil { if err != nil {
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidAllocationID.NotFound" { if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidAllocationID.NotFound" {
d.SetId("") d.SetId("")
return nil return nil
} }
@ -125,8 +130,8 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
// Verify AWS returned our EIP // Verify AWS returned our EIP
if len(describeAddresses.Addresses) != 1 || if len(describeAddresses.Addresses) != 1 ||
describeAddresses.Addresses[0].AllocationId != id || *describeAddresses.Addresses[0].AllocationID != id ||
describeAddresses.Addresses[0].PublicIp != id { *describeAddresses.Addresses[0].PublicIP != id {
if err != nil { if err != nil {
return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses) return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses)
} }
@ -134,16 +139,16 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
address := describeAddresses.Addresses[0] address := describeAddresses.Addresses[0]
d.Set("association_id", address.AssociationId) d.Set("association_id", address.AssociationID)
d.Set("instance", address.InstanceId) d.Set("instance", address.InstanceID)
d.Set("public_ip", address.PublicIp) d.Set("private_ip", address.PrivateIPAddress)
d.Set("private_ip", address.PrivateIpAddress) d.Set("public_ip", address.PublicIP)
return nil return nil
} }
func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
domain := resourceAwsEipDomain(d) domain := resourceAwsEipDomain(d)
@ -151,22 +156,22 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
if v, ok := d.GetOk("instance"); ok { if v, ok := d.GetOk("instance"); ok {
instanceId := v.(string) instanceId := v.(string)
assocOpts := ec2.AssociateAddress{ assocOpts := &ec2.AssociateAddressRequest{
InstanceId: instanceId, InstanceID: aws.String(instanceId),
PublicIp: d.Id(), PublicIP: aws.String(d.Id()),
} }
// more unique ID conditionals // more unique ID conditionals
if domain == "vpc" { if domain == "vpc" {
assocOpts = ec2.AssociateAddress{ assocOpts = &ec2.AssociateAddressRequest{
InstanceId: instanceId, InstanceID: aws.String(instanceId),
AllocationId: d.Id(), AllocationID: aws.String(d.Id()),
PublicIp: "", PublicIP: aws.String(""),
} }
} }
log.Printf("[DEBUG] EIP associate configuration: %#v (domain: %v)", assocOpts, domain) log.Printf("[DEBUG] EIP associate configuration: %#v (domain: %v)", assocOpts, domain)
_, err := ec2conn.AssociateAddress(&assocOpts) _, err := ec2conn.AssociateAddress(assocOpts)
if err != nil { if err != nil {
return fmt.Errorf("Failure associating instances: %s", err) return fmt.Errorf("Failure associating instances: %s", err)
} }
@ -176,7 +181,7 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
} }
func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
if err := resourceAwsEipRead(d, meta); err != nil { if err := resourceAwsEipRead(d, meta); err != nil {
return err return err
@ -192,9 +197,13 @@ func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
var err error var err error
switch resourceAwsEipDomain(d) { switch resourceAwsEipDomain(d) {
case "vpc": case "vpc":
_, err = ec2conn.DisassociateAddress(d.Get("association_id").(string)) err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressRequest{
AssociationID: aws.String(d.Get("association_id").(string)),
})
case "standard": case "standard":
_, err = ec2conn.DisassociateAddressClassic(d.Get("public_ip").(string)) err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressRequest{
PublicIP: aws.String(d.Get("public_ip").(string)),
})
} }
if err != nil { if err != nil {
return err return err
@ -209,16 +218,20 @@ func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf( log.Printf(
"[DEBUG] EIP release (destroy) address allocation: %v", "[DEBUG] EIP release (destroy) address allocation: %v",
d.Id()) d.Id())
_, err = ec2conn.ReleaseAddress(d.Id()) err = ec2conn.ReleaseAddress(&ec2.ReleaseAddressRequest{
AllocationID: aws.String(d.Id()),
})
case "standard": case "standard":
log.Printf("[DEBUG] EIP release (destroy) address: %v", d.Id()) log.Printf("[DEBUG] EIP release (destroy) address: %v", d.Id())
_, err = ec2conn.ReleasePublicAddress(d.Id()) err = ec2conn.ReleaseAddress(&ec2.ReleaseAddressRequest{
PublicIP: aws.String(d.Id()),
})
} }
if err == nil { if err == nil {
return nil return nil
} }
if _, ok := err.(*ec2.Error); !ok { if _, ok := err.(aws.APIError); !ok {
return resource.RetryError{Err: err} return resource.RetryError{Err: err}
} }

View File

@ -5,9 +5,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
) )
func TestAccAWSEIP_normal(t *testing.T) { func TestAccAWSEIP_normal(t *testing.T) {
@ -57,24 +58,28 @@ func TestAccAWSEIP_instance(t *testing.T) {
} }
func testAccCheckAWSEIPDestroy(s *terraform.State) error { func testAccCheckAWSEIPDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_eip" { if rs.Type != "aws_eip" {
continue continue
} }
describe, err := conn.Addresses([]string{rs.Primary.ID}, []string{}, nil) req := &ec2.DescribeAddressesRequest{
AllocationIDs: []string{},
PublicIPs: []string{rs.Primary.ID},
}
describe, err := conn.DescribeAddresses(req)
if err == nil { if err == nil {
if len(describe.Addresses) != 0 && if len(describe.Addresses) != 0 &&
describe.Addresses[0].PublicIp == rs.Primary.ID { *describe.Addresses[0].PublicIP == rs.Primary.ID {
return fmt.Errorf("EIP still exists") return fmt.Errorf("EIP still exists")
} }
} }
// Verify the error // Verify the error
providerErr, ok := err.(*ec2.Error) providerErr, ok := err.(aws.APIError)
if !ok { if !ok {
return err return err
} }
@ -89,7 +94,7 @@ func testAccCheckAWSEIPDestroy(s *terraform.State) error {
func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc { func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if conf.PublicIp == "" { if *conf.PublicIP == "" {
return fmt.Errorf("empty public_ip") return fmt.Errorf("empty public_ip")
} }
@ -108,28 +113,36 @@ func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc
return fmt.Errorf("No EIP ID is set") return fmt.Errorf("No EIP ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
if strings.Contains(rs.Primary.ID, "eipalloc") { if strings.Contains(rs.Primary.ID, "eipalloc") {
describe, err := conn.Addresses([]string{}, []string{rs.Primary.ID}, nil) req := &ec2.DescribeAddressesRequest{
AllocationIDs: []string{rs.Primary.ID},
PublicIPs: []string{},
}
describe, err := conn.DescribeAddresses(req)
if err != nil { if err != nil {
return err return err
} }
if len(describe.Addresses) != 1 || if len(describe.Addresses) != 1 ||
describe.Addresses[0].AllocationId != rs.Primary.ID { *describe.Addresses[0].AllocationID != rs.Primary.ID {
return fmt.Errorf("EIP not found") return fmt.Errorf("EIP not found")
} }
*res = describe.Addresses[0] *res = describe.Addresses[0]
} else { } else {
describe, err := conn.Addresses([]string{rs.Primary.ID}, []string{}, nil) req := &ec2.DescribeAddressesRequest{
AllocationIDs: []string{},
PublicIPs: []string{rs.Primary.ID},
}
describe, err := conn.DescribeAddresses(req)
if err != nil { if err != nil {
return err return err
} }
if len(describe.Addresses) != 1 || if len(describe.Addresses) != 1 ||
describe.Addresses[0].PublicIp != rs.Primary.ID { *describe.Addresses[0].PublicIP != rs.Primary.ID {
return fmt.Errorf("EIP not found") return fmt.Errorf("EIP not found")
} }
*res = describe.Addresses[0] *res = describe.Addresses[0]