Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/internet_gateway - add arn + disappears test #13614

Merged
merged 4 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions aws/data_source_aws_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
Expand Down Expand Up @@ -41,6 +42,10 @@ func dataSourceAwsInternetGateway() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -91,6 +96,16 @@ func dataSourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{})
d.Set("owner_id", igw.OwnerId)
d.Set("internet_gateway_id", igw.InternetGatewayId)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("internet-gateway/%s", d.Id()),
}.String()

d.Set("arn", arn)

err1 := d.Set("attachments", dataSourceAttachmentsRead(igw.Attachments))
return err1

Expand All @@ -100,8 +115,8 @@ func dataSourceAttachmentsRead(igwAttachments []*ec2.InternetGatewayAttachment)
attachments := make([]map[string]interface{}, 0, len(igwAttachments))
for _, a := range igwAttachments {
m := make(map[string]interface{})
m["state"] = *a.State
m["vpc_id"] = *a.VpcId
m["state"] = aws.StringValue(a.State)
m["vpc_id"] = aws.StringValue(a.VpcId)
attachments = append(attachments, m)
}

Expand Down
1 change: 1 addition & 0 deletions aws/data_source_aws_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAccDataSourceAwsInternetGateway_typical(t *testing.T) {
resource.TestCheckResourceAttrPair(ds1ResourceName, "internet_gateway_id", igwResourceName, "id"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "owner_id", igwResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "attachments.0.vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "arn", igwResourceName, "arn"),

resource.TestCheckResourceAttrPair(ds2ResourceName, "internet_gateway_id", igwResourceName, "id"),
resource.TestCheckResourceAttrPair(ds2ResourceName, "owner_id", igwResourceName, "owner_id"),
Expand Down
37 changes: 25 additions & 12 deletions aws/resource_aws_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -33,6 +34,10 @@ func resourceAwsInternetGateway() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -98,7 +103,7 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er
return err
}
if igRaw == nil {
// Seems we have lost our internet gateway
log.Printf("[WARN] Internet Gateway (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand All @@ -117,6 +122,16 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er

d.Set("owner_id", ig.OwnerId)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("internet-gateway/%s", d.Id()),
}.String()

d.Set("arn", arn)

return nil
}

Expand Down Expand Up @@ -227,7 +242,7 @@ func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{})
// Wait for it to be fully attached before continuing
log.Printf("[DEBUG] Waiting for internet gateway (%s) to attach", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"detached", "attaching"},
Pending: []string{ec2.AttachmentStatusDetached, ec2.AttachmentStatusAttaching},
Target: []string{"available"},
Refresh: IGAttachStateRefreshFunc(conn, d.Id(), "available"),
Timeout: 4 * time.Minute,
Expand Down Expand Up @@ -262,8 +277,8 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
// Wait for it to be fully detached before continuing
log.Printf("[DEBUG] Waiting for internet gateway (%s) to detach", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"detaching"},
Target: []string{"detached"},
Pending: []string{ec2.AttachmentStatusDetaching},
Target: []string{ec2.AttachmentStatusDetached},
Refresh: detachIGStateRefreshFunc(conn, d.Id(), vpcID.(string)),
Timeout: 15 * time.Minute,
Delay: 10 * time.Second,
Expand Down Expand Up @@ -294,7 +309,7 @@ func detachIGStateRefreshFunc(conn *ec2.EC2, gatewayID, vpcID string) resource.S
return nil, "", nil

case "Gateway.NotAttached":
return 42, "detached", nil
return 42, ec2.AttachmentStatusDetached, nil

case "DependencyViolation":
// This can be caused by associated public IPs left (e.g. by ELBs)
Expand All @@ -308,15 +323,15 @@ func detachIGStateRefreshFunc(conn *ec2.EC2, gatewayID, vpcID string) resource.S
len(out.NetworkInterfaces), out.NetworkInterfaces)
}

return 42, "detaching", nil
return 42, ec2.AttachmentStatusDetaching, nil
}
}
return 42, "", err
}

// DetachInternetGateway only returns an error, so if it's nil, assume we're
// detached
return 42, "detached", nil
return 42, ec2.AttachmentStatusDetached, nil
}
}

Expand All @@ -343,8 +358,7 @@ func IGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
InternetGatewayIds: []*string{aws.String(id)},
})
if err != nil {
ec2err, ok := err.(awserr.Error)
if ok && ec2err.Code() == "InvalidInternetGatewayID.NotFound" {
if isAWSErr(err, "InvalidInternetGatewayID.NotFound", "") {
resp = nil
} else {
log.Printf("[ERROR] Error on IGStateRefresh: %s", err)
Expand Down Expand Up @@ -376,8 +390,7 @@ func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resourc
InternetGatewayIds: []*string{aws.String(id)},
})
if err != nil {
ec2err, ok := err.(awserr.Error)
if ok && ec2err.Code() == "InvalidInternetGatewayID.NotFound" {
if isAWSErr(err, "InvalidInternetGatewayID.NotFound", "") {
resp = nil
} else {
log.Printf("[ERROR] Error on IGStateRefresh: %s", err)
Expand All @@ -399,7 +412,7 @@ func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resourc

if len(ig.Attachments) == 0 {
// No attachments, we're detached
return ig, "detached", nil
return ig, ec2.AttachmentStatusDetached, nil
}

return ig, *ig.Attachments[0].State, nil
Expand Down
44 changes: 29 additions & 15 deletions aws/resource_aws_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
Expand Down Expand Up @@ -86,8 +86,8 @@ func testSweepInternetGateways(region string) error {
}

stateConf := &resource.StateChangeConf{
Pending: []string{"detaching"},
Target: []string{"detached"},
Pending: []string{ec2.AttachmentStatusDetaching},
Target: []string{ec2.AttachmentStatusDetached},
Refresh: detachIGStateRefreshFunc(conn, aws.StringValue(internetGateway.InternetGatewayId), aws.StringValue(attachment.VpcId)),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
Expand Down Expand Up @@ -148,9 +148,10 @@ func TestAccAWSInternetGateway_basic(t *testing.T) {
{
Config: testAccInternetGatewayConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(
resourceName, &v),
testAccCheckInternetGatewayExists(resourceName, &v),
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`internet-gateway/igw-.+`)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand All @@ -161,8 +162,7 @@ func TestAccAWSInternetGateway_basic(t *testing.T) {
{
Config: testAccInternetGatewayConfigChangeVPC,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(
resourceName, &v2),
testAccCheckInternetGatewayExists(resourceName, &v2),
testNotEqual,
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),
),
Expand Down Expand Up @@ -243,6 +243,27 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
})
}

func TestAccAWSInternetGateway_disappears(t *testing.T) {
var v ec2.InternetGateway
resourceName := "aws_internet_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInternetGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccInternetGatewayConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(resourceName, &v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsInternetGateway(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

Expand All @@ -264,11 +285,7 @@ func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
}

// Verify the error is what we want
ec2err, ok := err.(awserr.Error)
if !ok {
return err
}
if ec2err.Code() != "InvalidInternetGatewayID.NotFound" {
if !isAWSErr(err, "InvalidInternetGatewayID.NotFound", "") {
return err
}
}
Expand Down Expand Up @@ -323,9 +340,6 @@ resource "aws_vpc" "test" {

resource "aws_internet_gateway" "test" {
vpc_id = "${aws_vpc.test.id}"
tags = {
Name = "terraform-testacc-internet-gateway"
}
}
`

Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/internet_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ which take the following arguments:

## Attributes Reference

* `arn` - The ARN of the Internet Gateway.

All of the argument attributes except `filter` block are also exported as
result attributes. This data source will complete the data by populating
any fields that are not included in the configuration with the data for
Expand Down
4 changes: 3 additions & 1 deletion website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ for more information about connecting to alternate AWS endpoints or AWS compatib
- [`aws_guardduty_threatintelset` resource](/docs/providers/aws/r/guardduty_threatintelset.html)
- [`aws_instance` data source](/docs/providers/aws/d/instance.html)
- [`aws_instance` resource](/docs/providers/aws/r/instance.html)
- [`aws_key_pair` resource](/docs/providers/aws/r/key_pair.html)
- [`aws_internet_gateway` data source](/docs/providers/aws/d/internet_gateway.html)
- [`aws_internet_gateway` resource](/docs/providers/aws/r/internet_gateway.html)
- [`aws_key_pair` resource](/docs/providers/aws/r/key_pair.html)
- [`aws_launch_template` data source](/docs/providers/aws/d/launch_template.html)
- [`aws_launch_template` resource](/docs/providers/aws/r/launch_template.html)
- [`aws_redshift_cluster` resource](/docs/providers/aws/r/redshift_cluster.html)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/internet_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ resource "aws_instance" "foo" {
In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the Internet Gateway.
* `arn` - The ARN of the Internet Gateway.
* `owner_id` - The ID of the AWS account that owns the internet gateway.


Expand Down