From 89d6cdb0a69fe003792c6f95528519760d7859b7 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Tue, 3 Mar 2015 16:08:52 -0600 Subject: [PATCH] providers/aws: Convert AWS Internet Gateway to aws-sdk-go --- .../aws/resource_aws_internet_gateway.go | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/builtin/providers/aws/resource_aws_internet_gateway.go b/builtin/providers/aws/resource_aws_internet_gateway.go index 39aed079d..08f77a5c6 100644 --- a/builtin/providers/aws/resource_aws_internet_gateway.go +++ b/builtin/providers/aws/resource_aws_internet_gateway.go @@ -5,9 +5,10 @@ import ( "log" "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/schema" - "github.com/mitchellh/goamz/ec2" ) func resourceAwsInternetGateway() *schema.Resource { @@ -28,7 +29,7 @@ func resourceAwsInternetGateway() *schema.Resource { } func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{}) error { - ec2conn := meta.(*AWSClient).ec2conn + ec2conn := meta.(*AWSClient).awsEC2conn // Create the gateway log.Printf("[DEBUG] Creating internet gateway") @@ -38,8 +39,8 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{}) } // Get the ID and store it - ig := &resp.InternetGateway - d.SetId(ig.InternetGatewayId) + ig := resp.InternetGateway + d.SetId(*ig.InternetGatewayID) log.Printf("[INFO] InternetGateway ID: %s", d.Id()) // Attach the new gateway to the correct vpc @@ -47,7 +48,7 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{}) } func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error { - ec2conn := meta.(*AWSClient).ec2conn + ec2conn := meta.(*AWSClient).awsEC2conn igRaw, _, err := IGStateRefreshFunc(ec2conn, d.Id())() if err != nil { @@ -64,10 +65,10 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er // Gateway exists but not attached to the VPC d.Set("vpc_id", "") } else { - 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 } @@ -85,9 +86,9 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{}) } } - ec2conn := meta.(*AWSClient).ec2conn + ec2conn := meta.(*AWSClient).awsEC2conn - if err := setTags(ec2conn, d); err != nil { + if err := setTagsSDK(ec2conn, d); err != nil { return err } @@ -97,7 +98,7 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{}) } func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{}) error { - ec2conn := meta.(*AWSClient).ec2conn + ec2conn := meta.(*AWSClient).awsEC2conn // Detach if it is attached if err := resourceAwsInternetGatewayDetach(d, meta); err != nil { @@ -107,12 +108,14 @@ 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(d.Id()) + err := ec2conn.DeleteInternetGateway(&ec2.DeleteInternetGatewayRequest{ + InternetGatewayID: aws.String(d.Id()), + }) if err == nil { return nil } - ec2err, ok := err.(*ec2.Error) + ec2err, ok := err.(aws.APIError) if !ok { return err } @@ -129,7 +132,7 @@ func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{}) } func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) error { - ec2conn := meta.(*AWSClient).ec2conn + ec2conn := meta.(*AWSClient).awsEC2conn if d.Get("vpc_id").(string) == "" { log.Printf( @@ -143,7 +146,10 @@ func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) d.Id(), d.Get("vpc_id").(string)) - _, err := ec2conn.AttachInternetGateway(d.Id(), d.Get("vpc_id").(string)) + err := ec2conn.AttachInternetGateway(&ec2.AttachInternetGatewayRequest{ + InternetGatewayID: aws.String(d.Id()), + VPCID: aws.String(d.Get("vpc_id").(string)), + }) if err != nil { return err } @@ -171,7 +177,7 @@ func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) } func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) error { - ec2conn := meta.(*AWSClient).ec2conn + ec2conn := meta.(*AWSClient).awsEC2conn // Get the old VPC ID to detach from vpcID, _ := d.GetChange("vpc_id") @@ -189,9 +195,12 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) vpcID.(string)) wait := true - _, err := ec2conn.DetachInternetGateway(d.Id(), vpcID.(string)) + err := ec2conn.DetachInternetGateway(&ec2.DetachInternetGatewayRequest{ + InternetGatewayID: aws.String(d.Id()), + VPCID: aws.String(vpcID.(string)), + }) if err != nil { - ec2err, ok := err.(*ec2.Error) + ec2err, ok := err.(aws.APIError) if ok { if ec2err.Code == "InvalidInternetGatewayID.NotFound" { err = nil @@ -232,9 +241,11 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) // an internet gateway. func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc { return func() (interface{}, string, error) { - resp, err := ec2conn.DescribeInternetGateways([]string{id}, ec2.NewFilter()) + resp, err := ec2conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysRequest{ + InternetGatewayIDs: []string{id}, + }) if err != nil { - ec2err, ok := err.(*ec2.Error) + ec2err, ok := err.(aws.APIError) if ok && ec2err.Code == "InvalidInternetGatewayID.NotFound" { resp = nil } else { @@ -256,16 +267,18 @@ func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc { // IGAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used // watch the state of an internet gateway's attachment. -func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc { +func IGAttachStateRefreshFunc(ec2conn *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 := conn.DescribeInternetGateways([]string{id}, ec2.NewFilter()) + resp, err := ec2conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysRequest{ + InternetGatewayIDs: []string{id}, + }) if err != nil { - ec2err, ok := err.(*ec2.Error) + ec2err, ok := err.(aws.APIError) if ok && ec2err.Code == "InvalidInternetGatewayID.NotFound" { resp = nil } else { @@ -291,6 +304,6 @@ func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resourc return ig, "detached", nil } - return ig, ig.Attachments[0].State, nil + return ig, *ig.Attachments[0].State, nil } }