Skip to content

Commit

Permalink
r/aws_vpc_dhcp_options_association: Support 'default' DHCP Options ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jan 21, 2022
1 parent 6ff11f8 commit 837c4b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/service/ec2/vpc_dhcp_options_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions internal/service/ec2/vpc_dhcp_options_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}

0 comments on commit 837c4b9

Please sign in to comment.