Merge pull request #1407 from hashicorp/f-aws-upstream-internet-gateway

Convert AWS Internet Gateway to use upstream aws-sdk-go
This commit is contained in:
Clint 2015-04-08 15:51:10 -05:00
commit 0f6ba3eae9
3 changed files with 43 additions and 43 deletions

View File

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

View File

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

View File

@ -63,9 +63,9 @@ func TestDiffTagsSDK(t *testing.T) {
// testAccCheckTags can be used to check the tags on a resource.
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 {
m := tagsToMapSDK(ts)
m := tagsToMapSDK(*ts)
v, ok := m[key]
if value != "" && !ok {
return fmt.Errorf("Missing tag: %s", key)