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

[New Resource] aws_default_internet_gateway #37899

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .changelog/37899.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_vpc_default_internet_gateway
```
1 change: 1 addition & 0 deletions .github/labeler-pr-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ service/vpc:
- changed-files:
- any-glob-to-any-file:
- 'internal/service/ec2/**/vpc_*'
- 'website/**/default_internet_gateway*'
- 'website/**/default_network_*'
- 'website/**/default_route_*'
- 'website/**/default_security_*'
Expand Down
8 changes: 8 additions & 0 deletions internal/service/ec2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions internal/service/ec2/vpc_default_internet_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ec2

import (
"context"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_default_internet_gateway", name="Internet Gateway")
// @Tags(identifierAttribute="id")
// @Testing(tagsTest=false)
func ResourceDefaultInternetGateway() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceDefaultInternetGatewayCreate,
ReadWithoutTimeout: resourceInternetGatewayRead,
UpdateWithoutTimeout: resourceInternetGatewayUpdate,
DeleteWithoutTimeout: resourceDefaultInternetGatewayDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
names.AttrARN: {
Type: schema.TypeString,
Computed: true,
},
names.AttrOwnerID: {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
names.AttrVPCID: {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"existing_default_internet_gateway": {
Type: schema.TypeBool,
Computed: true,
},
names.AttrForceDestroy: {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceDefaultInternetGatewayCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

// Check if there is a default VPC
input := &ec2.DescribeVpcsInput{
Filters: newAttributeFilterList(
map[string]string{
"isDefault": "true",
},
),
}
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
vpc, err := FindVPC(ctx, conn, input)
if err == nil {
log.Print("[INFO] Found existing attached EC2 Internet Gateway")
input := &ec2.DescribeInternetGatewaysInput{}
input.Filters = newAttributeFilterList(map[string]string{
"attachment.vpc-id": *vpc.VpcId,
})

igw, err := FindInternetGateway(ctx, conn, input)
log.Printf("found igw with ID: %s", igw)

if err == nil {
d.SetId(aws.StringValue(igw.InternetGatewayId))
d.Set("existing_default_internet_gateway", true)
} else if tfresource.NotFound(err) {
log.Print("[INFO] Found default VPC without attached EC2 Internet Gateway. Creating and attaching one")
input := &ec2.CreateInternetGatewayInput{}
output, err := conn.CreateInternetGatewayWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "Creating EC2 Internet Gateway: %s", err)
}

igw = output.InternetGateway

d.SetId(aws.StringValue(igw.InternetGatewayId))
d.Set("existing_default_internet_gateway", false)

if err := attachInternetGateway(ctx, conn, d.Id(), *vpc.VpcId, d.Timeout(schema.TimeoutDelete)); err != nil {
return sdkdiag.AppendErrorf(diags, "Attaching EC2 Internet Gateway (%s): %s", d.Id(), err)
}
} else {
return sdkdiag.AppendErrorf(diags, "Creating EC2 Internet Gateway (%s): %s", d.Id(), err)
}
}
return append(diags, resourceInternetGatewayRead(ctx, d, meta)...)
}

func resourceDefaultInternetGatewayDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
if d.Get(names.AttrForceDestroy).(bool) {
// See if the VPC assigned to the IGW has the isDefault property
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
input := &ec2.DescribeVpcsInput{
Filters: newAttributeFilterList(
map[string]string{
"isDefault": "true",
"vpc-id": d.Get(names.AttrVPCID).(string),
},
),
}
_, err := FindVPC(ctx, conn, input)

if err == nil {
// Detach if it is attached.
if v, ok := d.GetOk(names.AttrVPCID); ok {
if err := detachInternetGateway(ctx, conn, d.Id(), v.(string), d.Timeout(schema.TimeoutDelete)); err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EC2 Internet Gateway (%s): %s", d.Id(), err)
}

input := &ec2.DeleteInternetGatewayInput{
InternetGatewayId: aws.String(d.Id()),
}

log.Printf("[INFO] Deleting Internet Gateway: %s", d.Id())
_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, d.Timeout(schema.TimeoutDelete), func() (interface{}, error) {
return conn.DeleteInternetGatewayWithContext(ctx, input)
}, errCodeDependencyViolation)

if tfawserr.ErrCodeEquals(err, errCodeInvalidInternetGatewayIDNotFound) {
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EC2 Internet Gateway (%s): %s", d.Id(), err)
}
}
}
}
log.Printf("[INFO] Skipping Internet Gateway: %s", d.Id())
return diags
}
Loading
Loading