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_vpc_dhcp_options_association: Support default DHCP Options ID #22722

Merged
merged 2 commits into from
Jan 22, 2022
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
3 changes: 3 additions & 0 deletions .changelog/22722.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_vpc_dhcp_options_association: Support `default` DHCP Options ID
```
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)
}