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/aws_subnet: Resource-based naming is not available in the ap-southeast-3 region #22531

Merged
merged 4 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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/22531.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_subnet: Resource-based naming is not available in the `ap-southeast-3` region
```
16 changes: 16 additions & 0 deletions internal/service/ec2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@ func StatusSubnetMapPublicIPOnLaunch(conn *ec2.EC2, id string) resource.StateRef
}
}

func StatusSubnetPrivateDNSHostnameTypeOnLaunch(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindSubnetByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.PrivateDnsNameOptionsOnLaunch.HostnameType), nil
}
}

func StatusTransitGatewayPrefixListReferenceState(conn *ec2.EC2, transitGatewayRouteTableID string, prefixListID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
transitGatewayPrefixListReference, err := FindTransitGatewayPrefixListReference(conn, transitGatewayRouteTableID, prefixListID)
Expand Down
13 changes: 12 additions & 1 deletion internal/service/ec2/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func resourceSubnetCreate(d *schema.ResourceData, meta interface{}) error {
input := &ec2.CreateSubnetInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
AvailabilityZoneId: aws.String(d.Get("availability_zone_id").(string)),
Ipv6Native: aws.Bool(d.Get("ipv6_native").(bool)),
TagSpecifications: ec2TagSpecificationsFromKeyValueTags(tags, ec2.ResourceTypeSubnet),
VpcId: aws.String(d.Get("vpc_id").(string)),
}
Expand All @@ -160,6 +159,10 @@ func resourceSubnetCreate(d *schema.ResourceData, meta interface{}) error {
input.Ipv6CidrBlock = aws.String(v.(string))
}

if v, ok := d.GetOk("ipv6_native"); ok {
input.Ipv6Native = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("outpost_arn"); ok {
input.OutpostArn = aws.String(v.(string))
}
Expand Down Expand Up @@ -291,6 +294,10 @@ func resourceSubnetCreate(d *schema.ResourceData, meta interface{}) error {
if _, err := conn.ModifySubnetAttribute(input); err != nil {
return fmt.Errorf("error setting EC2 Subnet (%s) PrivateDnsHostnameTypeOnLaunch: %w", d.Id(), err)
}

if _, err := WaitSubnetPrivateDNSHostnameTypeOnLaunchUpdated(conn, d.Id(), d.Get("private_dns_hostname_type_on_launch").(string)); err != nil {
return fmt.Errorf("error waiting for EC2 Subnet (%s) PrivateDnsHostnameTypeOnLaunch update: %w", d.Id(), err)
}
}

return resourceSubnetRead(d, meta)
Expand Down Expand Up @@ -480,6 +487,10 @@ func resourceSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
if _, err := conn.ModifySubnetAttribute(input); err != nil {
return fmt.Errorf("error setting EC2 Subnet (%s) PrivateDnsHostnameTypeOnLaunch: %w", d.Id(), err)
}

if _, err := WaitSubnetPrivateDNSHostnameTypeOnLaunchUpdated(conn, d.Id(), d.Get("private_dns_hostname_type_on_launch").(string)); err != nil {
return fmt.Errorf("error waiting for EC2 Subnet (%s) PrivateDnsHostnameTypeOnLaunch update: %w", d.Id(), err)
}
}

if d.HasChange("ipv6_cidr_block") {
Expand Down
19 changes: 18 additions & 1 deletion internal/service/ec2/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/ec2"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -22,6 +23,12 @@ func TestAccEC2Subnet_basic(t *testing.T) {
resourceName := "aws_subnet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

// Resource-based naming is not available in the AWS Asia Pacific (Jakarta) Region.
defaultPrivateDNSHostnameTypeOnLaunch := "ip-name"
if acctest.Region() == endpoints.ApSoutheast3RegionID {
defaultPrivateDNSHostnameTypeOnLaunch = ""
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
Expand All @@ -46,7 +53,7 @@ func TestAccEC2Subnet_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "map_public_ip_on_launch", "false"),
resource.TestCheckResourceAttr(resourceName, "outpost_arn", ""),
acctest.CheckResourceAttrAccountID(resourceName, "owner_id"),
resource.TestCheckResourceAttr(resourceName, "private_dns_hostname_type_on_launch", "ip-name"),
resource.TestCheckResourceAttr(resourceName, "private_dns_hostname_type_on_launch", defaultPrivateDNSHostnameTypeOnLaunch),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
Expand Down Expand Up @@ -808,6 +815,11 @@ func TestAccEC2Subnet_enableDNS64(t *testing.T) {
}

func TestAccEC2Subnet_privateDnsNameOptionsOnLaunch(t *testing.T) {
// Resource-based naming is not available in the AWS Asia Pacific (Jakarta) Region.
if region := acctest.Region(); region == endpoints.ApSoutheast3RegionID {
t.Skipf("Resource-based naming is not available in the %s region", region)
}

var subnet ec2.Subnet
resourceName := "aws_subnet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -855,6 +867,11 @@ func TestAccEC2Subnet_privateDnsNameOptionsOnLaunch(t *testing.T) {
}

func TestAccEC2Subnet_ipv6Native(t *testing.T) {
// IPv6-only subnets are not available in the AWS Asia Pacific (Jakarta) Region.
if region := acctest.Region(); region == endpoints.ApSoutheast3RegionID {
t.Skipf("Resource-based naming is not available in the %s region", region)
}

var v ec2.Subnet
resourceName := "aws_subnet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down
18 changes: 18 additions & 0 deletions internal/service/ec2/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,24 @@ func WaitSubnetMapPublicIPOnLaunchUpdated(conn *ec2.EC2, subnetID string, expect
return nil, err
}

func WaitSubnetPrivateDNSHostnameTypeOnLaunchUpdated(conn *ec2.EC2, subnetID string, expectedValue string) (*ec2.Subnet, error) {
stateConf := &resource.StateChangeConf{
Target: []string{expectedValue},
Refresh: StatusSubnetPrivateDNSHostnameTypeOnLaunch(conn, subnetID),
Timeout: SubnetAttributePropagationTimeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*ec2.Subnet); ok {
return output, err
}

return nil, err
}

const (
TransitGatewayPrefixListReferenceTimeout = 5 * time.Minute
)
Expand Down