diff --git a/.changelog/22722.txt b/.changelog/22722.txt new file mode 100644 index 000000000000..12d40bd8756e --- /dev/null +++ b/.changelog/22722.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_vpc_dhcp_options_association: Support `default` DHCP Options ID +``` \ No newline at end of file diff --git a/internal/service/ec2/vpc_dhcp_options_association.go b/internal/service/ec2/vpc_dhcp_options_association.go index 6577b304f7cd..df9ad58ed8b7 100644 --- a/internal/service/ec2/vpc_dhcp_options_association.go +++ b/internal/service/ec2/vpc_dhcp_options_association.go @@ -154,9 +154,17 @@ func VPCDHCPOptionsAssociationCreateResourceID(dhcpOptionsID, vpcID string) stri func VPCDHCPOptionsAssociationParseResourceID(id string) (string, string, error) { parts := strings.Split(id, vpcDHCPOptionsAssociationResourceIDSeparator) - // DHCP Options ID and VPC ID themselves contain '-'. - if len(parts) == 4 && parts[0] != "" && parts[1] != "" && parts[2] != "" && parts[3] != "" { - return strings.Join([]string{parts[0], parts[1]}, vpcDHCPOptionsAssociationResourceIDSeparator), strings.Join([]string{parts[2], parts[3]}, vpcDHCPOptionsAssociationResourceIDSeparator), nil + // The DHCP Options ID either contains '-' or is the special value "default". + // The VPC ID contains '-'. + switch n := len(parts); n { + case 3: + if parts[0] == DefaultDHCPOptionsID && parts[1] != "" && parts[2] != "" { + return parts[0], strings.Join([]string{parts[1], parts[2]}, vpcDHCPOptionsAssociationResourceIDSeparator), nil + } + case 4: + if parts[0] != "" && parts[1] != "" && parts[2] != "" && parts[3] != "" { + return strings.Join([]string{parts[0], parts[1]}, vpcDHCPOptionsAssociationResourceIDSeparator), strings.Join([]string{parts[2], parts[3]}, vpcDHCPOptionsAssociationResourceIDSeparator), nil + } } return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected DHCPOptionsID%[2]sVPCID", id, vpcDHCPOptionsAssociationResourceIDSeparator) diff --git a/internal/service/ec2/vpc_dhcp_options_association_test.go b/internal/service/ec2/vpc_dhcp_options_association_test.go index abe1810889fc..40de707e9b2e 100644 --- a/internal/service/ec2/vpc_dhcp_options_association_test.go +++ b/internal/service/ec2/vpc_dhcp_options_association_test.go @@ -106,6 +106,32 @@ func TestAccEC2VPCDHCPOptionsAssociation_disappears(t *testing.T) { }) } +func TestAccEC2VPCDHCPOptionsAssociation_default(t *testing.T) { + resourceName := "aws_vpc_dhcp_options_association.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckVPCDHCPOptionsAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccVPCDHCPOptionsAssociationDefaultConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckVPCDHCPOptionsAssociationExist(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccVPCDHCPOptionsAssociationVPCImportIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccVPCDHCPOptionsAssociationVPCImportIdFunc(resourceName string) resource.ImportStateIdFunc { return func(s *terraform.State) (string, error) { rs, ok := s.RootModule().Resources[resourceName] @@ -204,3 +230,20 @@ resource "aws_vpc_dhcp_options_association" "test" { } `, rName) } + +func testAccVPCDHCPOptionsAssociationDefaultConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "10.1.0.0/16" + + tags = { + Name = %[1]q + } +} + +resource "aws_vpc_dhcp_options_association" "test" { + vpc_id = aws_vpc.test.id + dhcp_options_id = "default" +} +`, rName) +}