providers/aws: Convert AWS Internet gateway to upstream aws-sdk-go

This commit is contained in:
Clint Shryock 2015-04-07 09:44:00 -05:00
parent 0fbbd1195a
commit 2c1c448031
3 changed files with 43 additions and 43 deletions

View File

@ -5,8 +5,8 @@ import (
"log" "log"
"time" "time"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/awslabs/aws-sdk-go/service/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"
) )
@ -29,21 +29,21 @@ func resourceAwsInternetGateway() *schema.Resource {
} }
func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
// Create the gateway // Create the gateway
log.Printf("[DEBUG] Creating internet gateway") log.Printf("[DEBUG] Creating internet gateway")
resp, err := ec2conn.CreateInternetGateway(nil) resp, err := conn.CreateInternetGateway(nil)
if err != nil { if err != nil {
return fmt.Errorf("Error creating internet gateway: %s", err) return fmt.Errorf("Error creating internet gateway: %s", err)
} }
// Get the ID and store it // Get the ID and store it
ig := resp.InternetGateway ig := *resp.InternetGateway
d.SetId(*ig.InternetGatewayID) d.SetId(*ig.InternetGatewayID)
log.Printf("[INFO] InternetGateway ID: %s", d.Id()) log.Printf("[INFO] InternetGateway ID: %s", d.Id())
err = setTags(ec2conn, d) err = setTagsSDK(conn, d)
if err != nil { if err != nil {
return err return err
} }
@ -53,9 +53,9 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{})
} }
func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
igRaw, _, err := IGStateRefreshFunc(ec2conn, d.Id())() igRaw, _, err := IGStateRefreshFunc(conn, d.Id())()
if err != nil { if err != nil {
return err return err
} }
@ -73,7 +73,7 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er
d.Set("vpc_id", ig.Attachments[0].VPCID) d.Set("vpc_id", ig.Attachments[0].VPCID)
} }
d.Set("tags", tagsToMap(ig.Tags)) d.Set("tags", tagsToMapSDK(ig.Tags))
return nil return nil
} }
@ -91,9 +91,9 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{})
} }
} }
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
if err := setTags(ec2conn, d); err != nil { if err := setTagsSDK(conn, d); err != nil {
return err return err
} }
@ -103,7 +103,7 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{})
} }
func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
// Detach if it is attached // Detach if it is attached
if err := resourceAwsInternetGatewayDetach(d, meta); err != nil { if err := resourceAwsInternetGatewayDetach(d, meta); err != nil {
@ -113,7 +113,7 @@ func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{})
log.Printf("[INFO] Deleting Internet Gateway: %s", d.Id()) log.Printf("[INFO] Deleting Internet Gateway: %s", d.Id())
return resource.Retry(5*time.Minute, func() error { return resource.Retry(5*time.Minute, func() error {
err := ec2conn.DeleteInternetGateway(&ec2.DeleteInternetGatewayRequest{ _, err := conn.DeleteInternetGateway(&ec2.DeleteInternetGatewayInput{
InternetGatewayID: aws.String(d.Id()), InternetGatewayID: aws.String(d.Id()),
}) })
if err == nil { if err == nil {
@ -137,7 +137,7 @@ func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{})
} }
func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) error { func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
if d.Get("vpc_id").(string) == "" { if d.Get("vpc_id").(string) == "" {
log.Printf( log.Printf(
@ -151,7 +151,7 @@ func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{})
d.Id(), d.Id(),
d.Get("vpc_id").(string)) d.Get("vpc_id").(string))
err := ec2conn.AttachInternetGateway(&ec2.AttachInternetGatewayRequest{ _, err := conn.AttachInternetGateway(&ec2.AttachInternetGatewayInput{
InternetGatewayID: aws.String(d.Id()), InternetGatewayID: aws.String(d.Id()),
VPCID: aws.String(d.Get("vpc_id").(string)), VPCID: aws.String(d.Get("vpc_id").(string)),
}) })
@ -169,7 +169,7 @@ func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{})
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"detached", "attaching"}, Pending: []string{"detached", "attaching"},
Target: "available", Target: "available",
Refresh: IGAttachStateRefreshFunc(ec2conn, d.Id(), "available"), Refresh: IGAttachStateRefreshFunc(conn, d.Id(), "available"),
Timeout: 1 * time.Minute, Timeout: 1 * time.Minute,
} }
if _, err := stateConf.WaitForState(); err != nil { if _, err := stateConf.WaitForState(); err != nil {
@ -182,7 +182,7 @@ func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{})
} }
func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) error { func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
// Get the old VPC ID to detach from // Get the old VPC ID to detach from
vpcID, _ := d.GetChange("vpc_id") vpcID, _ := d.GetChange("vpc_id")
@ -204,7 +204,7 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"detaching"}, Pending: []string{"detaching"},
Target: "detached", Target: "detached",
Refresh: detachIGStateRefreshFunc(ec2conn, d.Id(), vpcID.(string)), Refresh: detachIGStateRefreshFunc(conn, d.Id(), vpcID.(string)),
Timeout: 2 * time.Minute, Timeout: 2 * time.Minute,
Delay: 10 * time.Second, Delay: 10 * time.Second,
} }
@ -221,7 +221,7 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
// an EC2 instance. // an EC2 instance.
func detachIGStateRefreshFunc(conn *ec2.EC2, instanceID, vpcID string) resource.StateRefreshFunc { func detachIGStateRefreshFunc(conn *ec2.EC2, instanceID, vpcID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
err := conn.DetachInternetGateway(&ec2.DetachInternetGatewayRequest{ _, err := conn.DetachInternetGateway(&ec2.DetachInternetGatewayInput{
InternetGatewayID: aws.String(instanceID), InternetGatewayID: aws.String(instanceID),
VPCID: aws.String(vpcID), VPCID: aws.String(vpcID),
}) })
@ -245,10 +245,10 @@ func detachIGStateRefreshFunc(conn *ec2.EC2, instanceID, vpcID string) resource.
// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an internet gateway. // an internet gateway.
func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc { func IGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
resp, err := ec2conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysRequest{ resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
InternetGatewayIDs: []string{id}, InternetGatewayIDs: []*string{aws.String(id)},
}) })
if err != nil { if err != nil {
ec2err, ok := err.(aws.APIError) ec2err, ok := err.(aws.APIError)
@ -266,22 +266,22 @@ func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc {
return nil, "", nil return nil, "", nil
} }
ig := &resp.InternetGateways[0] ig := resp.InternetGateways[0]
return ig, "available", nil return ig, "available", nil
} }
} }
// IGAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used // IGAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used
// watch the state of an internet gateway's attachment. // watch the state of an internet gateway's attachment.
func IGAttachStateRefreshFunc(ec2conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc { func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
var start time.Time var start time.Time
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
if start.IsZero() { if start.IsZero() {
start = time.Now() start = time.Now()
} }
resp, err := ec2conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysRequest{ resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
InternetGatewayIDs: []string{id}, InternetGatewayIDs: []*string{aws.String(id)},
}) })
if err != nil { if err != nil {
ec2err, ok := err.(aws.APIError) ec2err, ok := err.(aws.APIError)
@ -299,7 +299,7 @@ func IGAttachStateRefreshFunc(ec2conn *ec2.EC2, id string, expected string) reso
return nil, "", nil return nil, "", nil
} }
ig := &resp.InternetGateways[0] ig := resp.InternetGateways[0]
if len(ig.Attachments) == 0 { if len(ig.Attachments) == 0 {
// No attachments, we're detached // No attachments, we're detached

View File

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
func TestAccAWSInternetGateway(t *testing.T) { func TestAccAWSInternetGateway_basic(t *testing.T) {
var v, v2 ec2.InternetGateway var v, v2 ec2.InternetGateway
testNotEqual := func(*terraform.State) error { testNotEqual := func(*terraform.State) error {
@ -86,7 +86,7 @@ func TestAccAWSInternetGateway_delete(t *testing.T) {
}) })
} }
func TestAccInternetGateway_tags(t *testing.T) { func TestAccAWSInternetGateway_tags(t *testing.T) {
var v ec2.InternetGateway var v ec2.InternetGateway
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -98,7 +98,7 @@ func TestAccInternetGateway_tags(t *testing.T) {
Config: testAccCheckInternetGatewayConfigTags, Config: testAccCheckInternetGatewayConfigTags,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v), testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
testAccCheckTags(&v.Tags, "foo", "bar"), testAccCheckTagsSDK(&v.Tags, "foo", "bar"),
), ),
}, },
@ -106,8 +106,8 @@ func TestAccInternetGateway_tags(t *testing.T) {
Config: testAccCheckInternetGatewayConfigTagsUpdate, Config: testAccCheckInternetGatewayConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v), testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
testAccCheckTags(&v.Tags, "foo", ""), testAccCheckTagsSDK(&v.Tags, "foo", ""),
testAccCheckTags(&v.Tags, "bar", "baz"), testAccCheckTagsSDK(&v.Tags, "bar", "baz"),
), ),
}, },
}, },
@ -115,7 +115,7 @@ func TestAccInternetGateway_tags(t *testing.T) {
} }
func testAccCheckInternetGatewayDestroy(s *terraform.State) error { func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_internet_gateway" { if rs.Type != "aws_internet_gateway" {
@ -123,8 +123,8 @@ func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
} }
// Try to find the resource // Try to find the resource
resp, err := ec2conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysRequest{ resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
InternetGatewayIDs: []string{rs.Primary.ID}, InternetGatewayIDs: []*string{aws.String(rs.Primary.ID)},
}) })
if err == nil { if err == nil {
if len(resp.InternetGateways) > 0 { if len(resp.InternetGateways) > 0 {
@ -158,9 +158,9 @@ func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resour
return fmt.Errorf("No ID is set") return fmt.Errorf("No ID is set")
} }
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
resp, err := ec2conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysRequest{ resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
InternetGatewayIDs: []string{rs.Primary.ID}, InternetGatewayIDs: []*string{aws.String(rs.Primary.ID)},
}) })
if err != nil { if err != nil {
return err return err
@ -169,7 +169,7 @@ func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resour
return fmt.Errorf("InternetGateway not found") return fmt.Errorf("InternetGateway not found")
} }
*ig = resp.InternetGateways[0] *ig = *resp.InternetGateways[0]
return nil return nil
} }

View File

@ -63,9 +63,9 @@ func TestDiffTagsSDK(t *testing.T) {
// testAccCheckTags can be used to check the tags on a resource. // testAccCheckTags can be used to check the tags on a resource.
func testAccCheckTagsSDK( func testAccCheckTagsSDK(
ts []*ec2.Tag, key string, value string) resource.TestCheckFunc { ts *[]*ec2.Tag, key string, value string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
m := tagsToMapSDK(ts) m := tagsToMapSDK(*ts)
v, ok := m[key] v, ok := m[key]
if value != "" && !ok { if value != "" && !ok {
return fmt.Errorf("Missing tag: %s", key) return fmt.Errorf("Missing tag: %s", key)